FEATURE: add unique code to widgets
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
|
||||||
|
class AddUniqueCodeToWidgets extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('widgets', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user