Files
coolify/tests/Feature/CleanupDatabaseImportUploadsTest.php
Andras BacsaiandClaude Opus 5.5 476ba3c6ab fix(database-import): delete staged API uploads that were never imported
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>
2026-10-02 16:00:29 +02:00

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();
});