Skip to content

Commit 0819414

Browse files
committed
fix(database): delete rolled back migration records
1 parent c699ab8 commit 0819414

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

packages/database/src/Migrations/MigrationManager.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,9 @@ public function executeDown(MigratesDown $migration): void
267267

268268
try {
269269
$this->database->execute(
270-
new Query(
271-
'DELETE FROM Migration WHERE name = :name',
272-
['name' => $migration->name],
273-
),
270+
query(Migration::class)
271+
->delete()
272+
->whereRaw('name = ?', $migration->name),
274273
);
275274
} catch (QueryWasInvalid) { // @mago-expect lint:no-empty-catch-clause
276275
/**

tests/Integration/Database/MigrationManagerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44

55
namespace Tests\Tempest\Integration\Database;
66

7+
use Tempest\Database\MigratesDown;
8+
use Tempest\Database\MigratesUp;
9+
use Tempest\Database\Migrations\CreateMigrationsTable;
710
use Tempest\Database\Migrations\Migration;
811
use Tempest\Database\Migrations\MigrationManager;
12+
use Tempest\Database\QueryStatement;
13+
use Tempest\Database\QueryStatements\CreateTableStatement;
14+
use Tempest\Database\QueryStatements\DropTableStatement;
915
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1016

17+
use function Tempest\Database\query;
18+
1119
/**
1220
* @internal
1321
*/
@@ -29,4 +37,42 @@ public function test_migration(): void
2937
$this->assertNotEmpty($migrations);
3038
$this->assertSame($oldCount, count($migrations));
3139
}
40+
41+
public function test_execute_down_removes_rolled_back_migration_record(): void
42+
{
43+
$migrationManager = $this->container->get(MigrationManager::class);
44+
$migration = new MigrationManagerTestRollbackMigration();
45+
46+
$migrationManager->executeUp(new CreateMigrationsTable());
47+
$migrationManager->executeUp($migration);
48+
49+
$this->assertSame(1, $this->countMigrationRecords($migration));
50+
51+
$migrationManager->executeDown($migration);
52+
53+
$this->assertSame(0, $this->countMigrationRecords($migration));
54+
}
55+
56+
private function countMigrationRecords(MigrationManagerTestRollbackMigration $migration): int
57+
{
58+
return query(Migration::class)
59+
->count()
60+
->whereRaw('name = ?', $migration->name)
61+
->execute();
62+
}
63+
}
64+
65+
final class MigrationManagerTestRollbackMigration implements MigratesUp, MigratesDown
66+
{
67+
public string $name = '0000-00-00_create_migration_manager_test_table';
68+
69+
public function up(): QueryStatement
70+
{
71+
return new CreateTableStatement('migration_manager_test_table')->primary();
72+
}
73+
74+
public function down(): QueryStatement
75+
{
76+
return new DropTableStatement('migration_manager_test_table');
77+
}
3278
}

0 commit comments

Comments
 (0)