Skip to content

Commit

Permalink
control-plane: fix deletion of old drafts
Browse files Browse the repository at this point in the history
This commit fixes the migration for deleting old drafts, so that it can be
applied.  It was broken, and was thankfully never applied in production.
  • Loading branch information
psFried committed Sep 26, 2023
1 parent 7513a2f commit 6c4f463
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions supabase/migrations/26_delete_old_drafts.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
begin;
-- The purpose of this is to cleanup old drafts, draft_specs, discovers, and evolutions.
-- These things can pile up over time, and there's no need to retain them for a long time.
-- Note that the cleanup of draft_specs, discovers, and evolutions happens due to cascading
-- deletions from drafts.

-- We need to add the foreign key constraint to evolutions, since it was not there originally.
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()
Expand All @@ -16,9 +18,11 @@ returns bigint as $$
select count(*) from d;
$$ language sql 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)
-- $$ delete from drafts $$
-- );
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() $$
);

commit;

0 comments on commit 6c4f463

Please sign in to comment.