|
1 | 1 | -- migrate:up |
2 | 2 | do $$ |
3 | 3 | begin |
4 | | - -- First, verify that pgmq.meta is a regular table with the required structure |
| 4 | + -- Check if the pgmq.meta table exists |
5 | 5 | if exists ( |
6 | 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' |
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' |
37 | 10 | ) 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'); |
61 | 26 | end if; |
62 | 27 | end $$; |
63 | 28 |
|
|
0 commit comments