Move video over to software_id
This commit is contained in:
98
database/migrations/2019_12_26_125230_softwareid_videos.php
Normal file
98
database/migrations/2019_12_26_125230_softwareid_videos.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use App\Models\{Software,Video,Make,Model};
|
||||
|
||||
class SoftwareidVideos extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('videos', function (Blueprint $table) {
|
||||
$table->bigInteger('software_id')->unsigned()->nullable();
|
||||
$table->foreign('software_id')->references('id')->on('software');
|
||||
});
|
||||
|
||||
$po = Video::select(['make','model','software'])
|
||||
->groupBy(['make','model','software']);
|
||||
|
||||
foreach ($po->get() as $o) {
|
||||
if (! $o->make AND ! $o->model AND ! $o->software)
|
||||
continue;
|
||||
|
||||
$ma = NULL;
|
||||
|
||||
dump(['make'=>$o->make,'model'=>$o->model,'software'=>$o->software]);
|
||||
if ($o->make)
|
||||
$ma = Make::firstOrCreate(['name'=>$o->make]);
|
||||
|
||||
$mo = Model::firstOrCreate([
|
||||
'name'=>$o->model ?: NULL,
|
||||
'make_id'=>$ma->id,
|
||||
]);
|
||||
|
||||
$so = Software::firstOrCreate([
|
||||
'name'=>$o->software ?: NULL,
|
||||
'model_id'=>$mo->id,
|
||||
]);
|
||||
|
||||
foreach (Video::where('make',$o->make)
|
||||
->where('model',$o->model)
|
||||
->where('software',$o->software)
|
||||
->get() as $p) {
|
||||
|
||||
$p->software_id = $so->id;
|
||||
$p->make = $p->model = $p->software = NULL;
|
||||
$p->save();
|
||||
}
|
||||
}
|
||||
|
||||
Schema::table('videos', function (Blueprint $table) {
|
||||
$table->dropColumn('make');
|
||||
$table->dropColumn('model');
|
||||
$table->dropColumn('software');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
/*
|
||||
Schema::table('videos', function (Blueprint $table) {
|
||||
$table->string('make',32)->nullable();
|
||||
$table->string('model',128)->nullable();
|
||||
$table->string('software')->nullable();
|
||||
});
|
||||
*/
|
||||
|
||||
foreach (Video::whereNotNULL('software_id')->get() as $p)
|
||||
{
|
||||
$s = $p->software()->first();
|
||||
$p->software = $s->name;
|
||||
|
||||
if ($s->model)
|
||||
$p->model = $s->model->name;
|
||||
|
||||
if ($s->model_id AND $s->model->make_id)
|
||||
$p->make = $s->model->make->name;
|
||||
|
||||
$p->software_id = NULL;
|
||||
$p->save();
|
||||
}
|
||||
|
||||
Schema::table('videos', function (Blueprint $table) {
|
||||
$table->dropForeign(['software_id']);
|
||||
$table->dropColumn('software_id');
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user