From dee5060a2499f17f65643d5c9fd68d6a8612233d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Tue, 11 Oct 2016 16:16:16 +0200 Subject: [PATCH] FEATURE: add unique code to widgets --- ...16_10_11_135756_AddUniqueCodeToWidgets.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 database/migrations/2016_10_11_135756_AddUniqueCodeToWidgets.php diff --git a/database/migrations/2016_10_11_135756_AddUniqueCodeToWidgets.php b/database/migrations/2016_10_11_135756_AddUniqueCodeToWidgets.php new file mode 100644 index 00000000..36141a15 --- /dev/null +++ b/database/migrations/2016_10_11_135756_AddUniqueCodeToWidgets.php @@ -0,0 +1,66 @@ +string('container_id') + ->nullable(true) + ->default(null) + ->after('name'); + $table->string('container_class') + ->nullable(true) + ->after('name') + ->default(null); + + $table->unique(['language', 'container_id']); + }); + + // migrate data + $widgets = \App\Models\Widget::all(); + foreach ($widgets as $widget) { + $widget->settings = json_decode($widget->settings); + if (isset($widget->settings->containerClass)) { + $widget->container_class = $widget->settings->containerClass; + unset($widget->settings->containerClass); + $widget->settings = json_encode($widget->settings); + $widget->save(); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('widgets', function (Blueprint $table) { + $table->dropUnique('widgets_language_container_id_unique'); + }); + + // migrate data + $widgets = \App\Models\Widget::all(); + foreach ($widgets as $widget) { + $widget->settings = (object)json_decode($widget->settings); + $widget->settings->containerClass = $widget->container_class; + $widget->settings = json_encode($widget->settings); + $widget->save(); + } + + Schema::table('widgets', function (Blueprint $table) { + $table->dropColumn('container_id'); + $table->dropColumn('container_class'); + }); + } +}