Skip to content

Commit

Permalink
Move deleteUnusedRows to DatabaseCleanupUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
zackgalbreath committed Nov 21, 2024
1 parent a362e63 commit fda4390
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 53 deletions.
65 changes: 12 additions & 53 deletions app/Console/Commands/DeepCleanDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Utils\DatabaseCleanupUtils;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

Expand All @@ -26,64 +27,22 @@ public function handle(): void
projectid NOT IN (SELECT id FROM project)");
echo "{$num_deleted} rows deleted from `banner`\n";

self::delete_unused_rows('dailyupdate', 'projectid', 'project');
// Reconfigure laravel to log to stderr for the rest of this command.
config(['logging.default' => 'stderr']);

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');
DatabaseCleanupUtils::deleteUnusedRows('dailyupdate', 'projectid', 'project');

DatabaseCleanupUtils::deleteUnusedRows('buildfailuredetails', 'id', 'buildfailure', 'detailsid');
DatabaseCleanupUtils::deleteUnusedRows('configure', 'id', 'build2configure', 'configureid');
DatabaseCleanupUtils::deleteUnusedRows('coveragefile', 'id', 'coverage', 'fileid');
DatabaseCleanupUtils::deleteUnusedRows('dailyupdatefile', 'dailyupdateid', 'dailyupdate');
DatabaseCleanupUtils::deleteUnusedRows('note', 'id', 'build2note', 'noteid');
DatabaseCleanupUtils::deleteUnusedRows('testoutput', 'id', 'build2test', 'outputid');
DatabaseCleanupUtils::deleteUnusedRows('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";
}
}
45 changes: 45 additions & 0 deletions app/Utils/DatabaseCleanupUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,49 @@ private static function deleteRowsChunked(string $query, array $ids): void
usleep(1);
}
}

/** Delete unused rows in batches */
public static function deleteUnusedRows(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)) {
Log::info("Could not determine min and max for `{$field}` on `{$table}`");
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;
Log::info("Deleting unused rows from `{$table}`");
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) {
Log::info("{$next_report}%");
$next_report = $next_report + 10;
}
}
}
Log::info("{$num_deleted} rows deleted from `{$table}`");
}
}

0 comments on commit fda4390

Please sign in to comment.