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 |
public static function getForeignKeyHandlingSql(Schema $schema, AbstractPlatform $platform, $tableNames, $search, $replace)
{
$foreignKeyHandlingSql = ['drop' => [], 'add' => []];
$tables = $schema->getTables();
foreach ($tables as $table) {
$foreignKeys = $table->getForeignKeys();
foreach ($foreignKeys as $foreignKey) {
if (!in_array($table->getName(), $tableNames) && !in_array($foreignKey->getForeignTableName(), $tableNames)) {
continue;
}
$localColumns = $foreignKey->getLocalColumns();
$foreignColumns = $foreignKey->getForeignColumns();
if (in_array($search, $foreignColumns) || in_array($search, $localColumns)) {
if (in_array($foreignKey->getLocalTableName(), $tableNames)) {
array_walk($localColumns, function (&$value) use($search, $replace) {
if ($value === $search) {
$value = $replace;
}
});
}
if (in_array($foreignKey->getForeignTableName(), $tableNames)) {
array_walk($foreignColumns, function (&$value) use($search, $replace) {
if ($value === $search) {
$value = $replace;
}
});
}
$identifierConstructorCallback = function ($columnName) {
return new Identifier($columnName);
};
$localColumns = array_map($identifierConstructorCallback, $localColumns);
$foreignColumns = array_map($identifierConstructorCallback, $foreignColumns);
$newForeignKey = clone $foreignKey;
ObjectAccess::setProperty($newForeignKey, '_localColumnNames', $localColumns, true);
ObjectAccess::setProperty($newForeignKey, '_foreignColumnNames', $foreignColumns, true);
$foreignKeyHandlingSql['drop'][] = $platform->getDropForeignKeySQL($foreignKey, $table);
$foreignKeyHandlingSql['add'][] = $platform->getCreateForeignKeySQL($newForeignKey, $table);
}
}
}
return $foreignKeyHandlingSql;
}
/** * @param Schema $schema * @return void */ public function down(Schema $schema) { $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); // collect foreign keys pointing to "our" tables $tableNames = array('typo3_party_domain_model_abstractparty', 'typo3_party_domain_model_electronicaddress', 'typo3_party_domain_model_person', 'typo3_party_domain_model_personname'); $foreignKeyHandlingSql = Service::getForeignKeyHandlingSql($schema, $this->platform, $tableNames, 'persistence_object_identifier', 'flow3_persistence_identifier'); // drop FK constraints foreach ($foreignKeyHandlingSql['drop'] as $sql) { $this->addSql($sql); } // rename identifier fields $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty DROP PRIMARY KEY"); $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL"); $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty ADD PRIMARY KEY (flow3_persistence_identifier)"); $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress DROP PRIMARY KEY"); $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL"); $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress ADD PRIMARY KEY (flow3_persistence_identifier)"); $this->addSql("ALTER TABLE typo3_party_domain_model_person DROP PRIMARY KEY"); $this->addSql("ALTER TABLE typo3_party_domain_model_person CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL"); $this->addSql("ALTER TABLE typo3_party_domain_model_person ADD PRIMARY KEY (flow3_persistence_identifier)"); $this->addSql("ALTER TABLE typo3_party_domain_model_personname DROP PRIMARY KEY"); $this->addSql("ALTER TABLE typo3_party_domain_model_personname CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL"); $this->addSql("ALTER TABLE typo3_party_domain_model_personname ADD PRIMARY KEY (flow3_persistence_identifier)"); // add back FK constraints foreach ($foreignKeyHandlingSql['add'] as $sql) { $this->addSql($sql); } }