hasTable($this->concatPrefix(UtmTaxonomy::TABLE_NAME)) || !$this->hasTaxonomyColumn($schema) || [] !== $this->getMissingForeignKeys($schema); } protected function up(): void { $schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); if (!$schema->hasTable($this->concatPrefix(UtmTaxonomy::TABLE_NAME))) { $table = $this->concatPrefix(UtmTaxonomy::TABLE_NAME); $this->addSql(<<hasTaxonomyColumn($schema)) { $this->addSql(sprintf( 'ALTER TABLE %s ADD utm_taxonomy_id INT UNSIGNED DEFAULT NULL', $this->concatPrefix(SmartLink::TABLE_NAME) )); } foreach ($this->getMissingForeignKeys($schema) as $foreignKey) { $this->addSql($this->generateAlterTableForeignKeyStatement( SmartLink::TABLE_NAME, ['utm_taxonomy_id'], UtmTaxonomy::TABLE_NAME, ['id'], 'ON DELETE SET NULL' )); } } private function hasTaxonomyColumn(Schema $schema): bool { $links = $this->concatPrefix(SmartLink::TABLE_NAME); return $schema->hasTable($links) && $schema->getTable($links)->hasColumn('utm_taxonomy_id'); } /** * @return list */ private function getMissingForeignKeys(Schema $schema): array { $links = $this->concatPrefix(SmartLink::TABLE_NAME); $missing = ['utm_taxonomy_id']; if ($schema->hasTable($links)) { foreach ($schema->getTable($links)->getForeignKeys() as $foreignKey) { $missing = array_diff($missing, $foreignKey->getLocalColumns()); } } return array_values($missing); } }