diff --git a/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql b/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql index 652be2a1d..59ff0bf3a 100644 --- a/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql +++ b/migrations/db/migrations/20241215003910_backfill_pgmq_metadata.sql @@ -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_' insert into pgmq.meta (queue_name, is_partitioned, is_unlogged, created_at) @@ -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 $$;