Skip to content

Commit

Permalink
pgmq migration safety (#1380)
Browse files Browse the repository at this point in the history
* improve pgmq migration safety

* normalize whitespace
  • Loading branch information
olirice authored Dec 16, 2024
1 parent 039c470 commit 2a778f7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ do $$
begin
-- Check if the pgmq.meta table exists
if exists (
select 1
from pg_catalog.pg_class c
join pg_catalog.pg_namespace n on c.relnamespace = n.oid
where n.nspname = 'pgmq' and c.relname = 'meta'
select
1
from
pg_catalog.pg_class c
join pg_catalog.pg_namespace n
on c.relnamespace = n.oid
where
n.nspname = 'pgmq'
and c.relname = 'meta'
and c.relkind = 'r' -- regular table
-- Make sure only expected columns exist and are correctly named
and (
select array_agg(attname::text order by attname)
from pg_catalog.pg_attribute a
where
a.attnum > 0
and a.attrelid = c.oid
) = array['created_at', 'is_partitioned', 'is_unlogged', 'queue_name']::text[]
) then
-- Insert data into pgmq.meta for all tables matching the naming pattern 'pgmq.q_<queue_name>'
insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at)
Expand All @@ -20,9 +34,10 @@ begin
join pg_catalog.pg_namespace n
on c.relnamespace = n.oid
where
n.nspname = 'pgmq'
and c.relname like 'q_%'
and c.relkind in ('r', 'p', 'u');
n.nspname = 'pgmq'
and c.relname like 'q_%'
and c.relkind in ('r', 'p', 'u')
on conflict (queue_name) do nothing;
end if;
end $$;

Expand Down

0 comments on commit 2a778f7

Please sign in to comment.