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 1dddb7f3..d47b0e5d 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 00000000..9c5a403b --- /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 00000000..7bf99506 --- /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']); + }); + } +}