feat(db): create macros, macro_collections, and junction tables

This commit is contained in:
Thorsten Bus 2026-05-03 22:13:28 +02:00
parent 860db0405f
commit 09ab4821fc
2 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
public function up(): void
{
Schema::create('macros', function (Blueprint $table) {
$table->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');
}
};

View file

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
test('macros table has expected columns', function () {
expect(Schema::hasTable('macros'))->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);
});