public function getMigrationTableDDL(TableDiff $tableDiff)
    {
        $pattern = "
CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s;
DROP TABLE %s;
%s
INSERT INTO %s (%s) SELECT %s FROM %s;
DROP TABLE %s;
";

        $originTable     = clone $tableDiff->getFromTable();
        $newTable        = clone $tableDiff->getToTable();

        $originTableName = $originTable->getName();
        $tempTableName   = $newTable->getCommonName().'__temp__'.uniqid();

        $originTableFields = $this->getColumnListDDL($originTable->getColumns());

        $fieldMap = []; /** struct: [<oldCol> => <newCol>] */
        //start with modified columns
        foreach ($tableDiff->getModifiedColumns() as $diff) {
            $fieldMap[$diff->getFromColumn()->getName()] = $diff->getToColumn()->getName();
        }

        foreach ($tableDiff->getRenamedColumns() as $col) {
            list ($from, $to) = $col;
            $fieldMap[$from->getName()] = $to->getName();
        }

        foreach ($newTable->getColumns() as $col) {
            if ($originTable->hasColumn($col)) {
                if (!isset($fieldMap[$col->getName()])) {
                    $fieldMap[$col->getName()] = $col->getName();
                }
            }

        }

        $createTable = $this->getAddTableDDL($newTable);
        $createTable .= $this->getAddIndicesDDL($newTable);

        $sql = sprintf($pattern,
            $this->quoteIdentifier($tempTableName), //CREATE TEMPORARY TABLE %s
            $originTableFields, //select %s
            $this->quoteIdentifier($originTableName), //from %s
            $this->quoteIdentifier($originTableName), //drop table %s
            $createTable, //[create table] %s
            $this->quoteIdentifier($originTableName), //insert into %s
            implode(', ', $fieldMap), //(%s)
            implode(', ', array_keys($fieldMap)), //select %s
            $this->quoteIdentifier($tempTableName), //from %s
            $this->quoteIdentifier($tempTableName) //drop table %s
        );

        return $sql;
    }
public function getMigrationTableDDL(TableDiff $tableDiff)
    {
        $pattern = "
CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s;
DROP TABLE %s;
%s
INSERT INTO %s (%s) SELECT %s FROM %s;
DROP TABLE %s;
";

        $originTable     = clone $tableDiff->getFromTable();
        $newTable        = clone $tableDiff->getToTable();

        $originTableName = $originTable->getName();
        $tempTableName   = $newTable->getCommonName().'__temp__'.uniqid();

        $originTableFields = $this->getColumnListDDL($originTable->getColumns());

        $fieldMap = []; /** struct: [<oldCol> => <newCol>] */
        //start with modified columns
        foreach ($tableDiff->getModifiedColumns() as $diff) {
            $fieldMap[$diff->getFromColumn()->getName()] = $diff->getToColumn()->getName();
        }

        foreach ($tableDiff->getRenamedColumns() as $col) {
            list ($from, $to) = $col;
            $fieldMap[$from->getName()] = $to->getName();
        }

        foreach ($newTable->getColumns() as $col) {
            if ($originTable->hasColumn($col)) {
                if (!isset($fieldMap[$col->getName()])) {
                    $fieldMap[$col->getName()] = $col->getName();
                }
            }

        }

        $createTable = $this->getAddTableDDL($newTable);
        $createTable .= $this->getAddIndicesDDL($newTable);

        $sql = sprintf($pattern,
            $this->quoteIdentifier($tempTableName), //CREATE TEMPORARY TABLE %s
            $originTableFields, //select %s
            $this->quoteIdentifier($originTableName), //from %s
            $this->quoteIdentifier($originTableName), //drop table %s
            $createTable, //[create table] %s
            $this->quoteIdentifier($originTableName), //insert into %s
            implode(', ', $fieldMap), //(%s)
            implode(', ', array_keys($fieldMap)), //select %s
            $this->quoteIdentifier($tempTableName), //from %s
            $this->quoteIdentifier($tempTableName) //drop table %s
        );

        return $sql;
    }
public function getMigrationTableDDL ( TableDiff $ tableDiff ) { $ pattern = "
CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s;
DROP TABLE %s;
%s
INSERT INTO %s (%s) SELECT %s FROM %s;
DROP TABLE %s;
" ; $ originTable = clone $ tableDiff -> getFromTable ( ) ; $ newTable = clone $ tableDiff -> getToTable ( ) ; $ originTableName = $ originTable -> getName ( ) ; $ tempTableName = $ newTable -> getCommonName ( ) . '__temp__' . uniqid ( ) ; $ originTableFields = $ this -> getColumnListDDL ( $ originTable -> getColumns ( ) ) ; $ fieldMap = [ ] ; /** struct: [<oldCol> => <newCol>] */ //start with modified columns foreach ( $ tableDiff -> getModifiedColumns ( ) as $ diff ) { $ fieldMap [ $ diff -> getFromColumn ( ) -> getName ( ) ] = $ diff -> getToColumn ( ) -> getName ( ) ; } foreach ( $ tableDiff -> getRenamedColumns ( ) as $ col ) { list ( $ from , $ to ) = $ col ; $ fieldMap [ $ from -> getName ( ) ] = $ to -> getName ( ) ; } foreach ( $ newTable -> getColumns ( ) as $ col ) { if ( $ originTable -> hasColumn ( $ col ) ) { if ( ! isset ( $ fieldMap [ $ col -> getName ( ) ] ) ) { $ fieldMap [ $ col -> getName ( ) ] = $ col -> getName ( ) ; } } } $ createTable = $ this -> getAddTableDDL ( $ newTable ) ; $ createTable .= $ this -> getAddIndicesDDL ( $ newTable ) ; $ sql = sprintf ( $ pattern , $ this -> quoteIdentifier ( $ tempTableName ) , //CREATE TEMPORARY TABLE %s $ originTableFields , //select %s $ this -> quoteIdentifier ( $ originTableName ) , //from %s $ this -> quoteIdentifier ( $ originTableName ) , //drop table %s $ createTable , //[create table] %s $ this -> quoteIdentifier ( $ originTableName ) , //insert into %s implode ( ', ' , $ fieldMap ) , //(%s) implode ( ', ' , array_keys ( $ fieldMap ) ) , //select %s $ this -> quoteIdentifier ( $ tempTableName ) , //from %s $ this -> quoteIdentifier ( $ tempTableName ) //drop table %s ) ; return $ sql ; }
Creates a temporarily created table with the new schema,
moves all items into it and drops the origin as well as renames the temp table to the origin then.

@param  TableDiff $tableDiff
@return string
Creates a temporarily created table with the new schema moves all items into it and drops the origin as well as renames the temp table to the origin then .