Smartlinks) also renames its tables and their * indexes: utm_smart_links -> smart_links and utm_smart_link_hits -> smart_link_hits. * * Mautic syncs the plugin schema before running the migrations, so the new tables usually * already exist (empty, straight from the entity metadata) when this runs: the rows are copied * from the old tables, which are then dropped (hits first: they still reference the old links * table). When the new tables don't exist yet (a plugin update without the schema sync) the old * tables are renamed instead, which keeps the rows, the foreign keys and the indexes. * * Installs updated from 0.3.0 skipped the Mautic destinations of Version_0_4_0, which looks for * the table under its new name: the page_id / form_id columns and their foreign keys are * re-checked here so every install converges to the same schema. * * Idempotent: runs on plugin update (core's migration engine) and right after a fresh install * (PluginInstallSubscriber), and stops when nothing is left to rename, copy or add. */ class Version_0_5_0_SmartLinks extends AbstractMigration { private const RENAMED_TABLES = [ 'utm_smart_links' => 'smart_links', 'utm_smart_link_hits' => 'smart_link_hits', ]; private const RENAMED_INDEXES = [ 'utm_smart_link_slug' => 'smart_link_slug', 'utm_smart_link_hit_link' => 'smart_link_hit_link', ]; /** Local column => referenced core table (the Mautic destinations of Version_0_4_0). */ private const MAUTIC_DESTINATIONS = [ 'page_id' => 'pages', 'form_id' => 'forms', ]; protected function isApplicable(Schema $schema): bool { foreach (array_keys(self::RENAMED_TABLES) as $old) { if ($schema->hasTable($this->concatPrefix($old))) { return true; } } $links = $this->findLinksTable($schema); if (null !== $links) { foreach (array_keys(self::MAUTIC_DESTINATIONS) as $column) { if (!$links->hasColumn($column)) { return true; } } if ([] !== $this->getOldIndexes($links) || [] !== $this->getMissingForeignKeys($links)) { return true; } } $hits = $this->findHitsTable($schema); return null !== $hits && [] !== $this->getOldIndexes($hits); } protected function up(): void { $schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); $emptied = []; foreach (self::RENAMED_TABLES as $old => $new) { $oldTable = $this->findTable($schema, $old); $newTable = $this->findTable($schema, $new); if (null === $oldTable) { continue; // nothing left under the old name } if (null === $newTable) { // No schema sync ran: rename keeps the rows, the foreign keys and the indexes. $this->addSql(sprintf('RENAME TABLE %s TO %s', $this->concatPrefix($old), $this->concatPrefix($new))); continue; } // The schema sync already created the new table (empty): copy the rows over, then // drop the old table. Hits reference links, so everything is copied before the // first DROP (see below). $columns = implode(', ', array_intersect( array_keys($newTable->getColumns()), array_keys($oldTable->getColumns()) )); $this->addSql(sprintf( 'INSERT INTO %s (%s) SELECT %s FROM %s', $this->concatPrefix($new), $columns, $columns, $this->concatPrefix($old) )); $emptied[] = $old; } // Drop the copied-from tables, hits first: their foreign key still references the old links table. foreach (array_reverse($emptied) as $old) { $this->addSql(sprintf('DROP TABLE %s', $this->concatPrefix($old))); } $links = $this->findLinksTable($schema); $hits = $this->findHitsTable($schema); if (null === $links) { return; } // The statements below run after the RENAME TABLE ones, so they target the new names. foreach ([[SmartLink::TABLE_NAME, $links], [SmartLinkHit::TABLE_NAME, $hits]] as [$name, $table]) { if (null === $table) { continue; } foreach ($this->getOldIndexes($table) as $old) { $this->addSql(sprintf( 'ALTER TABLE %s DROP INDEX %s, ADD INDEX %s (%s)', $this->concatPrefix($name), $old, self::RENAMED_INDEXES[$old], implode(', ', $table->getIndex($old)->getColumns()) )); } } foreach (array_keys(self::MAUTIC_DESTINATIONS) as $column) { if (!$links->hasColumn($column)) { $this->addSql(sprintf( 'ALTER TABLE %s ADD %s INT UNSIGNED DEFAULT NULL', $this->concatPrefix(SmartLink::TABLE_NAME), $column )); } } foreach ($this->getMissingForeignKeys($links) as $column => $referenceTable) { $this->addSql($this->generateAlterTableForeignKeyStatement( SmartLink::TABLE_NAME, [$column], $referenceTable, ['id'], 'ON DELETE SET NULL' )); } } /** * The links table under the name it has right now: new on fresh installs, old on installs * that wait for the RENAME TABLE statements queued above. */ private function findLinksTable(Schema $schema): ?Table { return $this->findTable($schema, SmartLink::TABLE_NAME) ?? $this->findTable($schema, 'utm_smart_links'); } private function findHitsTable(Schema $schema): ?Table { return $this->findTable($schema, SmartLinkHit::TABLE_NAME) ?? $this->findTable($schema, 'utm_smart_link_hits'); } private function findTable(Schema $schema, string $name): ?Table { $name = $this->concatPrefix($name); return $schema->hasTable($name) ? $schema->getTable($name) : null; } /** * @return list Indexes still carrying their pre-rename name */ private function getOldIndexes(Table $table): array { return array_values(array_filter( array_keys(self::RENAMED_INDEXES), fn (string $index): bool => $table->hasIndex($index) )); } /** * @return array */ private function getMissingForeignKeys(Table $table): array { $missing = self::MAUTIC_DESTINATIONS; foreach ($table->getForeignKeys() as $foreignKey) { foreach ($foreignKey->getLocalColumns() as $column) { unset($missing[$column]); } } return $missing; } }