Models/DBOperator.php
public function getDifferenceSQL($varStructure, $tableName = null) {
        if (!$tableName) $tableName = $varStructure['table'];

        $current_structure = $this->getTableStructure($tableName, true);
        $res = array('result'=>false, 'sql'=>array());
        if (!$current_structure) {
            $sql = $this->generateTableSQL($varStructure);
            $res['result'] = true;
            $res['sql']['ADD'][] = $sql;
            return $res;
        }
        if (!isset($varStructure['columns'])) $varStructure['columns'] = array();
        if (!isset($varStructure['table'])) $varStructure['table'] = $tableName;

        // synchronizing columns
        foreach($varStructure['columns'] as $name=>$info) {
            if (!empty($varStructure['abilities']['mlt']['columns']) && in_array($name, $varStructure['abilities']['mlt']['columns'])) continue;
            if (!is_array($info)) $info = array('type'=>$info);
            if (!isset($info['name'])) $info['name'] = $name;
            if (!empty($varStructure['columns'][$name]['virtual'])) continue;

            if (!isset($current_structure['columns'][$name]) && !(isset($info['old_name']) && isset($current_structure['columns'][$info['old_name']]))) {
                if ((isset($info['auto_increment']))) {
                    unset($info['auto_increment']);
                    $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD COLUMN '.$this->generateColumnSQL($info);
                    if (!isset($current_structure['indexes']['primary'])) {
                        $varStructure['indexes']['primary'] = array('columns'=>$name);
                        $current_structure['indexes']['primary'] = array('columns'=>$name);
                        $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD PRIMARY KEY (`'.$name.'`)';
                    }
                    $info['auto_increment'] = true;
                    $res['sql']['CHANGE'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' MODIFY COLUMN '.$this->generateColumnSQL($info);
                } else {
                    $res['sql']['ADD'][]  = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD COLUMN '.$this->generateColumnSQL($info);
                }

            } else {
                if (isset($info['old_name'])) {
                    $current_structure['columns'][$name] = $current_structure['columns'][$info['old_name']];
                    unset($current_structure['columns'][$info['old_name']]);
                }
                $db_info = $current_structure['columns'][$name];
                if (
                    (isset($info['default']) && (strpos($info['default'], '#') === false) && (!isset($db_info['default']) || ($info['default'] != $db_info['default'])))
                    || (!isset($info['default']) && !empty($db_info['default']))
                    || ($info['type'] != $db_info['type'])
                    || isset($info['old_name'])
                    || (isset($info['auto_increment']) && !(isset($db_info['auto_increment'])))
                    || (!isset($info['auto_increment']) && isset($info['not_null']) && ($db_info['not_null'] != $info['not_null']))
                    || (!isset($info['auto_increment']) && !isset($info['not_null']) && $db_info['not_null'])
                ) {
                    if ((isset($info['auto_increment']) && !(isset($db_info['auto_increment'])))) {
                        if (!isset($current_structure['indexes']['primary'])) {
                            $varStructure['indexes']['primary'] = array('columns'=>$name);
                            $current_structure['indexes']['primary'] = array('columns'=>$name);
                            $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD PRIMARY KEY (`'.$name.'`)';
                        }
                    }
                    $res['sql']['CHANGE'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' CHANGE COLUMN '.(isset($info['old_name']) ? '' : '`'.$name.'` ').$this->generateColumnSQL($info);
                }
            }
        }
        // Dropping unused columns from DB
        foreach($current_structure['columns'] as $name=>$info) {
            if (!isset($varStructure['columns'][$name]) || !empty($varStructure['columns'][$name]['virtual'])) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'].' DROP COLUMN `'.$name.'`';
                $res['sql']['DROP'][] = $sql_diff;
            }
        }

        if (isset($varStructure['indexes'])) {
            // synchronizing indexes
            foreach($varStructure['indexes'] as $name=>$info) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'];

                if (!isset($current_structure['indexes'][$name])) {
                    if (!isset($info['name'])) $info['name'] = $name;
                    $sql_diff .= ' ADD '.$this->generateIndexSQL($info);
                    $res['sql']['ADD'][] = $sql_diff;;
                } else {
                    //@todo Altering indexes
                }
            }
        }

        // Dropping unused indexes from DB
        foreach($current_structure['indexes'] as $name=>$info) {
            if (!isset($varStructure['indexes'][$name])) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'].($name == 'primary' ? ' DROP PRIMARY KEY' : ' DROP KEY `'.$name.'`');
                $res['sql']['DROP'][] = $sql_diff;
            }
        }
        if (isset($varStructure['constraints'])) {
            // synchronizing constraints
            foreach($varStructure['constraints'] as $name=>$info) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'];
                if (!isset($current_structure['constraints'][$name])) {
                    $sql_diff .= ' ADD '.$this->generateConstraintSQL($info);
                    $res['sql']['ADD'][] = $sql_diff;;
                } else {
                    //@todo Altering indexes
                }
            }
        }


        if (count($res['sql'])) {
            $res['result'] = true;
        }
        return $res;
    }
