Skip to content

Commit

Permalink
Move "Cleanup Database" functionality to new artisan command
Browse files Browse the repository at this point in the history
  • Loading branch information
zackgalbreath committed Nov 21, 2024
1 parent ca4e645 commit 31579f1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 41 deletions.
89 changes: 89 additions & 0 deletions app/Console/Commands/DeepCleanDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DeepCleanDatabase extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'db:deep-clean';

/**
* The console command description.
*/
protected $description = 'Prune unused records from the CDash database';

/**
* Execute the console command.
*/
public function handle(): void
{
$num_deleted = DB::delete("DELETE FROM banner WHERE projectid != 0 AND
projectid NOT IN (SELECT id FROM project)");
echo "{$num_deleted} rows deleted from `banner`\n";

self::delete_unused_rows('dailyupdate', 'projectid', 'project');

self::delete_unused_rows('buildfailuredetails', 'id', 'buildfailure', 'detailsid');
self::delete_unused_rows('configure', 'id', 'build2configure', 'configureid');
self::delete_unused_rows('coveragefile', 'id', 'coverage', 'fileid');
self::delete_unused_rows('dailyupdatefile', 'dailyupdateid', 'dailyupdate');
self::delete_unused_rows('note', 'id', 'build2note', 'noteid');
self::delete_unused_rows('testoutput', 'id', 'build2test', 'outputid');
self::delete_unused_rows('uploadfile', 'id', 'build2uploadfile', 'fileid');

$num_deleted = DB::delete("DELETE FROM image WHERE
id NOT IN (SELECT imageid FROM project) AND
id NOT IN (SELECT imgid FROM test2image)");
echo "{$num_deleted} rows deleted from `image`\n";
}

/** Delete unused rows in batches */
private static function delete_unused_rows(string $table, string $field, string $targettable, string $selectfield = 'id'): void
{
$start = DB::table($table)->min($field);
$max = DB::table($table)->max($field);
if (!is_numeric($start) || !is_numeric($max)) {
echo "Could not determine min and max for `{$field}` on `{$table}`\n";
return;
}

$start = intval($start);
$max = intval($max);

$total = $max - $start;
if ($total < 1) {
return;
}
$num_done = 0;
$num_deleted = 0;
$next_report = 10;
$done = false;
echo "Deleting unused rows from `{$table}`\n";
while (!$done) {
$end = $start + 49999;
$num_deleted += DB::delete("
DELETE FROM $table
WHERE $field BETWEEN $start AND $end
AND $field NOT IN (SELECT $selectfield FROM $targettable)");
$num_done += 50000;
if ($end >= $max) {
$done = true;
} else {
usleep(1);
$start += 50000;
// Calculate percentage of work completed so far.
$percent = round(($num_done / $total) * 100, -1);
if ($percent > $next_report) {
echo "{$next_report}%\n";
$next_report = $next_report + 10;
}
}
}
echo "{$num_deleted} rows deleted from `{$table}`\n";
}
}
25 changes: 0 additions & 25 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ public function upgrade()
@$ComputeTestTiming = $_POST['ComputeTestTiming'];
@$ComputeUpdateStatistics = $_POST['ComputeUpdateStatistics'];

@$Cleanup = $_POST['Cleanup'];
@$Dependencies = $_POST['Dependencies'];
@$Audit = $_POST['Audit'];
@$ClearAudit = $_POST['Clear'];
Expand Down Expand Up @@ -377,23 +376,6 @@ public function upgrade()
unlink($configFile);
}


/* Cleanup the database */
if ($Cleanup) {
self::delete_unused_rows('banner', 'projectid', 'project');
self::delete_unused_rows('dailyupdate', 'projectid', 'project');

self::delete_unused_rows('buildfailuredetails', 'id', 'buildfailure', 'detailsid');
self::delete_unused_rows('configure', 'id', 'build2configure', 'configureid');

self::delete_unused_rows('dailyupdatefile', 'dailyupdateid', 'dailyupdate');
self::delete_unused_rows('coveragefile', 'id', 'coverage', 'fileid');

self::delete_unused_rows('dailyupdatefile', 'dailyupdateid', 'dailyupdate');

$xml .= add_XML_value('alert', 'Database cleanup complete.');
}

/* Check the builds with wrong date */
if ($CheckBuildsWrongDate) {
$currentdate = time() + 3600 * 24 * 3; // or 3 days away from now
Expand Down Expand Up @@ -474,11 +456,4 @@ public function userStatistics(): View
{
return $this->angular_view('userStatistics');
}

/** Delete unused rows */
private static function delete_unused_rows($table, $field, $targettable, $selectfield = 'id'): void
{
DB::delete("DELETE FROM $table WHERE $field NOT IN (SELECT $selectfield AS $field FROM $targettable)");
echo pdo_error();
}
}
4 changes: 0 additions & 4 deletions app/cdash/public/upgrade.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
<td><div align="right">Compute update statistics:</div></td>
<td><div align="left">for the last <input type="text" name="UpdateStatisticsDays" size="2" value="4"/> days <input type="submit" name="ComputeUpdateStatistics" value="Compute update statistics"/></div></td>
</tr>
<tr>
<td><div align="right">Cleanup CDash (can take a long time):</div></td>
<td><input type="submit" name="Cleanup" value="Cleanup database"/></td>
</tr>
<tr>
<td><div align="right">Manage CDash dependencies:</div></td>
<td><input type="submit" name="Audit" value="Display audit report"/>
Expand Down
12 changes: 0 additions & 12 deletions app/cdash/tests/test_upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ function testComputeUpdateStatistics()
}
*/

public function testCleanup()
{
if (!$this->getMaintenancePage()) {
return 1;
}
set_time_limit(0);
if (!$this->clickSubmitByName('Cleanup')) {
$this->fail('clicking Cleanup returned false');
}
$this->assertText('Database cleanup complete.');
}

public function getMaintenancePage()
{
$this->login();
Expand Down

0 comments on commit 31579f1

Please sign in to comment.