From 493c5888a3c80b38231cf1c3a4f03f42de6d786b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 2 Oct 2017 22:03:01 -0500 Subject: [PATCH] Migration change --- ...ngeToABetterUniqueServiceConfiguration.php | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php b/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php index c8159826a..990a07a63 100644 --- a/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php +++ b/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php @@ -15,21 +15,22 @@ class ChangeToABetterUniqueServiceConfiguration extends Migration { Schema::table('service_options', function (Blueprint $table) { $table->char('uuid', 36)->after('id'); - $table->string('author')->after('service_id'); $table->index(['service_id', 'tag']); }); - DB::table('service_options')->select([ - 'service_options.id', - 'service_options.author', - 'service_options.uuid', - 'services.author AS service_author', - ])->join('services', 'services.id', '=', 'service_options.service_id')->get()->each(function ($option) { - DB::table('service_options')->where('id', $option->id)->update([ - 'author' => $option->service_author, - 'uuid' => Uuid::uuid4()->toString(), - ]); + DB::transaction(function () { + DB::table('service_options')->select([ + 'service_options.id', + 'service_options.uuid', + 'service_options.tag', + 'services.author AS service_author', + ])->join('services', 'services.id', '=', 'service_options.service_id')->get()->each(function ($option) { + DB::table('service_options')->where('id', $option->id)->update([ + 'tag' => $option->service_author . ':' . $option->tag, + 'uuid' => Uuid::uuid4()->toString(), + ]); + }); }); Schema::table('service_options', function (Blueprint $table) { @@ -44,8 +45,15 @@ class ChangeToABetterUniqueServiceConfiguration extends Migration { Schema::table('service_options', function (Blueprint $table) { $table->dropColumn('uuid'); - $table->dropColumn('author'); $table->dropIndex(['service_id', 'tag']); }); + + DB::transaction(function () { + DB::table('service_options')->select(['id', 'author'])->get()->each(function ($option) { + DB::table('service_options')->where('id', $option->id)->update([ + 'tag' => array_get(explode(':', $option->tag), 1), + ]); + }); + }); } }