Skip to content

Commit d996bd9

Browse files
authored
Handle unified migration ledger in test migrator (#1072)
1 parent 1fc7f8d commit d996bd9

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

src/TestSuite/Migrator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected function shouldDropTables(Migrations $migrations, array $options): boo
213213

214214
/**
215215
* Drops the regular tables of the provided connection
216-
* and truncates the phinx tables.
216+
* and truncates the migration metadata tables.
217217
*
218218
* @param string $connection Connection on which tables are dropped.
219219
* @param string[] $skip A fnmatch compatible list of tables to skip.
@@ -225,26 +225,26 @@ protected function dropTables(string $connection, array $skip = []): void
225225
if ($dropTables !== []) {
226226
$this->helper->dropTables($connection, $dropTables);
227227
}
228-
$phinxTables = $this->getPhinxTables($connection);
229-
if ($phinxTables !== []) {
230-
$this->helper->truncateTables($connection, $phinxTables);
228+
$migrationTables = $this->getMigrationTables($connection);
229+
if ($migrationTables !== []) {
230+
$this->helper->truncateTables($connection, $migrationTables);
231231
}
232232
}
233233

234234
/**
235-
* Get the list of tables that are phinxlog
235+
* Get the list of migration metadata tables.
236236
*
237237
* @param string $connection The connection name to operate on.
238-
* @return string[] The list of tables that are not related to phinx in the provided connection.
238+
* @return string[] The list of migration metadata tables in the provided connection.
239239
*/
240-
protected function getPhinxTables(string $connection): array
240+
protected function getMigrationTables(string $connection): array
241241
{
242242
$connection = ConnectionManager::get($connection);
243243
assert($connection instanceof Connection);
244244
$tables = $connection->getSchemaCollection()->listTables();
245245

246246
return array_filter($tables, function (string $table): bool {
247-
return str_contains($table, 'phinxlog');
247+
return str_contains($table, 'phinxlog') || $table === 'cake_migrations';
248248
});
249249
}
250250

tests/TestCase/TestSuite/MigratorTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Chronos\ChronosDate;
1717
use Cake\Core\Configure;
1818
use Cake\Database\Driver\Postgres;
19+
use Cake\Database\Schema\TableSchema;
1920
use Cake\Datasource\ConnectionManager;
2021
use Cake\TestSuite\ConnectionHelper;
2122
use Cake\TestSuite\TestCase;
@@ -55,6 +56,47 @@ protected function getMigratorWhereClause(): array
5556
: [];
5657
}
5758

59+
protected function makeInspectableMigrator(): Migrator
60+
{
61+
return new class () extends Migrator {
62+
/**
63+
* @return array<string>
64+
*/
65+
public function exposedGetMigrationTables(string $connection): array
66+
{
67+
return array_values($this->getMigrationTables($connection));
68+
}
69+
70+
/**
71+
* @return array<string>
72+
*/
73+
public function exposedGetNonPhinxTables(string $connection, array $skip = []): array
74+
{
75+
return array_values($this->getNonPhinxTables($connection, $skip));
76+
}
77+
};
78+
}
79+
80+
protected function createPortableTable(string $name, bool $withPlugin = false): void
81+
{
82+
$connection = ConnectionManager::get('test');
83+
$schema = new TableSchema($name);
84+
$schema
85+
->addColumn('version', ['type' => 'biginteger', 'null' => false])
86+
->addColumn('migration_name', ['type' => 'string', 'limit' => 100, 'null' => false])
87+
->addColumn('start_time', ['type' => 'timestamp', 'default' => null, 'null' => true])
88+
->addColumn('end_time', ['type' => 'timestamp', 'default' => null, 'null' => true])
89+
->addColumn('breakpoint', ['type' => 'boolean', 'default' => false, 'null' => false]);
90+
91+
if ($withPlugin) {
92+
$schema->addColumn('plugin', ['type' => 'string', 'limit' => 100, 'default' => null, 'null' => true]);
93+
}
94+
95+
foreach ($schema->createSql($connection) as $statement) {
96+
$connection->execute($statement);
97+
}
98+
}
99+
58100
protected function setUp(): void
59101
{
60102
parent::setUp();
@@ -95,6 +137,32 @@ public function testMigrateDropTruncate(): void
95137
$this->assertCount(0, $connection->selectQuery()->select(['*'])->from('migrator')->execute()->fetchAll());
96138
}
97139

140+
public function testGetMigrationTablesIncludesUnifiedLedger(): void
141+
{
142+
$this->skipIf(Configure::read('Migrations.legacyTables') !== false);
143+
144+
$connection = ConnectionManager::get('test');
145+
$connection->execute('CREATE TABLE sample_table (id INT)');
146+
$this->createPortableTable('cake_migrations', true);
147+
148+
$migrator = $this->makeInspectableMigrator();
149+
150+
$this->assertSame(['cake_migrations'], $migrator->exposedGetMigrationTables('test'));
151+
$this->assertSame(['sample_table'], $migrator->exposedGetNonPhinxTables('test'));
152+
}
153+
154+
public function testGetMigrationTablesIncludesLegacyLedger(): void
155+
{
156+
$connection = ConnectionManager::get('test');
157+
$connection->execute('CREATE TABLE sample_table (id INT)');
158+
$this->createPortableTable('app_phinxlog');
159+
160+
$migrator = $this->makeInspectableMigrator();
161+
162+
$this->assertSame(['app_phinxlog'], $migrator->exposedGetMigrationTables('test'));
163+
$this->assertSame(['sample_table'], $migrator->exposedGetNonPhinxTables('test'));
164+
}
165+
98166
public function testMigrateDropNoTruncate(): void
99167
{
100168
$migrator = new Migrator();

0 commit comments

Comments
 (0)