Skip to content

Commit f2b40e8

Browse files
committed
test
1 parent 034d40f commit f2b40e8

File tree

1 file changed

+27
-7
lines changed
  • ansible/files/admin_api_scripts/pg_upgrade_scripts

1 file changed

+27
-7
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,29 @@ declare
324324
postgres_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'postgres'::regrole);
325325
supabase_admin_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'supabase_admin'::regrole);
326326
event_triggers jsonb[] := (select coalesce(array_agg(jsonb_build_object('name', evtname)), '{}') from pg_event_trigger where evtowner = 'postgres'::regrole);
327-
default_acls jsonb[] := (
328-
select coalesce(array_agg(jsonb_build_object('oid', d.oid, 'role', a.rolname, 'schema', n.nspname, 'objtype', d.defaclobjtype, 'acl', defaclacl::text)), '{}')
329-
from pg_default_acl d
330-
join pg_authid a on a.oid = d.defaclrole
331-
left join pg_namespace n on n.oid = d.defaclnamespace
327+
user_mappings jsonb[] := (
328+
select coalesce(array_agg(jsonb_build_object('oid', um.oid, 'role', a.rolname, 'server', s.srvname, 'options', um.umoptions::text)), '{}')
329+
from pg_user_mapping um
330+
join pg_authid a on a.oid = um.umuser
331+
join pg_foreign_server s on s.oid = um.umserver
332+
where a.rolname in ('postgres', 'supabase_admin')
332333
);
333-
-- We only care about swapping init_privs for extensions
334+
-- Objects can have initial privileges either by having those privileges set
335+
-- when the system is initialized (by initdb) or when the object is created
336+
-- during a CREATE EXTENSION and the extension script sets initial
337+
-- privileges using the GRANT system. (https://www.postgresql.org/docs/current/catalog-pg-init-privs.html)
338+
-- We only care about swapping init_privs for extensions.
334339
init_privs jsonb[] := (
335340
select coalesce(array_agg(jsonb_build_object('objoid', objoid, 'classoid', classoid, 'initprivs', initprivs::text)), '{}')
336341
from pg_init_privs
337342
where privtype = 'e'
338343
);
344+
default_acls jsonb[] := (
345+
select coalesce(array_agg(jsonb_build_object('oid', d.oid, 'role', a.rolname, 'schema', n.nspname, 'objtype', d.defaclobjtype, 'acl', defaclacl::text)), '{}')
346+
from pg_default_acl d
347+
join pg_authid a on a.oid = d.defaclrole
348+
left join pg_namespace n on n.oid = d.defaclnamespace
349+
);
339350
schemas jsonb[] := (
340351
select coalesce(array_agg(jsonb_build_object('oid', n.oid, 'owner', a.rolname, 'acl', nspacl::text)), '{}')
341352
from pg_namespace n
@@ -476,11 +487,20 @@ begin
476487
477488
-- user mappings
478489
-- TODO: don't modify system catalog directly
479-
update pg_user_mapping set umuser = 'postgres'::regrole where umuser = 'supabase_admin'::regrole;
490+
foreach obj in array user_mappings
491+
loop
492+
execute(format('drop user mapping for %I server %I', case when obj->>'role' = 'postgres' then 'supabase_admin' else 'postgres' end, obj->>'server'));
493+
end loop;
494+
foreach obj in array user_mappings
495+
loop
496+
execute(format('create user mapping for %I server %I', obj->>'role', obj->>'server'));
497+
update pg_user_mapping set umoptions = (obj->>'options')::text[] where umuser = (obj->>'role')::regrole and umserver = (select oid from pg_foreign_server where srvname = obj->>'server');
498+
end loop;
480499
481500
-- init privs
482501
foreach obj in array init_privs
483502
loop
503+
-- We need to modify system catalog directly here because there's no ALTER INIT PRIVILEGES.
484504
update pg_init_privs set initprivs = (obj->>'initprivs')::aclitem[] where objoid = (obj->>'objoid')::oid and classoid = (obj->>'classoid')::oid;
485505
end loop;
486506

0 commit comments

Comments
 (0)