new table name, in dependency order. */ private const TABLES = [ 'initiatives' => 'mt3s_mp_programs', 'initiative_objectives' => 'mt3s_mp_program_objectives', 'initiative_description_items' => 'mt3s_mp_program_description_items', 'initiative_allocation_items' => 'mt3s_mp_program_allocation_items', 'initiative_tasks' => 'mt3s_mp_program_tasks', 'initiative_task_dependencies' => 'mt3s_mp_program_task_dependencies', 'initiative_task_raci' => 'mt3s_mp_program_task_raci', 'initiative_task_raci_stakeholders' => 'mt3s_mp_program_task_raci_stakeholders', 'initiative_stakeholders' => 'mt3s_mp_program_stakeholders', 'initiative_stakeholders_initiative' => 'mt3s_mp_program_stakeholders_program', 'initiative_audiences' => 'mt3s_mp_program_audiences', 'initiative_audiences_initiative' => 'mt3s_mp_program_audiences_program', 'initiative_audiences_segments' => 'mt3s_mp_program_audiences_segments', 'initiative_audiences_stages' => 'mt3s_mp_program_audiences_stages', ]; /** * Table (any of its names) => [old column => new column, definition]. */ private const COLUMNS = [ ['initiative_objectives', 'mt3s_mp_program_objectives', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'], ['initiative_description_items', 'mt3s_mp_program_description_items', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'], ['initiative_allocation_items', 'mt3s_mp_program_allocation_items', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'], ['initiative_tasks', 'mt3s_mp_program_tasks', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'], ['initiative_stakeholders_initiative', 'mt3s_mp_program_stakeholders_program', 'initiative_id', 'program_id', 'INT UNSIGNED NOT NULL'], ['initiative_stakeholders_initiative', 'mt3s_mp_program_stakeholders_program', 'role_in_initiative', 'role_in_program', 'VARCHAR(191) DEFAULT NULL'], ['initiative_audiences_initiative', 'mt3s_mp_program_audiences_program', 'initiative_id', 'program_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]) { $table = $this->getTableName($schema, [$fromTable, $toTable]); if (null !== $table) { foreach ($schema->getTable($table)->getColumns() as $name => $column) { if (strtolower($name) === strtolower($fromColumn)) { return true; } } } } // The pre-rename plugins row: the update target of an installed 0.16.0. try { $row = $this->entityManager->getConnection()->fetchOne( sprintf("SELECT id FROM %s WHERE bundle = 'ProgramBundle'", $this->concatPrefix('plugins')) ); } catch (\Throwable) { $row = false; } return false !== $row; } 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); } $this->carryRowsOver(); } /** * The rows the rename orphans: the plugin's own, the roles' permissions, the categories and * the audit log. */ private function carryRowsOver(): void { $connection = $this->entityManager->getConnection(); // The plugins row: keep the install an update of the same plugin, not a new one. $oldBundle = $connection->fetchOne( sprintf("SELECT id FROM %s WHERE bundle = 'ProgramBundle'", $this->concatPrefix('plugins')) ); if (false !== $oldBundle) { $connection->executeStatement(sprintf( "UPDATE %s SET bundle = 'MT3SMarketingProgramBundle', name = 'MT3S Marketing Program' WHERE bundle = 'ProgramBundle'", $this->concatPrefix('plugins') )); } // The roles' permission rows: the permission base follows the bundle name, and // initiatives became programs (the 0.16.0 rename missed these). $connection->executeStatement(sprintf( "UPDATE %s SET name = 'programs' WHERE bundle = 'program' AND name = 'initiatives'", $this->concatPrefix('permissions') )); $connection->executeStatement(sprintf( "UPDATE %s SET bundle = 'mt3smarketingprogram' WHERE bundle = 'program'", $this->concatPrefix('permissions') )); // The categories of the program category type (bundle column), plan-era rows included: // the 0.16.0 rename left those behind too. $connection->executeStatement(sprintf( "UPDATE %s SET bundle = 'program' WHERE bundle IN ('plan', 'initiative')", $this->concatPrefix('categories') )); // The audit log rows about this plugin's entities. $connection->executeStatement(sprintf( "UPDATE %s SET bundle = 'MT3SMarketingProgramBundle' WHERE bundle IN ('PlanBundle', 'ProgramBundle')", $this->concatPrefix('audit_logs') )); } /** * 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; } }