|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Schema\Blueprint; |
| 4 | +use Illuminate\Support\Facades\Schema; |
| 5 | +use Statamic\Eloquent\Database\BaseMigration as Migration; |
| 6 | +use Statamic\Eloquent\Globals\GlobalSetModel; |
| 7 | +use Statamic\Eloquent\Globals\VariablesModel; |
| 8 | + |
| 9 | +return new class extends Migration { |
| 10 | + public function up() |
| 11 | + { |
| 12 | + Schema::table($this->prefix('global_sets'), function (Blueprint $table) { |
| 13 | + $table->jsonb('settings')->after('title')->nullable(); |
| 14 | + }); |
| 15 | + |
| 16 | + GlobalSetModel::all() |
| 17 | + ->each(function ($model) { |
| 18 | + $localizations = json_decode($model->localizations); |
| 19 | + |
| 20 | + foreach ($localizations as $localization) { |
| 21 | + VariablesModel::create([ |
| 22 | + 'handle' => $model->handle, |
| 23 | + 'locale' => $localization->locale, |
| 24 | + 'data' => $localization->data, |
| 25 | + 'origin' => $localization->origin, |
| 26 | + ]); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + Schema::table($this->prefix('global_sets'), function (Blueprint $table) { |
| 31 | + $table->dropColumn('localizations'); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + public function down() |
| 36 | + { |
| 37 | + Schema::table($this->prefix('global_sets'), function (Blueprint $table) { |
| 38 | + $table->dropColumn('settings'); |
| 39 | + $table->jsonb('localizations')->after('title')->nullable(); |
| 40 | + }); |
| 41 | + |
| 42 | + GlobalSetModel::all() |
| 43 | + ->each(function ($model) { |
| 44 | + $model->localizations = VariablesModel::where('handle', $model->handle) |
| 45 | + ->get() |
| 46 | + ->mapWithKeys(function ($variable, $key) { |
| 47 | + return [ |
| 48 | + $variable->locale = [ |
| 49 | + 'locale' => $variable->locale, |
| 50 | + 'data' => $variable->data, |
| 51 | + 'origin' => $variable->origin, |
| 52 | + ], |
| 53 | + ]; |
| 54 | + }); |
| 55 | + |
| 56 | + $model->save(); |
| 57 | + }); |
| 58 | + } |
| 59 | +}; |
0 commit comments