Skip to content

Commit 5d47fdb

Browse files
committed
test
1 parent 2b37c9f commit 5d47fdb

File tree

1 file changed

+43
-9
lines changed
  • ansible/files/admin_api_scripts/pg_upgrade_scripts

1 file changed

+43
-9
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ declare
323323
supabase_admin_rolpassword text := (select rolpassword from pg_authid where rolname = 'supabase_admin');
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);
326+
role_settings jsonb[] := (
327+
select coalesce(array_agg(jsonb_build_object('database', d.datname, 'role', a.rolname, 'configs', s.setconfig)), '{}')
328+
from pg_db_role_setting s
329+
left join pg_database d on d.oid = s.setdatabase
330+
join pg_authid a on a.oid = s.setrole
331+
where a.rolname in ('postgres', 'supabase_admin')
332+
);
326333
event_triggers jsonb[] := (select coalesce(array_agg(jsonb_build_object('name', evtname)), '{}') from pg_event_trigger where evtowner = 'postgres'::regrole);
327334
user_mappings jsonb[] := (
328335
select coalesce(array_agg(jsonb_build_object('oid', um.oid, 'role', a.rolname, 'server', s.srvname, 'options', um.umoptions)), '{}')
@@ -445,9 +452,37 @@ begin
445452
execute(format('alter role supabase_admin password %L;', supabase_admin_rolpassword));
446453
447454
-- role settings
448-
-- TODO: don't modify system catalog directly
449-
update pg_db_role_setting set setconfig = postgres_role_settings where setdatabase = 0 and setrole = 'postgres'::regrole;
450-
update pg_db_role_setting set setconfig = supabase_admin_role_settings where setdatabase = 0 and setrole = 'supabase_admin'::regrole;
455+
foreach obj in array role_settings
456+
loop
457+
raise info '%', (format('alter role %I %s reset all',
458+
case when obj->>'role' = 'postgres' then 'supabase_admin' else 'postgres' end,
459+
case when obj->>'database' is null then '' else format('in database %I', obj->>'database') end
460+
));
461+
execute(format('alter role %I %s reset all',
462+
case when obj->>'role' = 'postgres' then 'supabase_admin' else 'postgres' end,
463+
case when obj->>'database' is null then '' else format('in database %I', obj->>'database') end
464+
));
465+
end loop;
466+
foreach obj in array role_settings
467+
loop
468+
for rec in
469+
select split_part(value, '=', 1) as key, substr(value, strpos(value, '=') + 1) as value
470+
from jsonb_array_elements_text(obj->'configs')
471+
loop
472+
raise info '%', (format('alter role %I %s set %I to %s',
473+
obj->>'role',
474+
case when obj->>'database' is null then '' else format('in database %I', obj->>'database') end,
475+
rec.key,
476+
rec.value
477+
));
478+
execute(format('alter role %I %s set %I to %s',
479+
obj->>'role',
480+
case when obj->>'database' is null then '' else format('in database %I', obj->>'database') end,
481+
rec.key,
482+
rec.value
483+
));
484+
end loop;
485+
end loop;
451486
452487
reassign owned by postgres to supabase_admin;
453488
@@ -722,12 +757,11 @@ EOSQL
722757
su -c "$PGBINNEW/initdb -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
723758
fi
724759

725-
# TODO: Make this declarative, replace file with the most up to date content
726-
# of pg_hba.conf.j2. Otherwise we'd need to supply the password for
727-
# supabase_admin, because pg_upgrade connects to the db as supabase_admin
728-
# using unix sockets, which is gated behind scram-sha-256 per the current
729-
# pg_hba.conf.j2.
730-
echo "local all all trust
760+
# pg_upgrade connects to the db as supabase_admin using unix sockets, which
761+
# is gated behind scram-sha-256 per the current pg_hba.conf.j2. This avoids
762+
# the need to supply the supabase_admin password on the old instance. The
763+
# new instance is unaffected.
764+
echo "local all supabase_admin trust
731765
$(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
732766
run_sql -c "select pg_reload_conf();"
733767

0 commit comments

Comments
 (0)