migrations: add foreign keys for mount relations (#3574)

This commit is contained in:
Matthew Penner 2021-08-24 12:12:35 -07:00 committed by GitHub
parent bd271e2e62
commit 7330a747b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 176 additions and 0 deletions

View file

@ -1,5 +1,6 @@
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

View file

@ -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']);
});
}
}

View file

@ -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']);
});
}
}

View file

@ -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']);
});
}
}