-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to fix allocations server_id foreign key (#542)
* add migration to fix allocations server_id foreign key * fix the fix...
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
database/migrations/2024_08_13_171337_fix_allocation_server_foreign_key.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
// Only needed for sqlite | ||
if (Schema::getConnection()->getDriverName() !== 'sqlite') { | ||
return; | ||
} | ||
|
||
// Disable foreign checks | ||
// legacy_alter_table needs to be 'ON' so existing foreign key table references aren't renamed when renaming the table, see https://www.sqlite.org/lang_altertable.html | ||
DB::statement('PRAGMA foreign_keys = OFF'); | ||
DB::statement('PRAGMA legacy_alter_table = ON'); | ||
|
||
DB::transaction(function () { | ||
DB::statement('ALTER TABLE allocations RENAME TO _allocations_old'); | ||
DB::statement('CREATE TABLE allocations | ||
("id" integer primary key autoincrement not null, | ||
"node_id" integer not null, | ||
"ip" varchar not null, | ||
"port" integer not null, | ||
"server_id" integer, | ||
"created_at" datetime, | ||
"updated_at" datetime, | ||
"ip_alias" text, | ||
"notes" varchar, | ||
foreign key("node_id") references "nodes"("id") on delete cascade, | ||
foreign key("server_id") references "servers"("id") on delete set null)'); | ||
DB::statement('INSERT INTO allocations SELECT * FROM _allocations_old'); | ||
DB::statement('DROP TABLE _allocations_old'); | ||
DB::statement('CREATE UNIQUE INDEX "allocations_node_id_ip_port_unique" on "allocations" ("node_id", "ip", "port")'); | ||
}); | ||
|
||
DB::statement('PRAGMA foreign_keys = ON'); | ||
DB::statement('PRAGMA legacy_alter_table = OFF'); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// Reverse not needed | ||
} | ||
}; |