mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4145261b6e
commit
dd7ad91ba2
@@ -0,0 +1,36 @@
|
||||
<?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());
|
||||
});
|
||||
Reference in New Issue
Block a user