77use Tempest \Container \Container ;
88use Tempest \Database \Config \DatabaseDialect ;
99use Tempest \Database \Database ;
10- use Tempest \Database \DatabaseMigration as MigrationInterface ;
11- use Tempest \Database \DatabaseMigration ;
1210use Tempest \Database \Exceptions \QueryWasInvalid ;
1311use Tempest \Database \HasLeadingStatements ;
1412use Tempest \Database \HasTrailingStatements ;
13+ use Tempest \Database \MigratesDown ;
14+ use Tempest \Database \MigratesUp ;
1515use Tempest \Database \OnDatabase ;
1616use Tempest \Database \Query ;
1717use Tempest \Database \QueryStatement ;
@@ -61,7 +61,7 @@ public function up(): void
6161 $ existingMigrations ,
6262 );
6363
64- foreach ($ this ->migrations as $ migration ) {
64+ foreach ($ this ->migrations -> up () as $ migration ) {
6565 if (in_array ($ migration ->name , $ existingMigrations , strict: true )) {
6666 continue ;
6767 }
@@ -92,7 +92,7 @@ public function down(): void
9292 $ existingMigrations ,
9393 );
9494
95- foreach ($ this ->migrations as $ migration ) {
95+ foreach ($ this ->migrations -> down () as $ migration ) {
9696 /* If the migration is not in the existing migrations, it means it has not been executed */
9797 if (! in_array ($ migration ->name , $ existingMigrations , strict: true )) {
9898 continue ;
@@ -143,7 +143,7 @@ public function rehashAll(): void
143143 */
144144 $ databaseMigration = array_find (
145145 iterator_to_array ($ this ->migrations ),
146- static fn (DatabaseMigration $ migration ) => $ migration ->name === $ existingMigration ->name ,
146+ static fn (MigratesUp | MigratesDown $ migration ) => $ migration ->name === $ existingMigration ->name ,
147147 );
148148
149149 if ($ databaseMigration === null ) {
@@ -169,7 +169,7 @@ public function validate(): void
169169 foreach ($ existingMigrations as $ existingMigration ) {
170170 $ databaseMigration = array_find (
171171 iterator_to_array ($ this ->migrations ),
172- static fn (DatabaseMigration $ migration ) => $ migration ->name === $ existingMigration ->name ,
172+ static fn (MigratesUp | MigratesDown $ migration ) => $ migration ->name === $ existingMigration ->name ,
173173 );
174174
175175 if ($ databaseMigration === null ) {
@@ -186,7 +186,7 @@ public function validate(): void
186186 }
187187 }
188188
189- public function executeUp (MigrationInterface $ migration ): void
189+ public function executeUp (MigratesUp $ migration ): void
190190 {
191191 if ($ migration instanceof ShouldMigrate && $ migration ->shouldMigrate ($ this ->database ) === false ) {
192192 return ;
@@ -239,7 +239,7 @@ public function executeUp(MigrationInterface $migration): void
239239 event (new MigrationMigrated ($ migration ->name ));
240240 }
241241
242- public function executeDown (MigrationInterface $ migration ): void
242+ public function executeDown (MigratesDown $ migration ): void
243243 {
244244 if ($ migration instanceof ShouldMigrate && $ migration ->shouldMigrate ($ this ->database ) === false ) {
245245 return ;
@@ -306,12 +306,19 @@ private function getTableDefinitions(): array
306306 );
307307 }
308308
309- private function getMigrationHash (DatabaseMigration $ migration ): string
309+ private function getMigrationHash (MigratesUp $ migration ): string
310310 {
311- $ minifiedDownSql = $ this ->getMinifiedSqlFromStatement ($ migration ->down ());
312- $ minifiedUpSql = $ this ->getMinifiedSqlFromStatement ($ migration ->up ());
311+ $ sql = '' ;
313312
314- return hash ('xxh128 ' , $ minifiedDownSql . $ minifiedUpSql );
313+ if ($ migration instanceof MigratesUp) {
314+ $ sql .= $ this ->getMinifiedSqlFromStatement ($ migration ->up ());
315+ }
316+
317+ if ($ migration instanceof MigratesDown) {
318+ $ sql .= $ this ->getMinifiedSqlFromStatement ($ migration ->down ());
319+ }
320+
321+ return hash ('xxh128 ' , $ sql );
315322 }
316323
317324 private function getMinifiedSqlFromStatement (?QueryStatement $ statement ): string
0 commit comments