protected function generateCreateOrUpdateDownCode($tableDiff, $isNewTable, $newOrUpdatedTable)
{
$result = '';
if ($isNewTable) {
$result = $this->generateTableDropCode($tableDiff->name);
} else {
$changedPrimaryKey = $this->getChangedOrRemovedPrimaryKey($tableDiff);
$addedPrimaryKey = $this->findPrimaryKeyIndex($tableDiff->addedIndexes, $newOrUpdatedTable);
if ($this->tableHasNameOrColumnChanges($tableDiff) || $changedPrimaryKey || $addedPrimaryKey) {
$hasColumnChanges = $this->tableHasNameOrColumnChanges($tableDiff, true);
if ($tableDiff->getNewName()) {
$result .= $this->generateTableRenameCode($tableDiff->newName, $tableDiff->name);
if ($hasColumnChanges || $changedPrimaryKey || $addedPrimaryKey) {
$result .= $this->eol;
}
}
if (!$hasColumnChanges && !$changedPrimaryKey && !$addedPrimaryKey) {
return $this->makeTabs($result);
}
$result .= $this->generateSchemaTableMethodStart($tableDiff->name, $isNewTable);
if ($changedPrimaryKey || $addedPrimaryKey) {
$result .= $this->generatePrimaryKeyDrop($newOrUpdatedTable);
}
foreach ($tableDiff->addedColumns as $column) {
$result .= $this->generateColumnDrop($column);
}
foreach ($tableDiff->changedColumns as $columnDiff) {
$result .= $this->generateColumnCode($columnDiff, self::COLUMN_MODE_REVERT);
}
foreach ($tableDiff->renamedColumns as $oldName => $column) {
$result .= $this->generateColumnRenameCode($column->getName(), $oldName);
}
foreach ($tableDiff->removedColumns as $name => $column) {
$result .= $this->generateColumnCode($column, self::COLUMN_MODE_CREATE);
}
if ($changedPrimaryKey || $addedPrimaryKey) {
$primaryKey = $this->findPrimaryKeyIndex($tableDiff->fromTable->getIndexes(), $tableDiff->fromTable);
if ($primaryKey) {
$result .= $this->generatePrimaryKeyCode($primaryKey, self::COLUMN_MODE_CREATE);
}
}
$result .= $this->generateSchemaTableMethodEnd();
}
}
return $this->makeTabs($result);
}