entityManager->getConnection()->createSchemaManager()->introspectSchema(); $plans = $this->concatPrefix(Initiative::TABLE_NAME); $items = $this->concatPrefix(InitiativeDescriptionItem::TABLE_NAME); if (!$schema->hasTable($items)) { $this->addSql($this->getCreateTableSql()); } if (!$schema->hasTable($items) || !$this->hasPlanForeignKey($schema)) { $this->addSql($this->generateAlterTableForeignKeyStatement( InitiativeDescriptionItem::TABLE_NAME, ['plan_id'], Initiative::TABLE_NAME, ['id'], 'ON DELETE CASCADE' )); } if ($schema->getTable($plans)->hasColumn('description')) { $this->addSql("ALTER TABLE {$plans} DROP COLUMN description"); } } private function hasPlanForeignKey(Schema $schema): bool { $items = $this->concatPrefix(InitiativeDescriptionItem::TABLE_NAME); if (!$schema->hasTable($items)) { return false; } foreach ($schema->getTable($items)->getForeignKeys() as $foreignKey) { if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) { return true; } } return false; } private function getCreateTableSql(): string { $schemaTool = new SchemaTool($this->entityManager); $prefix = 'CREATE TABLE '.$this->concatPrefix(InitiativeDescriptionItem::TABLE_NAME).' '; foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(InitiativeDescriptionItem::class)]) as $sql) { if (str_starts_with($sql, $prefix)) { return $sql; } } throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.InitiativeDescriptionItem::TABLE_NAME.'.'); } }