For a column that is used in a FK constraint to be renamed, the FK constraint has to be
dropped first, then the column can be renamed and last the FK constraint needs to be
added back (using the new name, of course).
This method helps with the task of handling the FK constraints during this. Given a list
of tables that contain columns to be renamed and a search/replace pair for the column name,
it will return an array with arrays with drop and add SQL statements.
Use them like this before and after renaming the affected fields:
collect foreign keys pointing to "our" tables
$tableNames = array(...);
$foreignKeyHandlingSql = $this->getForeignKeyHandlingSql($schema, $tableNames, 'old_name', 'new_name');
drop FK constraints
foreach ($foreignKeyHandlingSql['drop'] as $sql) {
$this->addSql($sql);
}
rename columns now
add back FK constraints
foreach ($foreignKeyHandlingSql['add'] as $sql) {
$this->addSql($sql);
}
public static getForeignKeyHandlingSql ( Doctrine\DBAL\Schema\Schema $schema, Doctrine\DBAL\Platforms\AbstractPlatform $platform, array $tableNames, string $search, string $replace ) : array | ||
$schema | Doctrine\DBAL\Schema\Schema | |
$platform | Doctrine\DBAL\Platforms\AbstractPlatform | |
$tableNames | array | |
$search | string | |
$replace | string | |
return | array |