Skip to content

Commit 5c7be93

Browse files
refactor(migrations): simplify pgmq.meta backfill migration
- Remove verification step as it is not needed - Add descriptive aliases to improve query readability - Maintain fully qualified table names in FROM clause - Remove redundant nested IF EXISTS check Co-Authored-By: [email protected] <[email protected]>
1 parent bb9d583 commit 5c7be93

File tree

1 file changed

+19
-54
lines changed

1 file changed

+19
-54
lines changed

migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
11
-- migrate:up
22
do $$
33
begin
4-
-- First, verify that pgmq.meta is a regular table with the required structure
4+
-- Check if the pgmq.meta table exists
55
if exists (
66
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'
10-
and c.relname = 'meta'
11-
and c.relkind = 'r' -- Verify it's a regular table
12-
-- Verify the table has exactly these columns and no others
13-
and (
14-
select count(*)
15-
from pg_catalog.pg_attribute a
16-
where a.attrelid = c.oid
17-
and a.attnum > 0
18-
and not a.attisdropped
19-
) = 4 -- Only 4 columns should exist
20-
and not exists (
21-
select 1
22-
from pg_catalog.pg_attribute a
23-
where a.attrelid = c.oid
24-
and a.attnum > 0
25-
and not a.attisdropped
26-
and a.attname not in ('queue_name', 'is_partitioned', 'is_unlogged', 'created_at')
27-
) -- No other columns should exist
28-
and exists (
29-
select 1
30-
from pg_catalog.pg_attribute a
31-
where a.attrelid = c.oid
32-
and a.attnum > 0
33-
and not a.attisdropped
34-
and a.attname in ('queue_name', 'is_partitioned', 'is_unlogged', 'created_at')
35-
having count(*) = 4 -- All required columns must exist
36-
)
7+
from pg_catalog.pg_class cls
8+
join pg_catalog.pg_namespace ns on cls.relnamespace = ns.oid
9+
where ns.nspname = 'pgmq' and cls.relname = 'meta'
3710
) then
38-
-- Check if the pgmq.meta table exists
39-
if exists (
40-
select 1
41-
from pg_catalog.pg_class c
42-
join pg_catalog.pg_namespace n on c.relnamespace = n.oid
43-
where n.nspname = 'pgmq' and c.relname = 'meta'
44-
) then
45-
-- Insert data into pgmq.meta for all tables matching the naming pattern 'pgmq.q_<queue_name>'
46-
insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at)
47-
select
48-
substring(c.relname from 3) as queue_name,
49-
false as is_partitioned,
50-
case when c.relpersistence = 'u' then true else false end as is_unlogged,
51-
now() as created_at
52-
from
53-
pg_catalog.pg_class c
54-
join pg_catalog.pg_namespace n
55-
on c.relnamespace = n.oid
56-
where
57-
n.nspname = 'pgmq'
58-
and c.relname like 'q_%'
59-
and c.relkind in ('r', 'p', 'u');
60-
end if;
11+
-- Insert data into pgmq.meta for all tables matching the naming pattern 'pgmq.q_<queue_name>'
12+
insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at)
13+
select
14+
substring(cls.relname from 3) as queue_name,
15+
false as is_partitioned,
16+
case when cls.relpersistence = 'u' then true else false end as is_unlogged,
17+
now() as created_at
18+
from
19+
pg_catalog.pg_class cls
20+
join pg_catalog.pg_namespace ns
21+
on cls.relnamespace = ns.oid
22+
where
23+
ns.nspname = 'pgmq'
24+
and cls.relname like 'q_%'
25+
and cls.relkind in ('r', 'p', 'u');
6126
end if;
6227
end $$;
6328

0 commit comments

Comments
 (0)