public function getDiff($table)
{
Logger::info("Now calculating schema diff for table `{$table}`");
$diffSequence = [];
$sourceSchema = $this->getSchema('source', $table);
$targetSchema = $this->getSchema('target', $table);
// Engine
$sourceEngine = $sourceSchema['engine'];
$targetEngine = $targetSchema['engine'];
if ($sourceEngine != $targetEngine) {
$diffSequence[] = new AlterTableEngine($table, $sourceEngine, $targetEngine);
}
// Collation
$sourceCollation = $sourceSchema['collation'];
$targetCollation = $targetSchema['collation'];
if ($sourceCollation != $targetCollation) {
$diffSequence[] = new AlterTableCollation($table, $sourceCollation, $targetCollation);
}
// Columns
$sourceColumns = $sourceSchema['columns'];
$targetColumns = $targetSchema['columns'];
$differ = new MapDiffer();
$diffs = $differ->doDiff($targetColumns, $sourceColumns);
foreach ($diffs as $column => $diff) {
if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
$diffSequence[] = new AlterTableDropColumn($table, $column, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
$diffSequence[] = new AlterTableChangeColumn($table, $column, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
$diffSequence[] = new AlterTableAddColumn($table, $column, $diff);
}
}
}
}
// Keys
$sourceKeys = $sourceSchema['keys'];
$targetKeys = $targetSchema['keys'];
$differ = new MapDiffer();
$diffs = $differ->doDiff($targetKeys, $sourceKeys);
foreach ($diffs as $key => $diff) {
if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
$diffSequence[] = new AlterTableDropKey($table, $key, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
$diffSequence[] = new AlterTableChangeKey($table, $key, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
$diffSequence[] = new AlterTableAddKey($table, $key, $diff);
}
}
}
}
// Constraints
$sourceConstraints = $sourceSchema['constraints'];
$targetConstraints = $targetSchema['constraints'];
$differ = new MapDiffer();
$diffs = $differ->doDiff($targetConstraints, $sourceConstraints);
foreach ($diffs as $name => $diff) {
if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
$diffSequence[] = new AlterTableDropConstraint($table, $name, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
$diffSequence[] = new AlterTableChangeConstraint($table, $name, $diff);
} else {
if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
$diffSequence[] = new AlterTableAddConstraint($table, $name, $diff);
}
}
}
}
return $diffSequence;
}