-
Notifications
You must be signed in to change notification settings - Fork 37
/
clone_schema.sql
38 lines (27 loc) · 1.19 KB
/
clone_schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{#
-- This macro clones the source schema into the destination schema and
-- optionally grants ownership over it and its tables and views to a new owner.
#}
{% macro clone_schema(
source_schema,
destination_schema,
source_database=target.database,
destination_database=target.database,
new_owner_role=''
) %}
{% if source_schema and destination_schema %}
{{ (log("Cloning existing schema " ~ source_database ~ "." ~ source_schema ~
" into schema " ~ destination_database ~ "." ~ destination_schema, info=True)) }}
{% call statement('clone_schema', fetch_result=True, auto_begin=False) -%}
CREATE OR REPLACE SCHEMA {{ destination_database }}.{{ destination_schema }}
CLONE {{ source_database }}.{{ source_schema }}
{%- endcall %}
{%- set result = load_result('clone_schema') -%}
{{ log(result['data'][0][0], info=True)}}
{% else %}
{{ exceptions.raise_compiler_error("Invalid arguments. Missing source schema and/or destination schema") }}
{% endif %}
{% if new_owner_role != '' %}
{{ snowflake_utils.grant_ownership_on_schema_objects(new_owner_role, destination_schema, destination_database) }}
{% endif %}
{% endmacro %}