44
55namespace Tests \Tempest \Integration \Database ;
66
7+ use Tempest \Database \MigratesDown ;
8+ use Tempest \Database \MigratesUp ;
9+ use Tempest \Database \Migrations \CreateMigrationsTable ;
710use Tempest \Database \Migrations \Migration ;
811use Tempest \Database \Migrations \MigrationManager ;
12+ use Tempest \Database \QueryStatement ;
13+ use Tempest \Database \QueryStatements \CreateTableStatement ;
14+ use Tempest \Database \QueryStatements \DropTableStatement ;
915use 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