protected function alterTableMigrate($tablename, array $alterDef, array $options = []) {
        $currentDef = $this->getTableDef($tablename);

        // Merge the table definitions if we aren't dropping stuff.
        if (!val(Db::OPTION_DROP, $options)) {
            $tableDef = $this->mergeTableDefs($currentDef, $alterDef);
        } else {
            $tableDef = $alterDef['def'];
        }

        // Drop all of the indexes on the current table.
        foreach (val('indexes', $currentDef, []) as $indexDef) {
            if (val('type', $indexDef, Db::INDEX_IX) === Db::INDEX_IX) {
                $this->dropIndex($indexDef['name']);
            }
        }

        $tmpTablename = $tablename.'_'.time();

        // Rename the current table.
        $this->renameTable($tablename, $tmpTablename);

        // Create the new table.
        $this->createTable($tablename, $tableDef, $options);

        // Figure out the columns that we can insert.
        $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns']));

        // Build the insert/select statement.
        $sql = 'insert into '.$this->backtick($this->px.$tablename)."\n".
            $this->bracketList($columns, '`')."\n".
            $this->buildSelect($tmpTablename, [], ['columns' => $columns]);

        $this->query($sql, Db::QUERY_WRITE);

        // Drop the temp table.
        $this->dropTable($tmpTablename);
    }
protected function alterTableMigrate($tablename, array $alterDef, array $options = []) {
        $currentDef = $this->getTableDef($tablename);

        // Merge the table definitions if we aren't dropping stuff.
        if (!val(Db::OPTION_DROP, $options)) {
            $tableDef = $this->mergeTableDefs($currentDef, $alterDef);
        } else {
            $tableDef = $alterDef['def'];
        }

        // Drop all of the indexes on the current table.
        foreach (val('indexes', $currentDef, []) as $indexDef) {
            if (val('type', $indexDef, Db::INDEX_IX) === Db::INDEX_IX) {
                $this->dropIndex($indexDef['name']);
            }
        }

        $tmpTablename = $tablename.'_'.time();

        // Rename the current table.
        $this->renameTable($tablename, $tmpTablename);

        // Create the new table.
        $this->createTable($tablename, $tableDef, $options);

        // Figure out the columns that we can insert.
        $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns']));

        // Build the insert/select statement.
        $sql = 'insert into '.$this->backtick($this->px.$tablename)."\n".
            $this->bracketList($columns, '`')."\n".
            $this->buildSelect($tmpTablename, [], ['columns' => $columns]);

        $this->query($sql, Db::QUERY_WRITE);

        // Drop the temp table.
        $this->dropTable($tmpTablename);
    }
protected function alterTableMigrate ( $ tablename , array $ alterDef , array $ options = [ ] ) { $ currentDef = $ this -> getTableDef ( $ tablename ) ; // Merge the table definitions if we aren't dropping stuff. if ( ! val ( Db :: OPTION_DROP , $ options ) ) { $ tableDef = $ this -> mergeTableDefs ( $ currentDef , $ alterDef ) ; } else { $ tableDef = $ alterDef [ 'def' ] ; } // Drop all of the indexes on the current table. foreach ( val ( 'indexes' , $ currentDef , [ ] ) as $ indexDef ) { if ( val ( 'type' , $ indexDef , Db :: INDEX_IX ) === Db :: INDEX_IX ) { $ this -> dropIndex ( $ indexDef [ 'name' ] ) ; } } $ tmpTablename = $ tablename . '_' . time ( ) ; // Rename the current table. $ this -> renameTable ( $ tablename , $ tmpTablename ) ; // Create the new table. $ this -> createTable ( $ tablename , $ tableDef , $ options ) ; // Figure out the columns that we can insert. $ columns = array_keys ( array_intersect_key ( $ tableDef [ 'columns' ] , $ currentDef [ 'columns' ] ) ) ; // Build the insert/select statement. $ sql = 'insert into ' . $ this -> backtick ( $ this -> px . $ tablename ) . "\n" . $ this -> bracketList ( $ columns , '`' ) . "\n" . $ this -> buildSelect ( $ tmpTablename , [ ] , [ 'columns' => $ columns ] ) ; $ this -> query ( $ sql , Db :: QUERY_WRITE ) ; // Drop the temp table. $ this -> dropTable ( $ tmpTablename ) ; }
Alter a table by creating a new table and copying the old table's data to it.

@param string $tablename The table to alter.
@param array $alterDef The new definition.
@param array $options An array of options for the migration.
Alter a table by creating a new table and copying the old table s data to it .