-
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.
Use more foreign keys for cascading deletions (#2577)
Use foreign keys to automatically delete records from the following tables: * buildfailure2argument * test2image * updatefile
- Loading branch information
1 parent
5d8caad
commit f88bd44
Showing
6 changed files
with
94 additions
and
291 deletions.
There are no files selected for viewing
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
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
31 changes: 31 additions & 0 deletions
31
database/migrations/2024_11_20_144306_buildfailure2argument_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,31 @@ | ||
<?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 foreign key constraint buildfailure2argument(buildfailureid)->buildfailure(id)..."; | ||
$num_deleted = DB::delete("DELETE FROM buildfailure2argument WHERE buildfailureid NOT IN (SELECT id FROM buildfailure)"); | ||
echo $num_deleted . ' invalid rows deleted' . PHP_EOL; | ||
Schema::table('buildfailure2argument', function (Blueprint $table) { | ||
$table->foreign('buildfailureid')->references('id')->on('buildfailure')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('buildfailure2argument', function (Blueprint $table) { | ||
$table->dropForeign(['buildfailureid']); | ||
}); | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
database/migrations/2024_11_20_144307_updatefile_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,31 @@ | ||
<?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 foreign key constraint updatefile(updateid)->buildupdate(id)..."; | ||
$num_deleted = DB::delete("DELETE FROM updatefile WHERE updateid NOT IN (SELECT id FROM buildupdate)"); | ||
echo $num_deleted . ' invalid rows deleted' . PHP_EOL; | ||
Schema::table('updatefile', function (Blueprint $table) { | ||
$table->foreign('updateid')->references('id')->on('buildupdate')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('updatefile', function (Blueprint $table) { | ||
$table->dropForeign(['updateid']); | ||
}); | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
database/migrations/2024_11_20_144308_test2image_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,31 @@ | ||
<?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 foreign key constraint test2image(outputid)->testoutput(id)..."; | ||
$num_deleted = DB::delete("DELETE FROM test2image WHERE outputid NOT IN (SELECT id FROM testoutput)"); | ||
echo $num_deleted . ' invalid rows deleted' . PHP_EOL; | ||
Schema::table('test2image', function (Blueprint $table) { | ||
$table->foreign('outputid')->references('id')->on('testoutput')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('test2image', function (Blueprint $table) { | ||
$table->dropForeign(['outputid']); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.