Skip to content

Commit d4c1297

Browse files
committed
test
1 parent 3c96465 commit d4c1297

File tree

1 file changed

+44
-18
lines changed
  • ansible/files/admin_api_scripts/pg_upgrade_scripts

1 file changed

+44
-18
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,29 @@ function initiate_upgrade {
316316
if [ "$OLD_BOOTSTRAP_USER" = "postgres" ]; then
317317
run_sql -c "create role supabase_tmp login superuser;"
318318
# TODO: move to its own file
319-
# TODO: GRANTED BY, ADMIN OPTION
319+
# TODO: GRANT TO postgres
320+
# TODO: GRANTED BY postgres
320321
psql -h localhost -U supabase_tmp -d postgres <<'EOSQL'
321322
do $$
322323
declare
323324
postgres_rolpassword text := (select rolpassword from pg_authid where rolname = 'postgres');
324325
supabase_admin_rolpassword text := (select rolpassword from pg_authid where rolname = 'supabase_admin');
325326
postgres_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'postgres'::regrole);
326327
supabase_admin_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'supabase_admin'::regrole);
327-
schemas jsonb[] := (select coalesce(array_agg(jsonb_build_object('oid', oid, 'owner', nspowner::regrole, 'acl', nspacl::text)), '{}') from pg_namespace);
328+
schemas jsonb[] := (
329+
select coalesce(array_agg(jsonb_build_object('oid', oid, 'owner', nspowner::regrole, 'acl', nspacl::text)), '{}')
330+
from pg_namespace
331+
where true
332+
and nspname != 'information_schema'
333+
and not starts_with(nspname, 'pg_')
334+
);
328335
types jsonb[] := (
329336
select coalesce(array_agg(jsonb_build_object('oid', t.oid, 'acl', t.typacl::text)), '{}')
330337
from pg_type t
331338
join pg_namespace n on n.oid = t.typnamespace
332-
join pg_authid a on a.oid = t.typowner
333339
where true
334340
and n.nspname != 'information_schema'
335341
and not starts_with(n.nspname, 'pg_')
336-
and a.rolname = 'postgres'
337342
and (
338343
t.typrelid = 0
339344
or (
@@ -354,7 +359,7 @@ declare
354359
and el.typarray = t.oid
355360
)
356361
);
357-
routines jsonb[] := (
362+
functions jsonb[] := (
358363
select coalesce(array_agg(jsonb_build_object('oid', p.oid, 'acl', p.proacl::text)), '{}')
359364
from pg_proc p
360365
join pg_namespace n on n.oid = p.pronamespace
@@ -466,13 +471,12 @@ begin
466471
foreach obj in array schemas
467472
loop
468473
if obj->>'owner' = 'postgres' then
469-
execute(format('alter schema %I owner to postgres;', (obj->>'oid')::regnamespace));
474+
execute(format('alter schema %s owner to postgres;', (obj->>'oid')::regnamespace));
470475
end if;
471-
-- TODO: don't modify system catalog directly
472476
for rec in
473477
select grantor, grantee, privilege_type, is_grantable
474478
from aclexplode((obj->>'acl')::aclitem[])
475-
where grantee = 'supabase_admin'::regrole
479+
where grantee = 'postgres'::regrole
476480
loop
477481
execute(format('grant %s on schema %s to postgres %s', rec.privilege_type, (obj->>'oid')::regnamespace, case when rec.is_grantable then 'with grant option' else '' end));
478482
end loop;
@@ -481,26 +485,48 @@ begin
481485
-- types
482486
foreach obj in array types
483487
loop
484-
execute(format('alter type %s owner to postgres;', (obj->>'oid')::regtype));
485-
-- TODO: don't modify system catalog directly
486-
update pg_type set typacl = (obj->>'acl')::aclitem[] where oid = obj->>'oid';
488+
if obj->>'owner' = 'postgres' then
489+
execute(format('alter type %s owner to postgres;', (obj->>'oid')::regtype));
490+
end if;
491+
for rec in
492+
select grantor, grantee, privilege_type, is_grantable
493+
from aclexplode((obj->>'acl')::aclitem[])
494+
where grantee = 'postgres'::regrole
495+
loop
496+
execute(format('grant %s on type %s to postgres %s', rec.privilege_type, (obj->>'oid')::regtype, case when rec.is_grantable then 'with grant option' else '' end));
497+
end loop;
487498
end loop;
488499
489500
-- functions
490-
foreach obj in array routines
501+
foreach obj in array functions
491502
loop
492-
execute(format('alter routine %s(%s) owner to postgres;', (obj->>'oid')::regproc, pg_get_function_identity_arguments((obj->>'oid')::regproc)));
493-
-- TODO: don't modify system catalog directly
494-
update pg_proc set proacl = (obj->>'acl')::aclitem[] where oid = (obj->>'oid')::regproc;
503+
if obj->>'owner' = 'postgres' then
504+
execute(format('alter routine %s(%s) owner to postgres;', (obj->>'oid')::regproc, pg_get_function_identity_arguments((obj->>'oid')::regproc)));
505+
end if;
506+
for rec in
507+
select grantor, grantee, privilege_type, is_grantable
508+
from aclexplode((obj->>'acl')::aclitem[])
509+
where grantee = 'postgres'::regrole
510+
loop
511+
execute(format('grant %s on function %s(%s) to postgres %s', rec.privilege_type, (obj->>'oid')::regproc, pg_get_function_identity_arguments((obj->>'oid')::regproc), case when rec.is_grantable then 'with grant option' else '' end));
512+
end loop;
495513
end loop;
496514
497515
-- relations
498516
foreach obj in array relations
499517
loop
500518
-- obj->>'oid' (text) needs to be casted to oid first for some reason
501-
execute(format('alter table %s owner to postgres;', (obj->>'oid')::oid::regclass));
502-
-- TODO: don't modify system catalog directly
503-
update pg_class set relacl = (obj->>'acl')::aclitem[] where oid = (obj->>'oid')::oid::regclass;
519+
520+
if obj->>'owner' = 'postgres' then
521+
execute(format('alter table %s owner to postgres;', (obj->>'oid')::oid::regclass));
522+
end if;
523+
for rec in
524+
select grantor, grantee, privilege_type, is_grantable
525+
from aclexplode((obj->>'acl')::aclitem[])
526+
where grantee = 'postgres'::regrole
527+
loop
528+
execute(format('grant %s on table %s to postgres %s', rec.privilege_type, (obj->>'oid')::oid::regclass, case when rec.is_grantable then 'with grant option' else '' end));
529+
end loop;
504530
end loop;
505531
end
506532
$$;

0 commit comments

Comments
 (0)