Skip to content

Commit

Permalink
Use more foreign keys for cascading deletions (#2577)
Browse files Browse the repository at this point in the history
Use foreign keys to automatically delete records from the following
tables:
* buildfailure2argument
* test2image
* updatefile
  • Loading branch information
zackgalbreath authored Nov 20, 2024
1 parent 5d8caad commit f88bd44
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 291 deletions.
13 changes: 0 additions & 13 deletions app/Utils/DatabaseCleanupUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ public static function removeBuild($buildid) : void
$db = Database::getInstance();
$buildid_prepare_array = $db->createPreparedArray(count($buildids));

// Remove the buildfailureargument
$buildfailureids = [];
$buildfailure = DB::select("SELECT id FROM buildfailure WHERE buildid IN $buildid_prepare_array", $buildids);
foreach ($buildfailure as $buildfailure_array) {
$buildfailureids[] = intval($buildfailure_array->id);
}
if (count($buildfailureids) > 0) {
$buildfailure_prepare_array = $db->createPreparedArray(count($buildfailureids));
DB::delete("DELETE FROM buildfailure2argument WHERE buildfailureid IN $buildfailure_prepare_array", $buildfailureids);
}

// Delete buildfailuredetails that are only used by builds that are being
// deleted.
DB::delete("
Expand Down Expand Up @@ -242,7 +231,6 @@ public static function removeBuild($buildid) : void
if (count($updateids) > 0) {
$updateids_prepare_array = $db->createPreparedArray(count($updateids));
DB::delete("DELETE FROM buildupdate WHERE id IN $updateids_prepare_array", $updateids);
DB::delete("DELETE FROM updatefile WHERE updateid IN $updateids_prepare_array", $updateids);
}

// Delete tests and testoutputs that are not shared.
Expand Down Expand Up @@ -303,7 +291,6 @@ public static function removeBuild($buildid) : void
$imgids_prepare_array = $db->createPreparedArray(count($imgids));
DB::delete("DELETE FROM image WHERE id IN $imgids_prepare_array", $imgids);
}
self::deleteRowsChunked('DELETE FROM test2image WHERE outputid IN ', $testoutputs_to_delete);
}
}

Expand Down
5 changes: 1 addition & 4 deletions app/cdash/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,8 @@ set_tests_properties(/Feature/GraphQL/CoverageTypeTest PROPERTIES DEPENDS /Featu
add_laravel_test(/Feature/PurgeUnusedProjectsCommand)
set_tests_properties(/Feature/PurgeUnusedProjectsCommand PROPERTIES DEPENDS "/Feature/GraphQL/TestTypeTest;/Feature/GraphQL/TestMeasurementTypeTest;/Feature/GraphQL/NoteTypeTest;/Feature/GraphQL/BuildMeasurementTypeTest;/Feature/GraphQL/CoverageTypeTest")

add_laravel_test(/Feature/TestSchemaMigration)
set_tests_properties(/Feature/TestSchemaMigration PROPERTIES DEPENDS /Feature/PurgeUnusedProjectsCommand)

add_laravel_test(/Feature/MeasurementPositionMigration)
set_tests_properties(/Feature/MeasurementPositionMigration PROPERTIES DEPENDS /Feature/TestSchemaMigration)
set_tests_properties(/Feature/MeasurementPositionMigration PROPERTIES DEPENDS /Feature/PurgeUnusedProjectsCommand)

add_laravel_test(/Feature/RemoveMeasurementCheckboxesMigration)
set_tests_properties(/Feature/RemoveMeasurementCheckboxesMigration PROPERTIES DEPENDS /Feature/MeasurementPositionMigration)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 buildfailure2argument(buildfailureid)->buildfailure(id)...";
$num_deleted = DB::delete("DELETE FROM buildfailure2argument WHERE buildfailureid NOT IN (SELECT id FROM buildfailure)");
echo $num_deleted . ' invalid rows deleted' . PHP_EOL;
Schema::table('buildfailure2argument', function (Blueprint $table) {
$table->foreign('buildfailureid')->references('id')->on('buildfailure')->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('buildfailure2argument', function (Blueprint $table) {
$table->dropForeign(['buildfailureid']);
});
}
};
31 changes: 31 additions & 0 deletions database/migrations/2024_11_20_144307_updatefile_foreign_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 updatefile(updateid)->buildupdate(id)...";
$num_deleted = DB::delete("DELETE FROM updatefile WHERE updateid NOT IN (SELECT id FROM buildupdate)");
echo $num_deleted . ' invalid rows deleted' . PHP_EOL;
Schema::table('updatefile', function (Blueprint $table) {
$table->foreign('updateid')->references('id')->on('buildupdate')->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('updatefile', function (Blueprint $table) {
$table->dropForeign(['updateid']);
});
}
};
31 changes: 31 additions & 0 deletions database/migrations/2024_11_20_144308_test2image_foreign_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 test2image(outputid)->testoutput(id)...";
$num_deleted = DB::delete("DELETE FROM test2image WHERE outputid NOT IN (SELECT id FROM testoutput)");
echo $num_deleted . ' invalid rows deleted' . PHP_EOL;
Schema::table('test2image', function (Blueprint $table) {
$table->foreign('outputid')->references('id')->on('testoutput')->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('test2image', function (Blueprint $table) {
$table->dropForeign(['outputid']);
});
}
};
Loading

0 comments on commit f88bd44

Please sign in to comment.