|
| 1 | +{% macro database_clone_grant_ownership(destination_database, new_owner_role) %} |
| 2 | + |
| 3 | + {% if not destination_database or not new_owner_role %} |
| 4 | + {{ exceptions.raise_compiler_error("Invalid arguments. Missing destination_database and/or new_owner_role") }} |
| 5 | + {% endif %} |
| 6 | + |
| 7 | + {{ log("Granting ownership on " ~ destination_database ~ " to " ~ new_owner_role, info=True) }} |
| 8 | + |
| 9 | + {% call statement('grant_db_ownership', fetch_result=True, auto_begin=False) -%} |
| 10 | + GRANT OWNERSHIP ON DATABASE {{ destination_database }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 11 | + {%- endcall %} |
| 12 | + |
| 13 | + {% set list_schemas_query %} |
| 14 | + SELECT schema_name |
| 15 | + FROM {{ destination_database }}.information_schema.schemata |
| 16 | + WHERE schema_name != 'INFORMATION_SCHEMA' |
| 17 | + {% endset %} |
| 18 | + |
| 19 | + {% if execute %} |
| 20 | + {% set results = run_query(list_schemas_query) %} |
| 21 | + {% set schemata_list = results.columns[0].values() %} |
| 22 | + {% else %} |
| 23 | + {% set schemata_list = [] %} |
| 24 | + {% endif %} |
| 25 | + |
| 26 | + {% for schema_name in schemata_list %} |
| 27 | + {{ log("Granting ownership on schema " ~ destination_database ~ "." ~ schema_name ~ " to " ~ new_owner_role, info=True) }} |
| 28 | + |
| 29 | + {% call statement('grant_schema_' ~ loop.index, auto_begin=False) -%} |
| 30 | + GRANT OWNERSHIP ON SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 31 | + {%- endcall %} |
| 32 | + |
| 33 | + {% call statement('grant_views_' ~ loop.index, auto_begin=False) -%} |
| 34 | + GRANT OWNERSHIP ON ALL VIEWS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 35 | + {%- endcall %} |
| 36 | + |
| 37 | + {% call statement('grant_mat_views_' ~ loop.index, auto_begin=False) -%} |
| 38 | + GRANT OWNERSHIP ON ALL MATERIALIZED VIEWS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 39 | + {%- endcall %} |
| 40 | + |
| 41 | + {% call statement('grant_tables_' ~ loop.index, auto_begin=False) -%} |
| 42 | + GRANT OWNERSHIP ON ALL TABLES IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 43 | + {%- endcall %} |
| 44 | + |
| 45 | + {% call statement('grant_ext_tables_' ~ loop.index, auto_begin=False) -%} |
| 46 | + GRANT OWNERSHIP ON ALL EXTERNAL TABLES IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 47 | + {%- endcall %} |
| 48 | + |
| 49 | + {% call statement('grant_dyn_tables_' ~ loop.index, auto_begin=False) -%} |
| 50 | + GRANT OWNERSHIP ON ALL DYNAMIC TABLES IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 51 | + {%- endcall %} |
| 52 | + |
| 53 | + {% call statement('grant_stages_' ~ loop.index, auto_begin=False) -%} |
| 54 | + GRANT OWNERSHIP ON ALL STAGES IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 55 | + {%- endcall %} |
| 56 | + |
| 57 | + {% call statement('grant_file_formats_' ~ loop.index, auto_begin=False) -%} |
| 58 | + GRANT OWNERSHIP ON ALL FILE FORMATS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 59 | + {%- endcall %} |
| 60 | + |
| 61 | + {% call statement('grant_functions_' ~ loop.index, auto_begin=False) -%} |
| 62 | + GRANT OWNERSHIP ON ALL FUNCTIONS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 63 | + {%- endcall %} |
| 64 | + |
| 65 | + {% call statement('grant_procedures_' ~ loop.index, auto_begin=False) -%} |
| 66 | + GRANT OWNERSHIP ON ALL PROCEDURES IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 67 | + {%- endcall %} |
| 68 | + |
| 69 | + {% call statement('grant_streams_' ~ loop.index, auto_begin=False) -%} |
| 70 | + GRANT OWNERSHIP ON ALL STREAMS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 71 | + {%- endcall %} |
| 72 | + |
| 73 | + {% call statement('grant_tasks_' ~ loop.index, auto_begin=False) -%} |
| 74 | + GRANT OWNERSHIP ON ALL TASKS IN SCHEMA {{ destination_database }}.{{ schema_name }} TO ROLE {{ new_owner_role }} REVOKE CURRENT GRANTS; |
| 75 | + {%- endcall %} |
| 76 | + |
| 77 | + {% endfor %} |
| 78 | + |
| 79 | +{% endmacro %} |
0 commit comments