public function getDifferenceSQL($varStructure, $tableName = null) {
        if (!$tableName) $tableName = $varStructure['table'];

        $current_structure = $this->getTableStructure($tableName, true);
        $res = array('result'=>false, 'sql'=>array());
        if (!$current_structure) {
            $sql = $this->generateTableSQL($varStructure);
            $res['result'] = true;
            $res['sql']['ADD'][] = $sql;
            return $res;
        }
        if (!isset($varStructure['columns'])) $varStructure['columns'] = array();
        if (!isset($varStructure['table'])) $varStructure['table'] = $tableName;

        // synchronizing columns
        foreach($varStructure['columns'] as $name=>$info) {
            if (!empty($varStructure['abilities']['mlt']['columns']) && in_array($name, $varStructure['abilities']['mlt']['columns'])) continue;
            if (!is_array($info)) $info = array('type'=>$info);
            if (!isset($info['name'])) $info['name'] = $name;
            if (!empty($varStructure['columns'][$name]['virtual'])) continue;

            if (!isset($current_structure['columns'][$name]) && !(isset($info['old_name']) && isset($current_structure['columns'][$info['old_name']]))) {
                if ((isset($info['auto_increment']))) {
                    unset($info['auto_increment']);
                    $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD COLUMN '.$this->generateColumnSQL($info);
                    if (!isset($current_structure['indexes']['primary'])) {
                        $varStructure['indexes']['primary'] = array('columns'=>$name);
                        $current_structure['indexes']['primary'] = array('columns'=>$name);
                        $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD PRIMARY KEY (`'.$name.'`)';
                    }
                    $info['auto_increment'] = true;
                    $res['sql']['CHANGE'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' MODIFY COLUMN '.$this->generateColumnSQL($info);
                } else {
                    $res['sql']['ADD'][]  = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD COLUMN '.$this->generateColumnSQL($info);
                }

            } else {
                if (isset($info['old_name'])) {
                    $current_structure['columns'][$name] = $current_structure['columns'][$info['old_name']];
                    unset($current_structure['columns'][$info['old_name']]);
                }
                $db_info = $current_structure['columns'][$name];
                if (
                    (isset($info['default']) && (strpos($info['default'], '#') === false) && (!isset($db_info['default']) || ($info['default'] != $db_info['default'])))
                    || (!isset($info['default']) && !empty($db_info['default']))
                    || ($info['type'] != $db_info['type'])
                    || isset($info['old_name'])
                    || (isset($info['auto_increment']) && !(isset($db_info['auto_increment'])))
                    || (!isset($info['auto_increment']) && isset($info['not_null']) && ($db_info['not_null'] != $info['not_null']))
                    || (!isset($info['auto_increment']) && !isset($info['not_null']) && $db_info['not_null'])
                ) {
                    if ((isset($info['auto_increment']) && !(isset($db_info['auto_increment'])))) {
                        if (!isset($current_structure['indexes']['primary'])) {
                            $varStructure['indexes']['primary'] = array('columns'=>$name);
                            $current_structure['indexes']['primary'] = array('columns'=>$name);
                            $res['sql']['ADD'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' ADD PRIMARY KEY (`'.$name.'`)';
                        }
                    }
                    $res['sql']['CHANGE'][] = 'ALTER TABLE' . ' ' . $varStructure['table'].' CHANGE COLUMN '.(isset($info['old_name']) ? '' : '`'.$name.'` ').$this->generateColumnSQL($info);
                }
            }
        }
        // Dropping unused columns from DB
        foreach($current_structure['columns'] as $name=>$info) {
            if (!isset($varStructure['columns'][$name]) || !empty($varStructure['columns'][$name]['virtual'])) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'].' DROP COLUMN `'.$name.'`';
                $res['sql']['DROP'][] = $sql_diff;
            }
        }

        if (isset($varStructure['indexes'])) {
            // synchronizing indexes
            foreach($varStructure['indexes'] as $name=>$info) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'];

                if (!isset($current_structure['indexes'][$name])) {
                    if (!isset($info['name'])) $info['name'] = $name;
                    $sql_diff .= ' ADD '.$this->generateIndexSQL($info);
                    $res['sql']['ADD'][] = $sql_diff;;
                } else {
                    //@todo Altering indexes
                }
            }
        }

        // Dropping unused indexes from DB
        foreach($current_structure['indexes'] as $name=>$info) {
            if (!isset($varStructure['indexes'][$name])) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'].($name == 'primary' ? ' DROP PRIMARY KEY' : ' DROP KEY `'.$name.'`');
                $res['sql']['DROP'][] = $sql_diff;
            }
        }
        if (isset($varStructure['constraints'])) {
            // synchronizing constraints
            foreach($varStructure['constraints'] as $name=>$info) {
                $sql_diff = 'ALTER TABLE' . ' ' . $varStructure['table'];
                if (!isset($current_structure['constraints'][$name])) {
                    $sql_diff .= ' ADD '.$this->generateConstraintSQL($info);
                    $res['sql']['ADD'][] = $sql_diff;;
                } else {
                    //@todo Altering indexes
                }
            }
        }


        if (count($res['sql'])) {
            $res['result'] = true;
        }
        return $res;
    }
Find differences between table structure in DB and $var_info structure

@param mixed $varStructure array with structure for diff
@param string $tableName specified table if not specified in structure
@return array table_exists true|false and sql array for altering