-
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 constraints to dynamic analysis tables
- Loading branch information
1 parent
125cfb3
commit d80ee07
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_09_23_133018_dynamic_analysis_foreign_keys.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 | ||
{ | ||
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']); | ||
}); | ||
} | ||
}; |