From bf153b2906af9a29ffc6cf38a04f93521a413a35 Mon Sep 17 00:00:00 2001 From: Thorsten Bus Date: Sun, 3 May 2026 22:20:07 +0200 Subject: [PATCH] feat(db): auto-migrate 4 legacy macro settings to new assignment system --- ...3_100700_migrate_legacy_macro_settings.php | 72 +++++++++++++++++++ .../MigrateLegacyMacroSettingsTest.php | 50 +++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 database/migrations/2026_05_03_100700_migrate_legacy_macro_settings.php create mode 100644 tests/Feature/Migrations/MigrateLegacyMacroSettingsTest.php diff --git a/database/migrations/2026_05_03_100700_migrate_legacy_macro_settings.php b/database/migrations/2026_05_03_100700_migrate_legacy_macro_settings.php new file mode 100644 index 0000000..557fa28 --- /dev/null +++ b/database/migrations/2026_05_03_100700_migrate_legacy_macro_settings.php @@ -0,0 +1,72 @@ +where('key', 'macro_name')->value('value'); + $uuid = DB::table('settings')->where('key', 'macro_uuid')->value('value'); + + if (empty($name) || empty($uuid)) { + return; + } + + $collectionName = DB::table('settings')->where('key', 'macro_collection_name')->value('value'); + $collectionUuid = DB::table('settings')->where('key', 'macro_collection_uuid')->value('value'); + + DB::transaction(function () use ($name, $uuid, $collectionName, $collectionUuid) { + $labelId = DB::table('labels')->insertGetId([ + 'name' => 'Copyright', + 'color' => null, + 'created_at' => now(), + 'updated_at' => now(), + ]); + + $macroId = DB::table('macros')->insertGetId([ + 'uuid' => strtoupper($uuid), + 'name' => $name, + 'last_imported_filename' => 'legacy-settings-migration', + 'last_imported_at' => now(), + 'created_at' => now(), + 'updated_at' => now(), + ]); + + if (! empty($collectionUuid) && ! empty($collectionName)) { + $collectionId = DB::table('macro_collections')->insertGetId([ + 'uuid' => strtoupper($collectionUuid), + 'name' => $collectionName, + 'last_imported_at' => now(), + 'created_at' => now(), + 'updated_at' => now(), + ]); + DB::table('macro_collection_macros')->insert([ + 'macro_collection_id' => $collectionId, + 'macro_id' => $macroId, + 'order' => 0, + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + DB::table('macro_assignments')->insert([ + 'part_type' => 'song', + 'macro_id' => $macroId, + 'position' => 'by_label', + 'label_id' => $labelId, + 'order' => 0, + 'created_at' => now(), + 'updated_at' => now(), + ]); + + DB::table('settings') + ->whereIn('key', ['macro_name', 'macro_uuid', 'macro_collection_name', 'macro_collection_uuid']) + ->delete(); + }); + } + + public function down(): void + { + } +}; diff --git a/tests/Feature/Migrations/MigrateLegacyMacroSettingsTest.php b/tests/Feature/Migrations/MigrateLegacyMacroSettingsTest.php new file mode 100644 index 0000000..2ad7a99 --- /dev/null +++ b/tests/Feature/Migrations/MigrateLegacyMacroSettingsTest.php @@ -0,0 +1,50 @@ +count())->toBe(0); + expect(DB::table('macros')->count())->toBe(0); +}); + +test('migration creates assignment when all 4 keys are present', function () { + DB::table('settings')->insert([ + ['key' => 'macro_name', 'value' => 'Copyright Makro', 'created_at' => now(), 'updated_at' => now()], + ['key' => 'macro_uuid', 'value' => 'AAAAAAAA-1111-2222-3333-FFFFFFFFFFFF', 'created_at' => now(), 'updated_at' => now()], + ['key' => 'macro_collection_name', 'value' => '--MAIN--', 'created_at' => now(), 'updated_at' => now()], + ['key' => 'macro_collection_uuid', 'value' => '8D02FC57-83F8-4042-9B90-81C229728426', 'created_at' => now(), 'updated_at' => now()], + ]); + + $migration = require database_path('migrations/2026_05_03_100700_migrate_legacy_macro_settings.php'); + $migration->up(); + + expect(DB::table('labels')->where('name', 'Copyright')->exists())->toBeTrue(); + expect(DB::table('macros')->where('uuid', 'AAAAAAAA-1111-2222-3333-FFFFFFFFFFFF')->exists())->toBeTrue(); + expect(DB::table('macro_assignments')->where('part_type', 'song')->where('position', 'by_label')->exists())->toBeTrue(); + expect(DB::table('settings')->whereIn('key', ['macro_name', 'macro_uuid', 'macro_collection_name', 'macro_collection_uuid'])->count())->toBe(0); +}); + +test('migration works when only name and uuid are set (no collection)', function () { + DB::table('settings')->insert([ + ['key' => 'macro_name', 'value' => 'Simple Makro', 'created_at' => now(), 'updated_at' => now()], + ['key' => 'macro_uuid', 'value' => 'BBBBBBBB-1111-2222-3333-FFFFFFFFFFFF', 'created_at' => now(), 'updated_at' => now()], + ]); + + $migration = require database_path('migrations/2026_05_03_100700_migrate_legacy_macro_settings.php'); + $migration->up(); + + expect(DB::table('macro_assignments')->where('part_type', 'song')->exists())->toBeTrue(); + expect(DB::table('macro_collections')->count())->toBe(0); +}); + +test('migration is no-op when macro_uuid is empty', function () { + DB::table('settings')->insert([ + ['key' => 'macro_name', 'value' => 'Makro Ohne UUID', 'created_at' => now(), 'updated_at' => now()], + ['key' => 'macro_uuid', 'value' => '', 'created_at' => now(), 'updated_at' => now()], + ]); + + $migration = require database_path('migrations/2026_05_03_100700_migrate_legacy_macro_settings.php'); + $migration->up(); + + expect(DB::table('macro_assignments')->count())->toBe(0); +});