From 09ab4821fc92ba7689cfd538c73ebe54905999e9 Mon Sep 17 00:00:00 2001 From: Thorsten Bus Date: Sun, 3 May 2026 22:13:28 +0200 Subject: [PATCH] feat(db): create macros, macro_collections, and junction tables --- ...2026_05_03_100200_create_macros_tables.php | 49 +++++++++++++++++++ tests/Feature/Migrations/MacrosTablesTest.php | 39 +++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 database/migrations/2026_05_03_100200_create_macros_tables.php create mode 100644 tests/Feature/Migrations/MacrosTablesTest.php diff --git a/database/migrations/2026_05_03_100200_create_macros_tables.php b/database/migrations/2026_05_03_100200_create_macros_tables.php new file mode 100644 index 0000000..b77a4ab --- /dev/null +++ b/database/migrations/2026_05_03_100200_create_macros_tables.php @@ -0,0 +1,49 @@ +id(); + $table->string('uuid', 36)->unique(); + $table->string('name'); + $table->string('color', 7)->nullable(); + $table->boolean('trigger_on_startup')->default(false); + $table->unsignedSmallInteger('image_type')->default(0); + $table->unsignedInteger('action_count')->default(0); + $table->timestamp('hidden_at')->nullable(); + $table->timestamp('last_imported_at')->nullable(); + $table->string('last_imported_filename')->nullable(); + $table->timestamps(); + $table->index('hidden_at'); + }); + + Schema::create('macro_collections', function (Blueprint $table) { + $table->id(); + $table->string('uuid', 36)->unique(); + $table->string('name'); + $table->timestamp('last_imported_at')->nullable(); + $table->timestamps(); + }); + + Schema::create('macro_collection_macros', function (Blueprint $table) { + $table->id(); + $table->foreignId('macro_collection_id')->constrained()->cascadeOnDelete(); + $table->foreignId('macro_id')->constrained('macros')->cascadeOnDelete(); + $table->unsignedInteger('order')->default(0); + $table->timestamps(); + $table->unique(['macro_collection_id', 'macro_id']); + }); + } + + public function down(): void + { + Schema::dropIfExists('macro_collection_macros'); + Schema::dropIfExists('macro_collections'); + Schema::dropIfExists('macros'); + } +}; diff --git a/tests/Feature/Migrations/MacrosTablesTest.php b/tests/Feature/Migrations/MacrosTablesTest.php new file mode 100644 index 0000000..5be4f45 --- /dev/null +++ b/tests/Feature/Migrations/MacrosTablesTest.php @@ -0,0 +1,39 @@ +toBeTrue(); + expect(Schema::hasColumns('macros', [ + 'id', 'uuid', 'name', 'color', 'trigger_on_startup', 'image_type', + 'action_count', 'hidden_at', 'last_imported_at', 'last_imported_filename', + 'created_at', 'updated_at', + ]))->toBeTrue(); +}); + +test('macro_collections table has expected columns', function () { + expect(Schema::hasTable('macro_collections'))->toBeTrue(); + expect(Schema::hasColumns('macro_collections', ['id', 'uuid', 'name', 'last_imported_at', 'created_at', 'updated_at']))->toBeTrue(); +}); + +test('macro_collection_macros junction has expected columns', function () { + expect(Schema::hasTable('macro_collection_macros'))->toBeTrue(); + expect(Schema::hasColumns('macro_collection_macros', ['id', 'macro_collection_id', 'macro_id', 'order', 'created_at', 'updated_at']))->toBeTrue(); +}); + +test('deleting a collection cascades to junction rows', function () { + $collId = DB::table('macro_collections')->insertGetId(['uuid' => 'COLL-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test', 'created_at' => now(), 'updated_at' => now()]); + $macroId = DB::table('macros')->insertGetId(['uuid' => 'AAAA-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test', 'created_at' => now(), 'updated_at' => now()]); + DB::table('macro_collection_macros')->insert(['macro_collection_id' => $collId, 'macro_id' => $macroId, 'order' => 0, 'created_at' => now(), 'updated_at' => now()]); + + DB::table('macro_collections')->where('id', $collId)->delete(); + + expect(DB::table('macro_collection_macros')->where('macro_collection_id', $collId)->count())->toBe(0); +}); + +test('macros uuid is unique', function () { + DB::table('macros')->insert(['uuid' => 'SAME-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'First', 'created_at' => now(), 'updated_at' => now()]); + expect(fn () => DB::table('macros')->insert(['uuid' => 'SAME-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Second', 'created_at' => now(), 'updated_at' => now()])) + ->toThrow(\Exception::class); +});