Files
coolify/tests/Feature/CleanupDatabaseCommandTest.php
T
Andras BacsaiandClaude Opus 5.5 dd7ad91ba2 fix(cleanup): delete old rows in batches and report the real count
cleanup:database used orderBy()->skip(10)->delete(). Laravel drops the offset
in a DELETE, so it deleted all old rows anyway, and the count query with the
offset printed 0 on PostgreSQL. Count without the offset, delete in batches of
1000 rows so a large table is not locked by one long DELETE, and write the
output through the command so it is captured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 11:50:32 +02:00

37 lines
1.3 KiB
PHP

<?php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Activitylog\Models\Activity;
uses(RefreshDatabase::class);
function cleanupTestActivity(int $ageInDays): Activity
{
$activity = activity()->withProperties(['type' => 'inline', 'status' => 'finished'])->event('inline')->log('[]');
Activity::query()->whereKey($activity->id)->toBase()->update(['created_at' => now()->subDays($ageInDays)]);
return $activity;
}
beforeEach(function () {
$this->old = collect(range(1, 12))->map(fn () => cleanupTestActivity(61));
$this->recent = collect(range(1, 2))->map(fn () => cleanupTestActivity(5));
});
it('reports the real number of old activities in a dry run and deletes nothing', function () {
$this->artisan('cleanup:database')
->expectsOutputToContain('Delete 12 entries from activity_log.')
->assertSuccessful();
expect(Activity::query()->count())->toBe(14);
});
it('deletes all activities older than the retention and keeps newer ones', function () {
$this->artisan('cleanup:database --yes')
->expectsOutputToContain('Delete 12 entries from activity_log.')
->assertSuccessful();
expect(Activity::query()->pluck('id')->sort()->values()->all())
->toBe($this->recent->pluck('id')->sort()->values()->all());
});