mirror of
https://github.com/coollabsio/coolify.git
synced 2026-10-28 17:44:36 +08:00
Add an hourly cleanup:database-import-uploads command that removes staged import uploads older than a day, while holding the import lock of the database so a running import keeps its file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
47 lines
1.8 KiB
PHP
47 lines
1.8 KiB
PHP
<?php
|
|
|
|
use App\Actions\Database\StartDatabaseImport;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
beforeEach(function () {
|
|
Storage::fake('local');
|
|
Cache::flush();
|
|
});
|
|
|
|
function stageDatabaseImportUploadForCleanupTest(string $databaseUuid, string $uploadId, Carbon $modifiedAt): string
|
|
{
|
|
$directory = "upload/imports/1/{$databaseUuid}/{$uploadId}";
|
|
Storage::put("{$directory}/restore", 'dump');
|
|
touch(Storage::path("{$directory}/restore"), $modifiedAt->getTimestamp());
|
|
|
|
return $directory;
|
|
}
|
|
|
|
it('deletes staged API import uploads older than a day and keeps newer ones', function () {
|
|
$old = stageDatabaseImportUploadForCleanupTest('db-old', 'f4c1d3a2-0000-4000-8000-000000000001', now()->subHours(25));
|
|
$fresh = stageDatabaseImportUploadForCleanupTest('db-fresh', 'f4c1d3a2-0000-4000-8000-000000000002', now()->subHours(23));
|
|
|
|
$this->artisan('cleanup:database-import-uploads')->assertSuccessful();
|
|
|
|
expect(Storage::exists($old))->toBeFalse()
|
|
->and(Storage::exists('upload/imports/1/db-old'))->toBeFalse()
|
|
->and(Storage::exists("{$fresh}/restore"))->toBeTrue();
|
|
});
|
|
|
|
it('keeps an old staged upload while an import of its database holds the import lock', function () {
|
|
$directory = stageDatabaseImportUploadForCleanupTest('db-busy', 'f4c1d3a2-0000-4000-8000-000000000003', now()->subDays(2));
|
|
$lock = Cache::lock(StartDatabaseImport::lockKey('db-busy'), 60);
|
|
$lock->get();
|
|
|
|
$this->artisan('cleanup:database-import-uploads')->assertSuccessful();
|
|
|
|
expect(Storage::exists("{$directory}/restore"))->toBeTrue();
|
|
|
|
$lock->release();
|
|
$this->artisan('cleanup:database-import-uploads')->assertSuccessful();
|
|
|
|
expect(Storage::exists($directory))->toBeFalse();
|
|
});
|