|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | + |
| 9 | +return new class extends Migration |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The morph aliases written by the metadata feature this migration enables. |
| 13 | + * |
| 14 | + * Hardcoded rather than read from the runtime morph map so this migration |
| 15 | + * stays stable as application code evolves. |
| 16 | + * |
| 17 | + * @var list<string> |
| 18 | + */ |
| 19 | + private array $morphAliases = [ |
| 20 | + 'component', |
| 21 | + 'component_group', |
| 22 | + 'incident', |
| 23 | + 'schedule', |
| 24 | + 'subscriber', |
| 25 | + ]; |
| 26 | + |
| 27 | + /** |
| 28 | + * Run the migrations. |
| 29 | + */ |
| 30 | + public function up(): void |
| 31 | + { |
| 32 | + Schema::table('meta', function (Blueprint $table) { |
| 33 | + $table->text('value')->nullable()->change(); |
| 34 | + }); |
| 35 | + |
| 36 | + DB::table('components') |
| 37 | + ->whereNotNull('meta') |
| 38 | + ->orderBy('id') |
| 39 | + ->each(function (object $component) { |
| 40 | + $meta = json_decode($component->meta, true); |
| 41 | + |
| 42 | + if (! is_array($meta)) { |
| 43 | + Log::warning('Skipping component meta that is not a key/value object during migration.', [ |
| 44 | + 'component_id' => $component->id, |
| 45 | + 'meta' => $component->meta, |
| 46 | + ]); |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $rows = collect($meta)->map(fn (mixed $value, int|string $key) => [ |
| 52 | + 'key' => (string) $key, |
| 53 | + 'value' => json_encode($value), |
| 54 | + 'meta_id' => $component->id, |
| 55 | + 'meta_type' => 'component', |
| 56 | + 'created_at' => now(), |
| 57 | + 'updated_at' => now(), |
| 58 | + ])->values()->all(); |
| 59 | + |
| 60 | + DB::table('meta')->insert($rows); |
| 61 | + }); |
| 62 | + |
| 63 | + Schema::table('components', function (Blueprint $table) { |
| 64 | + $table->dropColumn('meta'); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Reverse the migrations. |
| 70 | + */ |
| 71 | + public function down(): void |
| 72 | + { |
| 73 | + Schema::table('components', function (Blueprint $table) { |
| 74 | + $table->longText('meta')->nullable()->default(null)->after('enabled'); |
| 75 | + }); |
| 76 | + |
| 77 | + DB::table('meta') |
| 78 | + ->where('meta_type', 'component') |
| 79 | + ->orderBy('meta_id') |
| 80 | + ->get() |
| 81 | + ->groupBy('meta_id') |
| 82 | + ->each(function ($rows, $componentId) { |
| 83 | + $meta = $rows->mapWithKeys(fn (object $row) => [ |
| 84 | + $row->key => json_decode($row->value, true), |
| 85 | + ])->all(); |
| 86 | + |
| 87 | + DB::table('components') |
| 88 | + ->where('id', $componentId) |
| 89 | + ->update(['meta' => json_encode($meta)]); |
| 90 | + }); |
| 91 | + |
| 92 | + DB::table('meta')->whereIn('meta_type', $this->morphAliases)->delete(); |
| 93 | + |
| 94 | + Schema::table('meta', function (Blueprint $table) { |
| 95 | + $table->string('value')->nullable(false)->change(); |
| 96 | + }); |
| 97 | + } |
| 98 | +}; |
0 commit comments