|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Database\Migrations\Migration; |
| 6 | +use Illuminate\Database\Schema\Blueprint; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | +use Illuminate\Support\Facades\Schema; |
| 9 | + |
| 10 | +return new class extends Migration |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Run the migrations. |
| 14 | + */ |
| 15 | + public function up(): void |
| 16 | + { |
| 17 | + // Example upgrade migration |
| 18 | + Schema::table('activity_log', function (Blueprint $table) { |
| 19 | + $table->json('attribute_changes')->nullable()->after('causer_id'); |
| 20 | + $table->dropColumn('batch_uuid'); |
| 21 | + }); |
| 22 | + |
| 23 | + // Then migrate existing data: move 'attributes' and 'old' from properties to attribute_changes |
| 24 | + DB::table('activity_log')->whereNotNull('properties')->eachById(function ($row) { |
| 25 | + $properties = json_decode($row->properties, true); |
| 26 | + $changes = array_intersect_key($properties, array_flip(['attributes', 'old'])); |
| 27 | + $remaining = array_diff_key($properties, array_flip(['attributes', 'old'])); |
| 28 | + |
| 29 | + DB::table('activity_log')->where('id', $row->id)->update([ |
| 30 | + 'attribute_changes' => empty($changes) ? null : json_encode($changes), |
| 31 | + 'properties' => empty($remaining) ? null : json_encode($remaining), |
| 32 | + ]); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Reverse the migrations. |
| 38 | + */ |
| 39 | + public function down(): void |
| 40 | + { |
| 41 | + // |
| 42 | + } |
| 43 | +}; |
0 commit comments