Skip to content

Commit

Permalink
Add foreign-key constraint to siteinformation table (#2106)
Browse files Browse the repository at this point in the history
Closes #2105.
  • Loading branch information
williamjallen authored Mar 27, 2024
1 parent cacc163 commit e1fbc14
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions database/migrations/2024_03_23_144609_siteinformation_fk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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
{
if (Schema::hasTable('siteinformation')) {
echo "Adding siteid foreign key to siteinformation table...";
$num_deleted = DB::delete('DELETE FROM siteinformation WHERE siteid NOT IN (SELECT id FROM site)');
echo $num_deleted . ' invalid rows deleted' . PHP_EOL;
Schema::table('siteinformation', function (Blueprint $table) {
$table->foreign('siteid')->references('id')->on('site')->cascadeOnDelete();
});
} else {
echo "ERROR: siteinformation table does not exist!";
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
if (Schema::hasTable('siteinformation')) {
Schema::table('siteinformation', function (Blueprint $table) {
$table->dropForeign(['siteid']);
});
} else {
echo "ERROR: siteinformation table does not exist!";
}
}
};

0 comments on commit e1fbc14

Please sign in to comment.