Skip to content

Handle unified migration ledger in test migrator cleanup#1072

Merged
LordSimal merged 1 commit into5.xfrom
fix/testsuite-unified-ledger-reset
Apr 19, 2026
Merged

Handle unified migration ledger in test migrator cleanup#1072
LordSimal merged 1 commit into5.xfrom
fix/testsuite-unified-ledger-reset

Conversation

@dereuromark
Copy link
Copy Markdown
Member

Summary

Migrations\TestSuite\Migrator currently excludes cake_migrations from normal table drops, but only truncates tables matching *phinxlog* during cleanup.

In unified mode, that can leave the migration ledger behind while regular tables are reset, which makes later test bootstrap runs replay migrations against an inconsistent schema state.

This patch updates the test migrator to treat cake_migrations as a migration metadata table alongside legacy phinxlog tables.

Changes

  • replace the internal getPhinxTables() behavior with getMigrationTables()
  • include cake_migrations in the migration metadata table list
  • update comments/docblocks to reflect unified-mode terminology
  • add regression tests for:
    • unified mode: cake_migrations
    • legacy mode: *_phinxlog

Why

Unified mode is already first-class in the package, but the test cleanup path was still only recognizing legacy phinxlog tables as migration metadata. That can leave cake_migrations stale across test DB rebuilds.

Verification

  • vendor/bin/phpunit tests/TestCase/TestSuite/MigratorTest.php
  • vendor/bin/phpcs src/TestSuite/Migrator.php tests/TestCase/TestSuite/MigratorTest.php

@dereuromark
Copy link
Copy Markdown
Member Author

Current workaround...^^

 //   database at a time. Running multiple phpunit commands in parallel can
 //   race during migrations-table creation / schema rebuild and generate false
 //   failures.
-(new Migrator())->runMany([
+$migrationSets = [
     [],
     ['plugin' => 'Queue'],
     ['plugin' => 'DatabaseLog'],
     ['plugin' => 'Tools'],
-]);
+];
+
+// The upstream test migrator still only treats legacy `phinxlog` tables as
+// migration metadata when it decides to rebuild. In unified-mode setups like
+// this app, partial runs can leave real tables and the `cake_migrations` ledger
+// out of sync in either direction. Reusing that state makes bootstrap flaky.
+//
+// Reset the shared `db_test` schema unconditionally and replay migrations from
+// scratch. This is slower, but deterministic.
+$connection = ConnectionManager::get('test');
+$tables = $connection->getSchemaCollection()->listTables();
+$dropTables = array_values(array_filter($tables, static function (string $table): bool {
+    return $table !== 'cake_migrations' && !str_contains($table, 'phinxlog');
+}));
+
+if ($dropTables !== []) {
+    $connection->disableConstraints(function ($connection) use ($dropTables): void {
+        foreach ($dropTables as $table) {
+            $quotedTable = '`' . str_replace('`', '``', $table) . '`';
+            $connection->execute("DROP TABLE IF EXISTS {$quotedTable}");
+        }
+    });
+}
+if (in_array('cake_migrations', $tables, true)) {
+    ConnectionHelper::truncateTables('test', ['cake_migrations']);
+}
+
+(new Migrator())->runMany($migrationSets);
 
 // Swap rate limit cache buckets to an in-memory ArrayEngine so that
 // test runs never contaminate each other through file cache state.

@dereuromark dereuromark force-pushed the fix/testsuite-unified-ledger-reset branch from 9ca4404 to 93bbc28 Compare April 18, 2026 01:34
@dereuromark dereuromark marked this pull request as ready for review April 18, 2026 01:37
@LordSimal LordSimal merged commit d996bd9 into 5.x Apr 19, 2026
14 checks passed
@LordSimal LordSimal deleted the fix/testsuite-unified-ledger-reset branch April 19, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants