-
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 foreign-key constraint to
siteinformation
table (#2106)
Closes #2105.
- Loading branch information
1 parent
cacc163
commit e1fbc14
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
database/migrations/2024_03_23_144609_siteinformation_fk.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,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!"; | ||
} | ||
} | ||
}; |