|
| 1 | +-- Test: cleanup_ensure_workers_logs() deletes old entries and returns correct count |
| 2 | +begin; |
| 3 | +select plan(3); |
| 4 | +select pgflow_tests.reset_db(); |
| 5 | + |
| 6 | +-- Clear existing cron job run details |
| 7 | +delete from cron.job_run_details; |
| 8 | + |
| 9 | +-- Setup: Create job run details at different ages (testing 4h custom retention) |
| 10 | +insert into cron.job_run_details (runid, jobid, command, status, end_time) |
| 11 | +values |
| 12 | + (1, 1, 'select some_function()', 'succeeded', now() - interval '5 hours'), -- Should be deleted |
| 13 | + (2, 1, 'select some_function()', 'succeeded', now() - interval '6 hours'), -- Should be deleted |
| 14 | + (3, 1, 'select some_function()', 'succeeded', now() - interval '3 hours'), -- Should be kept |
| 15 | + (4, 1, 'select some_function()', 'succeeded', now() - interval '1 hour'); -- Should be kept |
| 16 | + |
| 17 | +-- Execute cleanup with 4 hour retention |
| 18 | +with result as ( |
| 19 | + select * from pgflow.cleanup_ensure_workers_logs(retention_hours => 4) |
| 20 | +) |
| 21 | +select is( |
| 22 | + (select cron_deleted from result), |
| 23 | + 2::bigint, |
| 24 | + 'Should return count of 2 deleted entries' |
| 25 | +); |
| 26 | + |
| 27 | +-- Test: Old entries were deleted |
| 28 | +select is( |
| 29 | + (select count(*) from cron.job_run_details where runid in (1, 2)), |
| 30 | + 0::bigint, |
| 31 | + 'Entries older than retention should be deleted' |
| 32 | +); |
| 33 | + |
| 34 | +-- Test: Recent entries were kept |
| 35 | +select is( |
| 36 | + (select count(*) from cron.job_run_details where runid in (3, 4)), |
| 37 | + 2::bigint, |
| 38 | + 'Entries newer than retention should be kept' |
| 39 | +); |
| 40 | + |
| 41 | +select finish(); |
| 42 | +rollback; |
0 commit comments