Skip to content

Commit 7330a74

Browse files
authored
migrations: add foreign keys for mount relations (pterodactyl#3574)
1 parent bd271e2 commit 7330a74

4 files changed

+176
-0
lines changed

database/migrations/2021_08_03_210600_change_successful_field_to_default_to_false_on_backups_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Illuminate\Support\Facades\Schema;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Database\Migrations\Migration;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\DB;
4+
use Illuminate\Support\Facades\Schema;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Database\Migrations\Migration;
7+
8+
class AddForeignKeysToMountNodeTable extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
// Fix the columns having a different type than their relations.
18+
Schema::table('mount_node', function (Blueprint $table) {
19+
$table->unsignedInteger('node_id')->change();
20+
$table->unsignedInteger('mount_id')->change();
21+
});
22+
23+
// Fetch an array of node and mount ids to check relations against.
24+
$nodes = DB::table('nodes')->select('id')->pluck('id')->toArray();
25+
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
26+
27+
// Drop any relations that are missing a node or mount.
28+
DB::table('mount_node')
29+
->select('node_id', 'mount_id')
30+
->whereNotIn('node_id', $nodes)
31+
->orWhereNotIn('mount_id', $mounts)
32+
->delete();
33+
34+
Schema::table('mount_node', function (Blueprint $table) {
35+
$table->foreign('node_id')
36+
->references('id')
37+
->on('nodes')
38+
->cascadeOnDelete()
39+
->cascadeOnUpdate();
40+
$table->foreign('mount_id')->references('id')
41+
->on('mounts')
42+
->cascadeOnDelete()
43+
->cascadeOnUpdate();
44+
});
45+
}
46+
47+
/**
48+
* Reverse the migrations.
49+
*
50+
* @return void
51+
*/
52+
public function down()
53+
{
54+
Schema::table('mount_node', function (Blueprint $table) {
55+
$table->dropForeign(['node_id']);
56+
$table->dropForeign(['mount_id']);
57+
});
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignKeysToMountServerTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
// Fix the columns having a different type than their relations.
17+
Schema::table('mount_server', function (Blueprint $table) {
18+
$table->unsignedInteger('server_id')->change();
19+
$table->unsignedInteger('mount_id')->change();
20+
});
21+
22+
// Fetch an array of node and mount ids to check relations against.
23+
$servers = DB::table('servers')->select('id')->pluck('id')->toArray();
24+
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
25+
26+
// Drop any relations that are missing a server or mount.
27+
DB::table('mount_server')
28+
->select('server_id', 'mount_id')
29+
->whereNotIn('server_id', $servers)
30+
->orWhereNotIn('mount_id', $mounts)
31+
->delete();
32+
33+
Schema::table('mount_server', function (Blueprint $table) {
34+
$table->foreign('server_id')
35+
->references('id')
36+
->on('servers')
37+
->cascadeOnDelete()
38+
->cascadeOnUpdate();
39+
$table->foreign('mount_id')->references('id')
40+
->on('mounts')
41+
->cascadeOnDelete()
42+
->cascadeOnUpdate();
43+
});
44+
}
45+
46+
/**
47+
* Reverse the migrations.
48+
*
49+
* @return void
50+
*/
51+
public function down()
52+
{
53+
Schema::table('mount_server', function (Blueprint $table) {
54+
$table->dropForeign(['server_id']);
55+
$table->dropForeign(['mount_id']);
56+
});
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignKeysToEggMountTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
// Fix the columns having a different type than their relations.
17+
Schema::table('egg_mount', function (Blueprint $table) {
18+
$table->unsignedInteger('egg_id')->change();
19+
$table->unsignedInteger('mount_id')->change();
20+
});
21+
22+
// Fetch an array of node and mount ids to check relations against.
23+
$eggs = DB::table('eggs')->select('id')->pluck('id')->toArray();
24+
$mounts = DB::table('mounts')->select('id')->pluck('id')->toArray();
25+
26+
// Drop any relations that are missing an egg or mount.
27+
DB::table('egg_mount')
28+
->select('egg_id', 'mount_id')
29+
->whereNotIn('egg_id', $eggs)
30+
->orWhereNotIn('mount_id', $mounts)
31+
->delete();
32+
33+
Schema::table('egg_mount', function (Blueprint $table) {
34+
$table->foreign('egg_id')
35+
->references('id')
36+
->on('eggs')
37+
->cascadeOnDelete()
38+
->cascadeOnUpdate();
39+
$table->foreign('mount_id')->references('id')
40+
->on('mounts')
41+
->cascadeOnDelete()
42+
->cascadeOnUpdate();
43+
});
44+
}
45+
46+
/**
47+
* Reverse the migrations.
48+
*
49+
* @return void
50+
*/
51+
public function down()
52+
{
53+
Schema::table('egg_mount', function (Blueprint $table) {
54+
$table->dropForeign(['egg_id']);
55+
$table->dropForeign(['mount_id']);
56+
});
57+
}
58+
}

0 commit comments

Comments
 (0)