From 3b72049d05a5103e76c46cf22dc4056aa3fa8b94 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 21 Aug 2021 12:15:39 -0600 Subject: [PATCH] migrations: add foreign keys for mount relations --- ...d_to_default_to_false_on_backups_table.php | 1 + ...1_add_foreign_keys_to_mount_node_table.php | 59 +++++++++++++++++++ ...add_foreign_keys_to_mount_server_table.php | 58 ++++++++++++++++++ ...21_add_foreign_keys_to_egg_mount_table.php | 58 ++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 database/migrations/2021_08_21_175111_add_foreign_keys_to_mount_node_table.php create mode 100644 database/migrations/2021_08_21_175118_add_foreign_keys_to_mount_server_table.php create mode 100644 database/migrations/2021_08_21_180921_add_foreign_keys_to_egg_mount_table.php diff --git a/database/migrations/2021_08_03_210600_change_successful_field_to_default_to_false_on_backups_table.php b/database/migrations/2021_08_03_210600_change_successful_field_to_default_to_false_on_backups_table.php index 1dddb7f3b..d47b0e5d2 100644 --- a/database/migrations/2021_08_03_210600_change_successful_field_to_default_to_false_on_backups_table.php +++ b/database/migrations/2021_08_03_210600_change_successful_field_to_default_to_false_on_backups_table.php @@ -1,5 +1,6 @@ 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']); + }); + } +} diff --git a/database/migrations/2021_08_21_175118_add_foreign_keys_to_mount_server_table.php b/database/migrations/2021_08_21_175118_add_foreign_keys_to_mount_server_table.php new file mode 100644 index 000000000..9c5a403b2 --- /dev/null +++ b/database/migrations/2021_08_21_175118_add_foreign_keys_to_mount_server_table.php @@ -0,0 +1,58 @@ +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']); + }); + } +} diff --git a/database/migrations/2021_08_21_180921_add_foreign_keys_to_egg_mount_table.php b/database/migrations/2021_08_21_180921_add_foreign_keys_to_egg_mount_table.php new file mode 100644 index 000000000..7bf99506b --- /dev/null +++ b/database/migrations/2021_08_21_180921_add_foreign_keys_to_egg_mount_table.php @@ -0,0 +1,58 @@ +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']); + }); + } +}