Handle unified migration ledger in test migrator cleanup#1072
Merged
Conversation
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. |
9ca4404 to
93bbc28
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Migrations\TestSuite\Migratorcurrently excludescake_migrationsfrom 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_migrationsas a migration metadata table alongside legacyphinxlogtables.Changes
getPhinxTables()behavior withgetMigrationTables()cake_migrationsin the migration metadata table listcake_migrations*_phinxlogWhy
Unified mode is already first-class in the package, but the test cleanup path was still only recognizing legacy
phinxlogtables as migration metadata. That can leavecake_migrationsstale across test DB rebuilds.Verification
vendor/bin/phpunit tests/TestCase/TestSuite/MigratorTest.phpvendor/bin/phpcs src/TestSuite/Migrator.php tests/TestCase/TestSuite/MigratorTest.php