/** * Builds the DDL SQL to alter a table's foreign keys * based on a TableDiff instance * * @return string */ public function getModifyTableForeignKeysDDL(TableDiff $tableDiff) { $ret = ''; foreach ($tableDiff->getRemovedFks() as $fk) { $ret .= $this->getDropForeignKeyDDL($fk); } foreach ($tableDiff->getAddedFks() as $fk) { $ret .= $this->getAddForeignKeyDDL($fk); } foreach ($tableDiff->getModifiedFks() as $fkModification) { list($fromFk, $toFk) = $fkModification; $ret .= $this->getDropForeignKeyDDL($fromFk); $ret .= $this->getAddForeignKeyDDL($toFk); } return $ret; }
public function getModifyTableDDL(TableDiff $tableDiff) { $alterTableStatements = ''; $toTable = $tableDiff->getToTable(); // drop indices, foreign keys foreach ($tableDiff->getRemovedFks() as $fk) { $alterTableStatements .= $this->getDropForeignKeyDDL($fk); } foreach ($tableDiff->getModifiedFks() as $fkModification) { list($fromFk) = $fkModification; $alterTableStatements .= $this->getDropForeignKeyDDL($fromFk); } foreach ($tableDiff->getRemovedIndices() as $index) { $alterTableStatements .= $this->getDropIndexDDL($index); } foreach ($tableDiff->getModifiedIndices() as $indexModification) { list($fromIndex) = $indexModification; $alterTableStatements .= $this->getDropIndexDDL($fromIndex); } // alter table structure if ($tableDiff->hasModifiedPk()) { $alterTableStatements .= $this->getDropPrimaryKeyDDL($tableDiff->getFromTable()); } foreach ($tableDiff->getRenamedColumns() as $columnRenaming) { $alterTableStatements .= $this->getRenameColumnDDL($columnRenaming[0], $columnRenaming[1]); } if ($modifiedColumns = $tableDiff->getModifiedColumns()) { $alterTableStatements .= $this->getModifyColumnsDDL($modifiedColumns); } if ($addedColumns = $tableDiff->getAddedColumns()) { $alterTableStatements .= $this->getAddColumnsDDL($addedColumns); } foreach ($tableDiff->getRemovedColumns() as $column) { $alterTableStatements .= $this->getRemoveColumnDDL($column); } // add new indices and foreign keys if ($tableDiff->hasModifiedPk()) { $alterTableStatements .= $this->getAddPrimaryKeyDDL($tableDiff->getToTable()); } // create indices, foreign keys foreach ($tableDiff->getModifiedIndices() as $indexModification) { list($oldIndex, $toIndex) = $indexModification; $alterTableStatements .= $this->getAddIndexDDL($toIndex); } foreach ($tableDiff->getAddedIndices() as $index) { $alterTableStatements .= $this->getAddIndexDDL($index); } foreach ($tableDiff->getModifiedFks() as $fkModification) { list(, $toFk) = $fkModification; $alterTableStatements .= $this->getAddForeignKeyDDL($toFk); } foreach ($tableDiff->getAddedFks() as $fk) { $alterTableStatements .= $this->getAddForeignKeyDDL($fk); } $ret = ''; if (trim($alterTableStatements)) { //merge all changes into one command. This prevents https://github.com/propelorm/Propel2/issues/1115 $changes = explode(';', $alterTableStatements); $changeFragments = []; foreach ($changes as $change) { if (trim($change)) { $changeFragments[] = preg_replace(sprintf('/ALTER TABLE %s /', $this->quoteIdentifier($toTable->getName())), "\n\n ", trim($change)); } } $ret .= sprintf("\nALTER TABLE %s%s;\n", $this->quoteIdentifier($toTable->getName()), implode(',', $changeFragments)); } return $ret; }