-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subproject2subproject foreign-key constraints (#2521)
This continues our ongoing effort described in #2093 to add foreign-key constraints wherever possible for better data integrity.
- Loading branch information
1 parent
872d47a
commit f179288
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
database/migrations/2024_10_29_154630_subproject2subproject_foreign_keys.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,42 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
echo "Adding subprojectid foreign key to subproject2subproject table..."; | ||
$num_deleted = DB::delete('DELETE FROM subproject2subproject WHERE subprojectid NOT IN (SELECT id FROM subproject)'); | ||
echo $num_deleted . ' invalid rows deleted' . PHP_EOL; | ||
|
||
echo "Adding dependsonid foreign key to subproject2subproject table..."; | ||
$num_deleted = DB::delete('DELETE FROM subproject2subproject WHERE dependsonid NOT IN (SELECT id FROM subproject)'); | ||
echo $num_deleted . ' invalid rows deleted' . PHP_EOL; | ||
|
||
Schema::table('subproject2subproject', function (Blueprint $table) { | ||
$table->bigInteger('subprojectid')->change(); | ||
$table->bigInteger('dependsonid')->change(); | ||
$table->foreign('subprojectid')->references('id')->on('subproject')->cascadeOnDelete(); | ||
$table->foreign('dependsonid')->references('id')->on('subproject')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('subproject2subproject', function (Blueprint $table) { | ||
$table->integer('subprojectid')->change(); | ||
$table->integer('dependsonid')->change(); | ||
$table->dropForeign(['subprojectid']); | ||
$table->dropForeign(['dependsonid']); | ||
}); | ||
} | ||
}; |