public static function findDifferences(Dialect $dialect, DBTable $source, DBTable $target) { $out = []; $head = 'ALTER TABLE ' . $dialect->quoteTable($target->getName()); $sourceColumns = $source->getColumns(); $targetColumns = $target->getColumns(); foreach ($sourceColumns as $name => $column) { if (isset($targetColumns[$name])) { if ($column->getType()->getId() != $targetColumns[$name]->getType()->getId()) { $targetColumn = $targetColumns[$name]; $out[] = $head . ' ALTER COLUMN ' . $dialect->quoteField($name) . ' TYPE ' . $targetColumn->getType()->toString() . ($targetColumn->getType()->hasSize() ? '(' . $targetColumn->getType()->getSize() . ($targetColumn->getType()->hasPrecision() ? ', ' . $targetColumn->getType()->getPrecision() : null) . ')' : null) . (in_array($targetColumn->getType()->getId(), array(DataType::JSON, DataType::JSONB)) ? ' USING NULL' : '') . ';'; } if ($column->getType()->isNull() != $targetColumns[$name]->getType()->isNull()) { $out[] = $head . ' ALTER COLUMN ' . $dialect->quoteField($name) . ' ' . ($targetColumns[$name]->getType()->isNull() ? 'DROP' : 'SET') . ' NOT NULL;'; } } else { $out[] = $head . ' DROP COLUMN ' . $dialect->quoteField($name) . ';'; } } foreach ($targetColumns as $name => $column) { if (!isset($sourceColumns[$name])) { $out[] = $head . ' ADD COLUMN ' . $column->toDialectString($dialect) . ';'; if ($column->hasReference()) { $out[] = 'CREATE INDEX ' . $dialect->quoteField($name . '_idx') . ' ON ' . $dialect->quoteTable($target->getName()) . '(' . $dialect->quoteField($name) . ');'; } } } return $out; }