@@ -315,20 +315,159 @@ function initiate_upgrade {
315
315
echo " 8. TODO"
316
316
run_sql -c " alter role postgres superuser;"
317
317
run_sql -c " create role supabase_tmp login superuser;"
318
- PGOPTIONS=' -c pg_stat_statements.track=none' psql -h localhost -U supabase_tmp -d postgres " $@ " << -EOSQL
318
+ psql -h localhost -U supabase_tmp -d postgres << -EOSQL
319
+ begin;
319
320
do $$
320
321
declare
321
- postgres_rolpassword text := select rolpassword from pg_authid where rolname = 'postgres';
322
- supabase_admin_rolpassword text := select rolpassword from pg_authid where rolname = 'supabase_admin';
322
+ postgres_rolpassword text := (select rolpassword from pg_authid where rolname = 'postgres');
323
+ supabase_admin_rolpassword text := (select rolpassword from pg_authid where rolname = 'supabase_admin');
324
+ postgres_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'postgres'::regrole);
325
+ supabase_admin_role_settings text[] := (select setconfig from pg_db_role_setting where setdatabase = 0 and setrole = 'supabase_admin'::regrole);
326
+ schemas oid[] := (select coalesce(array_agg(oid), '{}') from pg_namespace where nspowner = 'postgres'::regrole);
327
+ types oid[] := (
328
+ select coalesce(array_agg(t.oid), '{}')
329
+ from pg_type t
330
+ join pg_namespace n on n.oid = t.typnamespace
331
+ join pg_authid a on a.oid = t.typowner
332
+ where true
333
+ and n.nspname != 'information_schema'
334
+ and not starts_with(n.nspname, 'pg_')
335
+ and a.rolname = 'postgres'
336
+ and (
337
+ t.typrelid = 0
338
+ or (
339
+ select
340
+ c.relkind = 'c'
341
+ from
342
+ pg_class c
343
+ where
344
+ c.oid = t.typrelid
345
+ )
346
+ )
347
+ and not exists (
348
+ select
349
+ from
350
+ pg_type el
351
+ where
352
+ el.oid = t.typelem
353
+ and el.typarray = t.oid
354
+ )
355
+ );
356
+ routines oid[] := (
357
+ select coalesce(array_agg(p.oid), '{}')
358
+ from pg_proc p
359
+ join pg_namespace n on n.oid = p.pronamespace
360
+ join pg_authid a on a.oid = p.proowner
361
+ where true
362
+ and n.nspname != 'information_schema'
363
+ and not starts_with(n.nspname, 'pg_')
364
+ and a.rolname = 'postgres'
365
+ );
366
+ relations oid[] := (
367
+ select coalesce(array_agg(c.oid), '{}')
368
+ from pg_class c
369
+ join pg_namespace n on n.oid = c.relnamespace
370
+ join pg_authid a on a.oid = c.relowner
371
+ where true
372
+ and n.nspname != 'information_schema'
373
+ and not starts_with(n.nspname, 'pg_')
374
+ and a.rolname = 'postgres'
375
+ and c.relkind not in ('c', 'i')
376
+ );
377
+ rec record;
378
+ objid oid;
323
379
begin
380
+ set local search_path = '';
381
+
324
382
alter role postgres rename to supabase_admin_;
325
383
alter role supabase_admin rename to postgres;
326
384
alter role supabase_admin_ rename to supabase_admin;
327
385
328
- execute(format('alter role postgres password %L', postgres_rolpassword));
329
- execute(format('alter role supabase_admin password %L', supabase_admin_rolpassword));
386
+ -- role grants
387
+ for rec in
388
+ select * from pg_auth_members where member = 'supabase_admin'::regrole
389
+ loop
390
+ execute(format('revoke %I from supabase_admin;', rec.roleid::regrole));
391
+ execute(format('grant %I to postgres;', rec.roleid::regrole));
392
+ end loop;
393
+
394
+ -- role passwords
395
+ execute(format('alter role postgres password %L;', postgres_rolpassword));
396
+ execute(format('alter role supabase_admin password %L;', supabase_admin_rolpassword));
397
+
398
+ -- role settings
399
+ -- TODO: don't modify system catalog directly
400
+ update pg_db_role_setting set setconfig = postgres_role_settings where setdatabase = 0 and setrole = 'postgres'::regrole;
401
+ update pg_db_role_setting set setconfig = supabase_admin_role_settings where setdatabase = 0 and setrole = 'supabase_admin'::regrole;
402
+
403
+ reassign owned by postgres to supabase_admin;
404
+
405
+ -- databases
406
+ for rec in
407
+ select * from pg_database where datname not in ('template0')
408
+ loop
409
+ execute(format('alter database %I owner to postgres;', rec.datname));
410
+ end loop;
411
+
412
+ -- publications
413
+ for rec in
414
+ select * from pg_publication
415
+ loop
416
+ execute(format('alter publication %I owner to postgres;', rec.pubname));
417
+ end loop;
418
+
419
+ -- FDWs
420
+ for rec in
421
+ select * from pg_foreign_data_wrapper
422
+ loop
423
+ execute(format('alter foreign data wrapper %I owner to postgres;', rec.fdwname));
424
+ end loop;
425
+
426
+ -- foreign servers
427
+ for rec in
428
+ select * from pg_foreign_server
429
+ loop
430
+ execute(format('alter server %I owner to postgres;', rec.srvname));
431
+ end loop;
432
+
433
+ -- user mappings
434
+ -- TODO: don't modify system catalog directly
435
+ update pg_user_mapping set umuser = 'postgres'::regrole where umuser = 'supabase_admin'::regrole;
436
+
437
+ -- default acls
438
+ -- TODO: don't modify system catalog directly
439
+ update pg_default_acl set defaclrole = 0 where defaclrole = 'postgres'::regrole;
440
+ update pg_default_acl set defaclrole = 'postgres'::regrole where defaclrole = 'supabase_admin'::regrole;
441
+ update pg_default_acl set defaclrole = 'supabase_admin'::regrole where defaclrole = 0;
442
+
443
+ -- schemas
444
+ foreach objid in array schemas
445
+ loop
446
+ execute(format('alter schema %I owner to postgres;', objid::regnamespace));
447
+ end loop;
448
+
449
+ -- types
450
+ foreach objid in array types
451
+ loop
452
+ execute(format('alter type %I owner to postgres;', objid::regtype));
453
+ end loop;
454
+
455
+ -- functions
456
+ for rec in
457
+ select * from pg_proc where oid = any(routines)
458
+ loop
459
+ execute(format('alter routine %I.%I(%s) owner to postgres;', rec.pronamespace::regnamespace, rec.proname, pg_get_function_identity_arguments(rec.oid)));
460
+ end loop;
461
+
462
+ -- relations
463
+ for rec in
464
+ select * from pg_class where oid = any(relations)
465
+ loop
466
+ execute(format('alter table %I.%I owner to postgres;', rec.relnamespace::regnamespace, rec.relname));
467
+ end loop;
330
468
end
331
469
$$ ;
470
+ rollback;
332
471
EOSQL
333
472
run_sql -c " drop role supabase_tmp;"
334
473
0 commit comments