Skip to content

Commit

Permalink
Add subproject2subproject foreign-key constraints (#2521)
Browse files Browse the repository at this point in the history
This continues our ongoing effort described in
#2093 to add foreign-key
constraints wherever possible for better data integrity.
  • Loading branch information
williamjallen authored Nov 4, 2024
1 parent 872d47a commit f179288
Showing 1 changed file with 42 additions and 0 deletions.
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']);
});
}
};

0 comments on commit f179288

Please sign in to comment.