new table name, in dependency order (the audited tables first, then the * ones whose foreign keys point at them — RENAME TABLE takes both in one statement when * needed, but one rename per line keeps half-applied states recoverable). */ private const TABLES = [ 'plans' => 'initiatives', 'plan_objectives' => 'initiative_objectives', 'plan_description_items' => 'initiative_description_items', 'plan_allocation_items' => 'initiative_allocation_items', 'plan_tasks' => 'initiative_tasks', 'plan_task_dependencies' => 'initiative_task_dependencies', 'plan_task_raci' => 'initiative_task_raci', 'plan_task_raci_stakeholders' => 'initiative_task_raci_stakeholders', 'plan_stakeholders' => 'initiative_stakeholders', 'plan_stakeholders_plan' => 'initiative_stakeholders_initiative', 'plan_audiences' => 'initiative_audiences', 'plan_audiences_plan' => 'initiative_audiences_initiative', 'plan_audiences_segments' => 'initiative_audiences_segments', 'plan_audiences_stages' => 'initiative_audiences_stages', ]; /** * Table (any of its names) => [old column => [new column, definition]]. */ private const COLUMNS = [ ['plan_objectives', 'initiative_objectives', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ['plan_description_items', 'initiative_description_items', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ['plan_allocation_items', 'initiative_allocation_items', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ['plan_tasks', 'initiative_tasks', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ['plan_stakeholders_plan', 'initiative_stakeholders_initiative', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ['plan_stakeholders_plan', 'initiative_stakeholders_initiative', 'role_in_plan', 'role_in_initiative', 'VARCHAR(191) DEFAULT NULL'], ['plan_audiences_plan', 'initiative_audiences_initiative', 'plan_id', 'initiative_id', 'INT UNSIGNED NOT NULL'], ]; protected function isApplicable(Schema $schema): bool { foreach (self::TABLES as $from => $to) { if ($schema->hasTable($this->concatPrefix($from))) { return true; } } foreach (self::COLUMNS as [$fromTable, $toTable, $fromColumn, $toColumn]) { $table = $this->getTableName($schema, [$fromTable, $toTable]); if (null !== $table) { foreach ($schema->getTable($table)->getColumns() as $name => $column) { if (strtolower($name) === strtolower($fromColumn)) { return true; } } } } return false; } protected function up(): void { // Tables, one rename at a time; stable database names unique. foreach (self::TABLES as $from => $to) { $this->renameTableIfExists($from, $to); } // Columns, resolving each table to whichever name it has now. foreach (self::COLUMNS as [$fromTable, $toTable, $fromColumn, $toColumn, $definition]) { $this->renameColumnIfExists([$fromTable, $toTable], $fromColumn, $toColumn, $definition); } } /** * Renames a table when the old name still exists and the new one doesn't. */ private function renameTableIfExists(string $from, string $to): void { $connection = $this->entityManager->getConnection(); if ($this->getTableName($connection->createSchemaManager()->introspectSchema(), [$from, $to]) === $this->concatPrefix($from)) { $connection->executeStatement(sprintf( 'RENAME TABLE %1$s TO %2$s', $this->concatPrefix($from), $this->concatPrefix($to) )); } } /** * @param list $tables the table's possible names, oldest first: the first that exists is used */ private function renameColumnIfExists(array $tables, string $fromColumn, string $toColumn, string $definition): void { $connection = $this->entityManager->getConnection(); $table = $this->getTableName($connection->createSchemaManager()->introspectSchema(), $tables); if (null === $table) { return; } foreach ($connection->createSchemaManager()->introspectTable($table)->getColumns() as $name => $column) { if (strtolower($name) === strtolower($fromColumn)) { $connection->executeStatement(sprintf( 'ALTER TABLE %s CHANGE %s %s %s', $table, $name, $toColumn, $definition )); return; } } } /** * The first of the given table names that exists, prefixed. * * @param list $tables */ private function getTableName(Schema $schema, array $tables): ?string { foreach ($tables as $table) { if ($schema->hasTable($this->concatPrefix($table))) { return $this->concatPrefix($table); } } return null; } }