Skip to content

Commit 13ce251

Browse files
committed
Add foreign keys to all necessary tables 👏
I thoroughly hate myself for doing MEDIUMINT(8) on so many tables.
1 parent 55c9f0f commit 13ce251

13 files changed

+522
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignKeysServers extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
DB::statement('ALTER TABLE servers
17+
MODIFY COLUMN node INT(10) UNSIGNED NOT NULL,
18+
MODIFY COLUMN owner INT(10) UNSIGNED NOT NULL,
19+
MODIFY COLUMN allocation INT(10) UNSIGNED NOT NULL,
20+
MODIFY COLUMN service INT(10) UNSIGNED NOT NULL,
21+
MODIFY COLUMN servers.option INT(10) UNSIGNED NOT NULL
22+
');
23+
24+
Schema::table('servers', function (Blueprint $table) {
25+
$table->foreign('node')->references('id')->on('nodes');
26+
$table->foreign('owner')->references('id')->on('users');
27+
$table->foreign('allocation')->references('id')->on('allocations');
28+
$table->foreign('service')->references('id')->on('services');
29+
$table->foreign('option')->references('id')->on('service_options');
30+
$table->softDeletes();
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::table('servers', function (Blueprint $table) {
42+
$table->dropForeign('servers_node_foreign');
43+
$table->dropForeign('servers_owner_foreign');
44+
$table->dropForeign('servers_allocation_foreign');
45+
$table->dropForeign('servers_service_foreign');
46+
$table->dropForeign('servers_option_foreign');
47+
48+
$table->dropIndex('servers_node_foreign');
49+
$table->dropIndex('servers_owner_foreign');
50+
$table->dropIndex('servers_allocation_foreign');
51+
$table->dropIndex('servers_service_foreign');
52+
$table->dropIndex('servers_option_foreign');
53+
54+
$table->dropColumn('deleted_at');
55+
});
56+
57+
DB::statement('ALTER TABLE servers
58+
MODIFY COLUMN node MEDIUMINT(8) UNSIGNED NOT NULL,
59+
MODIFY COLUMN owner MEDIUMINT(8) UNSIGNED NOT NULL,
60+
MODIFY COLUMN allocation MEDIUMINT(8) UNSIGNED NOT NULL,
61+
MODIFY COLUMN service MEDIUMINT(8) UNSIGNED NOT NULL,
62+
MODIFY COLUMN servers.option MEDIUMINT(8) UNSIGNED NOT NULL
63+
');
64+
}
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignAllocations extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
DB::statement('ALTER TABLE allocations
17+
MODIFY COLUMN assigned_to INT(10) UNSIGNED NULL,
18+
MODIFY COLUMN node INT(10) UNSIGNED NOT NULL
19+
');
20+
21+
Schema::table('allocations', function (Blueprint $table) {
22+
$table->foreign('assigned_to')->references('id')->on('servers');
23+
$table->foreign('node')->references('id')->on('nodes');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::table('allocations', function (Blueprint $table) {
35+
$table->dropForeign('allocations_assigned_to_foreign');
36+
$table->dropForeign('allocations_node_foreign');
37+
38+
$table->dropIndex('allocations_assigned_to_foreign');
39+
$table->dropIndex('allocations_node_foreign');
40+
});
41+
42+
DB::statement('ALTER TABLE allocations
43+
MODIFY COLUMN assigned_to MEDIUMINT(8) UNSIGNED NULL,
44+
MODIFY COLUMN node MEDIUMINT(8) UNSIGNED NOT NULL
45+
');
46+
}
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignApiKeys extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('api_keys', function (Blueprint $table) {
17+
$table->foreign('user')->references('id')->on('users');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('api_keys', function (Blueprint $table) {
29+
$table->dropForeign('api_keys_user_foreign');
30+
$table->dropIndex('api_keys_user_foreign');
31+
});
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignApiPermissions extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
DB::statement('ALTER TABLE api_permissions MODIFY key_id INT(10) UNSIGNED NOT NULL');
17+
18+
Schema::table('api_permissions', function (Blueprint $table) {
19+
$table->foreign('key_id')->references('id')->on('api_keys');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('api_permissions', function (Blueprint $table) {
31+
$table->dropForeign('api_permissions_key_id_foreign');
32+
$table->dropIndex('api_permissions_key_id_foreign');
33+
});
34+
35+
DB::statement('ALTER TABLE api_permissions MODIFY key_id MEDIUMINT(8) UNSIGNED NOT NULL');
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignDatabaseServers extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('database_servers', function (Blueprint $table) {
17+
$table->foreign('linked_node')->references('id')->on('nodes');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('database_servers', function (Blueprint $table) {
29+
$table->dropForeign('database_servers_linked_node_foreign');
30+
$table->dropIndex('database_servers_linked_node_foreign');
31+
});
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignDatabases extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('databases', function (Blueprint $table) {
17+
$table->foreign('server_id')->references('id')->on('servers');
18+
$table->foreign('db_server')->references('id')->on('database_servers');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('databases', function (Blueprint $table) {
30+
$table->dropForeign('databases_server_id_foreign');
31+
$table->dropForeign('databases_db_server_foreign');
32+
33+
$table->dropIndex('databases_server_id_foreign');
34+
$table->dropIndex('databases_db_server_foreign');
35+
});
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignNodes extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
DB::statement('ALTER TABLE nodes MODIFY location INT(10) UNSIGNED NOT NULL');
17+
18+
Schema::table('nodes', function (Blueprint $table) {
19+
$table->foreign('location')->references('id')->on('locations');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('nodes', function (Blueprint $table) {
31+
$table->dropForeign('nodes_location_foreign');
32+
$table->dropIndex('nodes_location_foreign');
33+
});
34+
35+
DB::statement('ALTER TABLE nodes MODIFY location MEDIUMINT(10) UNSIGNED NOT NULL');
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignPermissions extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('permissions', function (Blueprint $table) {
17+
$table->foreign('user_id')->references('id')->on('users');
18+
$table->foreign('server_id')->references('id')->on('servers');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('permissions', function (Blueprint $table) {
30+
$table->dropForeign('permissions_user_id_foreign');
31+
$table->dropForeign('permissions_server_id_foreign');
32+
33+
$table->dropIndex('permissions_user_id_foreign');
34+
$table->dropIndex('permissions_server_id_foreign');
35+
});
36+
}
37+
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddForeignServerVariables extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
DB::statement('ALTER TABLE server_variables
17+
MODIFY COLUMN server_id INT(10) UNSIGNED NULL,
18+
MODIFY COLUMN variable_id INT(10) UNSIGNED NOT NULL
19+
');
20+
21+
Schema::table('server_variables', function (Blueprint $table) {
22+
$table->foreign('server_id')->references('id')->on('servers');
23+
$table->foreign('variable_id')->references('id')->on('service_variables');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::table('server_variables', function (Blueprint $table) {
35+
$table->dropForeign('server_variables_server_id_foreign');
36+
$table->dropForeign('server_variables_variable_id_foreign');
37+
38+
$table->dropIndex('server_variables_server_id_foreign');
39+
$table->dropIndex('server_variables_variable_id_foreign');
40+
});
41+
42+
DB::statement('ALTER TABLE allocations
43+
MODIFY COLUMN server_id MEDIUMINT(8) UNSIGNED NULL,
44+
MODIFY COLUMN variable_id MEDIUMINT(8) UNSIGNED NOT NULL
45+
');
46+
}
47+
48+
}

0 commit comments

Comments
 (0)