referenced core table. */ private const FOREIGN_KEYS = [ 'owner_id' => 'users', 'category_id' => 'categories', ]; protected function isApplicable(Schema $schema): bool { $table = $this->getTable($schema); if (null === $table) { return false; } return [] !== $this->getMissingForeignKeys($table) || $this->statusNeedsResize($table); } protected function up(): void { $schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); $table = $this->getTable($schema); if (null === $table) { return; } if ($this->statusNeedsResize($table)) { $this->addSql(sprintf( 'ALTER TABLE %s MODIFY status VARCHAR(%d) NOT NULL', $this->concatPrefix(Plan::TABLE_NAME), self::STATUS_LENGTH )); } foreach ($this->getMissingForeignKeys($table) as $column => $referenceTable) { $this->addSql($this->generateAlterTableForeignKeyStatement( Plan::TABLE_NAME, [$column], $referenceTable, ['id'], 'ON DELETE SET NULL' )); } } private function getTable(Schema $schema): ?Table { $name = $this->concatPrefix(Plan::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; } private function statusNeedsResize(Table $table): bool { return $table->hasColumn('status') && self::STATUS_LENGTH !== $table->getColumn('status')->getLength(); } }