From 7579c3c1b3605ffa2dd93840352000f896518a43 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Fri, 31 Jul 2026 10:56:25 +0300 Subject: [PATCH] test set order migration --- .../Feature/SetOrderingMigrationTest.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 backend/tests/Feature/SetOrderingMigrationTest.php diff --git a/backend/tests/Feature/SetOrderingMigrationTest.php b/backend/tests/Feature/SetOrderingMigrationTest.php new file mode 100644 index 0000000..806b892 --- /dev/null +++ b/backend/tests/Feature/SetOrderingMigrationTest.php @@ -0,0 +1,86 @@ +runMigrationAgainstLegacySets(); + + $this->assertSame( + [2, 7], + DB::table('sets')->orderBy('sort_order')->pluck('id')->all(), + ); + $this->assertSame( + [1, 2], + DB::table('sets') + ->orderBy('sort_order') + ->pluck('sort_order') + ->all(), + ); + + $this->expectException(QueryException::class); + DB::table('sets')->insert([ + 'id' => 9, + 'name' => 'Unordered Set', + 'description' => 'A set without an explicit position', + 'icon_image_url' => '/assets/unordered.png', + ]); + } + + public function testRollbackRemovesOrderWithoutDeletingSets(): void + { + $migration = $this->runMigrationAgainstLegacySets(); + + $migration->down(); + + $this->assertFalse(Schema::hasColumn('sets', 'sort_order')); + $this->assertSame( + [2, 7], + DB::table('sets')->orderBy('id')->pluck('id')->all(), + ); + } + + private function runMigrationAgainstLegacySets(): object + { + if (Schema::hasColumn('sets', 'sort_order')) { + Schema::table('sets', function (Blueprint $table): void { + $table->dropColumn('sort_order'); + }); + } + + DB::table('sets')->delete(); + DB::table('sets')->insert([ + [ + 'id' => 7, + 'name' => 'Later Set', + 'description' => 'Created later', + 'icon_image_url' => '/assets/later.png', + ], + [ + 'id' => 2, + 'name' => 'Earlier Set', + 'description' => 'Created earlier', + 'icon_image_url' => '/assets/earlier.png', + ], + ]); + + $migration = require database_path(self::MIGRATION_PATH); + $migration->up(); + + return $migration; + } +}