Snowflake存储过程中Grant Ownership语句被忽略,无法转移数据库所有权
问题解决:Snowflake克隆数据库后所有权转移语句未生效
核心原因
你的存储过程中GRANT OWNERSHIP语句未生效,是因为Snowflake在从高权限角色(如accountadmin)向低权限角色(如sysadmin)转移对象所有权时,必须显式添加REVOKE CURRENT GRANTS子句,否则该语句会静默跳过,不会执行所有权转移,也不会抛出报错。
你的存储过程以execute as owner执行,创建存储过程的角色是accountadmin,克隆后的数据库Y所有权默认属于accountadmin。此时直接执行GRANT OWNERSHIP ON DATABASE Y TO ROLE sysadmin,由于缺少REVOKE CURRENT GRANTS,Snowflake不会完成所有权转移操作。
修复方案
修改存储过程中的所有权转移语句,添加REVOKE CURRENT GRANTS:
grant ownership on database Y to role sysadmin revoke current grants;
完整修改后的存储过程:
use role accountadmin; use database X; use schema public; create or replace procedure test_clone_db() returns varchar language sql comment = 'Creates or replaces a Zero-Copy Clone of the X database into Y' execute as owner as declare message varchar; time_start timestamp default current_timestamp(); begin create or replace database Y clone X; grant usage on database Y to role fake_role; grant ownership on database Y to role sysadmin revoke current grants; -- 添加REVOKE CURRENT GRANTS message := 'Clone created successfully. Ended at ' || current_timestamp() || ' time elapsed: ' || timestampdiff(seconds, time_start, current_timestamp()) || ' seconds'; return message; end;
额外验证建议
如果担心后续出现隐藏错误,可以在存储过程中添加异常捕获逻辑,以便及时发现问题:
begin create or replace database Y clone X; grant usage on database Y to role fake_role; grant ownership on database Y to role sysadmin revoke current grants; exception when other then message := 'Error during execution: ' || sqlcode || ' - ' || sqlerrm; return message; end;
内容的提问来源于stack exchange,提问作者OSUDamian
相关产品推荐
相关产品推荐

