Skip to content

Commit

Permalink
control-plane: delete old log_lines
Browse files Browse the repository at this point in the history
Upgrades the `delete_old_drafts` function to `delete_old_rows`, because
it's now our one-stop-shop for daily cleanup. This adds deletion of old
`internal.log_lines`, which would otherwise accumulate indefinitely.
  • Loading branch information
psFried committed Sep 26, 2023
1 parent 6c4f463 commit e5e79c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,34 @@ begin;
delete from evolutions e where not exists (select d.id from drafts d where d.id = e.draft_id);
alter table evolutions add foreign key (draft_id) references drafts(id) on delete cascade;

create or replace function internal.delete_old_drafts()
returns bigint as $$
-- These CTE shennanigans brought to you by this rando:
-- https://stackoverflow.com/a/47857304
create or replace function internal.delete_old_rows()
returns jsonb as $$
declare
n_drafts integer;
n_logs integer;
begin
with d as (
delete from drafts where updated_at < (now() - '30 days'::interval) returning *
delete from public.drafts where updated_at < (now() - '30 days'::interval) returning *
)
select count(*) from d;
$$ language sql security definer;
select into n_drafts count(*) as n from d;

with l as (
delete from internal.log_lines where logged_at < (now() - '30 days'::interval) returning *
)
select into n_logs count(*) as n from l;

return json_build_object(
'drafts', coalesce(n_drafts, 0),
'log_lines', coalesce(n_logs)
);
end;
$$ language plpgsql security definer;

create extension if not exists pg_cron with schema extensions;
select cron.schedule(
'delete-drafts', -- name of the cron job
'0 15 * * *', -- Every day at 15:00Z (midmorning EST)
$$ select internal.delete_old_drafts() $$
'delete-drafts', -- name of the cron job
'0 05 * * *', -- Every day at 05:00Z
$$ select internal.delete_old_rows() $$
);

commit;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


create function tests.test_draft_deletion()
create function tests.test_delete_old_rows()
returns setof text as $$
declare
test_user_id uuid = '5ec31342-bfda-43d7-ac62-107e7e2f7f96';
Expand Down Expand Up @@ -32,7 +32,18 @@ begin
(old_draft_id, test_connector_tag_id, 'should delete', 'a/b/c', '{"type": "discoverFailed"}', '{}'),
(new_draft_id, test_connector_tag_id, 'should retain', 'a/b/d', '{"type": "discoverFailed"}', '{}');

return query select ok(internal.delete_old_drafts() = 1, 'one draft should have been deleted');

insert into internal.log_lines (log_line, stream, logged_at, token) values
('should delete line', 'foo', now() - '90 days'::interval, gen_random_uuid()),
('should delete line too', 'foo', now() - '31 days'::interval, gen_random_uuid()),
('should delete line', 'foo', now() - '29 days'::interval, gen_random_uuid()),
('should delete line', 'foo', now() - '5 minutes'::interval, gen_random_uuid());

--return query select ok(internal.delete_old_drafts() = 1, 'one draft should have been deleted');
return query select results_eq(
$i$ select internal.delete_old_rows() $i$,
$i$ values ('{"drafts": 1, "log_lines": 2}'::jsonb) $i$
);

return query select results_eq(
$i$ select ds.catalog_name::text
Expand Down

0 comments on commit e5e79c1

Please sign in to comment.