Skip to content

Commit

Permalink
Merge pull request #8326 from ehuelsmann/fix/1.11/migration-with-exis…
Browse files Browse the repository at this point in the history
…ting-role

Drop legacy role if new already exists
  • Loading branch information
ehuelsmann authored Aug 1, 2024
2 parents c52eb3d + 3cfb87a commit 3211093
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sql/changes/1.11/ar-ap-role-naming-consistency.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ CREATE FUNCTION pg_temp.lsmb__role(global_role text) RETURNS text
DO $$
DECLARE
t_rolename text;
t_new_rolename text;
BEGIN
select pg_temp.lsmb__role('ap_all_vouchers') into t_rolename;
select pg_temp.lsmb__role('ap_voucher_all') into t_new_rolename;

perform * from pg_roles where rolname = t_rolename;
if found then
execute 'alter role ' || quote_ident(t_rolename) || ' rename to ' || quote_ident(pg_temp.lsmb__role('ap_voucher_all'));
perform * from pg_roles where rolname = t_new_rolename;
if found then
execute 'drop role if exists' || quote_ident(t_rolename);
else
execute 'alter role ' || quote_ident(t_rolename) || ' rename to ' || quote_ident(t_new_rolename);
end if;
end if;

-- drop a role which doesn't have an AR equivalent and from its naming
Expand Down
26 changes: 26 additions & 0 deletions sql/changes/1.11/ar-ap-role-naming-consistency.sql@1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

-- Define a function which will be dropped after the session ends
CREATE FUNCTION pg_temp.lsmb__role(global_role text) RETURNS text
LANGUAGE SQL STABLE AS
$$ select coalesce((select value
from defaults
where setting_key = 'role_prefix'),
'lsmb_' || current_database() || '__') || global_role; $$;

DO $$
DECLARE
t_rolename text;
BEGIN
select pg_temp.lsmb__role('ap_all_vouchers') into t_rolename;
perform * from pg_roles where rolname = t_rolename;
if found then
execute 'alter role ' || quote_ident(t_rolename) || ' rename to ' || quote_ident(pg_temp.lsmb__role('ap_voucher_all'));
end if;

-- drop a role which doesn't have an AR equivalent and from its naming
-- is at least confusing...
select pg_temp.lsmb__role('ap_all_transactions') into t_rolename;
execute 'drop role if exists ' || quote_ident(t_rolename);
END;
$$ language plpgsql;

0 comments on commit 3211093

Please sign in to comment.