Skip to content

Commit

Permalink
Adding a schema check before privilege dependent ddl in Snowflake (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianrobinson-sage authored Aug 16, 2023
1 parent 1873d87 commit f4536ba
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions macros/plugins/snowflake/create_external_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% macro snowflake__create_external_schema(source_node) %}

{% set schema_exists_query %}
show terse schemas like '{{ source_node.schema }}' in database {{ source_node.database }} limit 1;
{% endset %}
{% if execute %}
{% set schema_exists = run_query(schema_exists_query)|length > 0 %}
{% else %}
{% set schema_exists = false %}
{% endif %}

{% if schema_exists %}
{% set ddl %}
select 'Schema {{ source_node.schema }} exists' from dual;
{% endset %}
{% else %}
{% set fqn %}
{% if source_node.database %}
{{ source_node.database }}.{{ source_node.schema }}
{% else %}
{{ source_node.schema }}
{% endif %}
{% endset %}

{% set ddl %}
create schema if not exists {{ fqn }};
{% endset %}
{% endif %}

{% do return(ddl) %}

{% endmacro %}

0 comments on commit f4536ba

Please sign in to comment.