migrations: add foreign keys for mount relations
This commit is contained in:
parent
b67aceb685
commit
3b72049d05
4 changed files with 176 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddForeignKeysToMountNodeTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
// Fix the columns having a different type than their relations.
|
||||||
|
Schema::table('mount_node', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('node_id')->change();
|
||||||
|
$table->unsignedInteger('mount_id')->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch an array of node and mount ids to check relations against.
|
||||||
|
$nodes = DB::table('nodes')->select('id')->pluck('id')->toArray();
|
||||||
|
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Drop any relations that are missing a node or mount.
|
||||||
|
DB::table('mount_node')
|
||||||
|
->select('node_id', 'mount_id')
|
||||||
|
->whereNotIn('node_id', $nodes)
|
||||||
|
->orWhereNotIn('mount_id', $mounts)
|
||||||
|
->delete();
|
||||||
|
|
||||||
|
Schema::table('mount_node', function (Blueprint $table) {
|
||||||
|
$table->foreign('node_id')
|
||||||
|
->references('id')
|
||||||
|
->on('nodes')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
$table->foreign('mount_id')->references('id')
|
||||||
|
->on('mounts')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('mount_node', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['node_id']);
|
||||||
|
$table->dropForeign(['mount_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddForeignKeysToMountServerTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
// Fix the columns having a different type than their relations.
|
||||||
|
Schema::table('mount_server', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('server_id')->change();
|
||||||
|
$table->unsignedInteger('mount_id')->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch an array of node and mount ids to check relations against.
|
||||||
|
$servers = DB::table('servers')->select('id')->pluck('id')->toArray();
|
||||||
|
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Drop any relations that are missing a server or mount.
|
||||||
|
DB::table('mount_server')
|
||||||
|
->select('server_id', 'mount_id')
|
||||||
|
->whereNotIn('server_id', $servers)
|
||||||
|
->orWhereNotIn('mount_id', $mounts)
|
||||||
|
->delete();
|
||||||
|
|
||||||
|
Schema::table('mount_server', function (Blueprint $table) {
|
||||||
|
$table->foreign('server_id')
|
||||||
|
->references('id')
|
||||||
|
->on('servers')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
$table->foreign('mount_id')->references('id')
|
||||||
|
->on('mounts')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('mount_server', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['server_id']);
|
||||||
|
$table->dropForeign(['mount_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddForeignKeysToEggMountTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
// Fix the columns having a different type than their relations.
|
||||||
|
Schema::table('egg_mount', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('egg_id')->change();
|
||||||
|
$table->unsignedInteger('mount_id')->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch an array of node and mount ids to check relations against.
|
||||||
|
$eggs = DB::table('eggs')->select('id')->pluck('id')->toArray();
|
||||||
|
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
|
||||||
|
|
||||||
|
// Drop any relations that are missing an egg or mount.
|
||||||
|
DB::table('egg_mount')
|
||||||
|
->select('egg_id', 'mount_id')
|
||||||
|
->whereNotIn('egg_id', $eggs)
|
||||||
|
->orWhereNotIn('mount_id', $mounts)
|
||||||
|
->delete();
|
||||||
|
|
||||||
|
Schema::table('egg_mount', function (Blueprint $table) {
|
||||||
|
$table->foreign('egg_id')
|
||||||
|
->references('id')
|
||||||
|
->on('eggs')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
$table->foreign('mount_id')->references('id')
|
||||||
|
->on('mounts')
|
||||||
|
->cascadeOnDelete()
|
||||||
|
->cascadeOnUpdate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('egg_mount', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['egg_id']);
|
||||||
|
$table->dropForeign(['mount_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue