Skip to content

Commit

Permalink
Add foreign key constraints to dynamic analysis tables
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjallen committed Sep 23, 2024
1 parent 125cfb3 commit d80ee07
Showing 1 changed file with 39 additions and 0 deletions.
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
{
echo "Adding foreign key constraint dynamicanalysisdefect(dynamicanalysisid)->dynamicanalysis(id)...";
$num_deleted = DB::delete("DELETE FROM dynamicanalysisdefect WHERE dynamicanalysisid NOT IN (SELECT id FROM dynamicanalysis)");
echo $num_deleted . ' invalid rows deleted' . PHP_EOL;
Schema::table('dynamicanalysisdefect', function (Blueprint $table) {
$table->foreign('dynamicanalysisid')->references('id')->on('dynamicanalysis')->cascadeOnDelete();
$table->index(['value']);
$table->index(['type']);
$table->unique(['dynamicanalysisid', 'value', 'type']);
$table->unique(['dynamicanalysisid', 'type', 'value']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('dynamicanalysisdefect', function (Blueprint $table) {
$table->dropForeign(['dynamicanalysisid']);
$table->dropIndex(['value']);
$table->dropIndex(['type']);
$table->dropUnique(['dynamicanalysisid', 'value', 'type']);
$table->dropUnique(['dynamicanalysisid', 'type', 'value']);
});
}
};

0 comments on commit d80ee07

Please sign in to comment.