referenced core table. */ private const FOREIGN_KEYS = [ 'page_id' => 'pages', 'form_id' => 'forms', ]; protected function isApplicable(Schema $schema): bool { $table = $this->getTable($schema); if (null === $table) { return false; } foreach (array_keys(self::FOREIGN_KEYS) as $column) { if (!$table->hasColumn($column)) { return true; } } return [] !== $this->getMissingForeignKeys($table); } protected function up(): void { $schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); $table = $this->getTable($schema); if (null === $table) { return; } foreach (array_keys(self::FOREIGN_KEYS) as $column) { if (!$table->hasColumn($column)) { $this->addSql(sprintf( 'ALTER TABLE %s ADD %s INT UNSIGNED DEFAULT NULL', $this->concatPrefix(SmartLink::TABLE_NAME), $column )); } } foreach ($this->getMissingForeignKeys($table) as $column => $referenceTable) { $this->addSql($this->generateAlterTableForeignKeyStatement( SmartLink::TABLE_NAME, [$column], $referenceTable, ['id'], 'ON DELETE SET NULL' )); } } private function getTable(Schema $schema): ?Table { $name = $this->concatPrefix(SmartLink::TABLE_NAME); return $schema->hasTable($name) ? $schema->getTable($name) : null; } /** * @return array */ private function getMissingForeignKeys(Table $table): array { $missing = self::FOREIGN_KEYS; foreach ($table->getForeignKeys() as $foreignKey) { foreach ($foreignKey->getLocalColumns() as $column) { unset($missing[$column]); } } return $missing; } }