Skip to content

Commit 2a778f7

Browse files
authored
pgmq migration safety (#1380)
* improve pgmq migration safety * normalize whitespace
1 parent 039c470 commit 2a778f7

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ do $$
33
begin
44
-- Check if the pgmq.meta table exists
55
if exists (
6-
select 1
7-
from pg_catalog.pg_class c
8-
join pg_catalog.pg_namespace n on c.relnamespace = n.oid
9-
where n.nspname = 'pgmq' and c.relname = 'meta'
6+
select
7+
1
8+
from
9+
pg_catalog.pg_class c
10+
join pg_catalog.pg_namespace n
11+
on c.relnamespace = n.oid
12+
where
13+
n.nspname = 'pgmq'
14+
and c.relname = 'meta'
15+
and c.relkind = 'r' -- regular table
16+
-- Make sure only expected columns exist and are correctly named
17+
and (
18+
select array_agg(attname::text order by attname)
19+
from pg_catalog.pg_attribute a
20+
where
21+
a.attnum > 0
22+
and a.attrelid = c.oid
23+
) = array['created_at', 'is_partitioned', 'is_unlogged', 'queue_name']::text[]
1024
) then
1125
-- Insert data into pgmq.meta for all tables matching the naming pattern 'pgmq.q_<queue_name>'
1226
insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at)
@@ -20,9 +34,10 @@ begin
2034
join pg_catalog.pg_namespace n
2135
on c.relnamespace = n.oid
2236
where
23-
n.nspname = 'pgmq'
24-
and c.relname like 'q_%'
25-
and c.relkind in ('r', 'p', 'u');
37+
n.nspname = 'pgmq'
38+
and c.relname like 'q_%'
39+
and c.relkind in ('r', 'p', 'u')
40+
on conflict (queue_name) do nothing;
2641
end if;
2742
end $$;
2843

0 commit comments

Comments
 (0)