|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | + |
| 8 | +return new class extends Migration |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Run the migrations. |
| 12 | + */ |
| 13 | + public function up(): void |
| 14 | + { |
| 15 | + // Get all fields with config containing optionType |
| 16 | + $fields = DB::table('fields') |
| 17 | + ->whereNotNull('config') |
| 18 | + ->where('config', 'like', '%optionType%') |
| 19 | + ->get(); |
| 20 | + |
| 21 | + foreach ($fields as $field) { |
| 22 | + $config = json_decode($field->config, true); |
| 23 | + |
| 24 | + if (isset($config['optionType']) && is_string($config['optionType'])) { |
| 25 | + // Convert string to array format |
| 26 | + $config['optionType'] = [$config['optionType']]; |
| 27 | + |
| 28 | + // Update the field with the corrected config |
| 29 | + DB::table('fields') |
| 30 | + ->where('ulid', $field->ulid) |
| 31 | + ->update(['config' => json_encode($config)]); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Reverse the migrations. |
| 38 | + */ |
| 39 | + public function down(): void |
| 40 | + { |
| 41 | + // Get all fields with config containing optionType arrays |
| 42 | + $fields = DB::table('fields') |
| 43 | + ->whereNotNull('config') |
| 44 | + ->where('config', 'like', '%optionType%') |
| 45 | + ->get(); |
| 46 | + |
| 47 | + foreach ($fields as $field) { |
| 48 | + $config = json_decode($field->config, true); |
| 49 | + |
| 50 | + if (isset($config['optionType']) && is_array($config['optionType']) && count($config['optionType']) === 1) { |
| 51 | + // Convert array back to string format |
| 52 | + $config['optionType'] = $config['optionType'][0]; |
| 53 | + |
| 54 | + // Update the field with the reverted config |
| 55 | + DB::table('fields') |
| 56 | + ->where('ulid', $field->ulid) |
| 57 | + ->update(['config' => json_encode($config)]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +}; |
0 commit comments