Migration change

This commit is contained in:
Dane Everitt 2017-10-02 22:03:01 -05:00
parent 220789a4b9
commit 493c5888a3
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -15,21 +15,22 @@ class ChangeToABetterUniqueServiceConfiguration extends Migration
{ {
Schema::table('service_options', function (Blueprint $table) { Schema::table('service_options', function (Blueprint $table) {
$table->char('uuid', 36)->after('id'); $table->char('uuid', 36)->after('id');
$table->string('author')->after('service_id');
$table->index(['service_id', 'tag']); $table->index(['service_id', 'tag']);
}); });
DB::table('service_options')->select([ DB::transaction(function () {
'service_options.id', DB::table('service_options')->select([
'service_options.author', 'service_options.id',
'service_options.uuid', 'service_options.uuid',
'services.author AS service_author', 'service_options.tag',
])->join('services', 'services.id', '=', 'service_options.service_id')->get()->each(function ($option) { 'services.author AS service_author',
DB::table('service_options')->where('id', $option->id)->update([ ])->join('services', 'services.id', '=', 'service_options.service_id')->get()->each(function ($option) {
'author' => $option->service_author, DB::table('service_options')->where('id', $option->id)->update([
'uuid' => Uuid::uuid4()->toString(), 'tag' => $option->service_author . ':' . $option->tag,
]); 'uuid' => Uuid::uuid4()->toString(),
]);
});
}); });
Schema::table('service_options', function (Blueprint $table) { Schema::table('service_options', function (Blueprint $table) {
@ -44,8 +45,15 @@ class ChangeToABetterUniqueServiceConfiguration extends Migration
{ {
Schema::table('service_options', function (Blueprint $table) { Schema::table('service_options', function (Blueprint $table) {
$table->dropColumn('uuid'); $table->dropColumn('uuid');
$table->dropColumn('author');
$table->dropIndex(['service_id', 'tag']); $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),
]);
});
});
} }
} }