-
Notifications
You must be signed in to change notification settings - Fork 1
/
optional_table.sql
25 lines (21 loc) · 1019 Bytes
/
optional_table.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
{%- macro optional_table(source_table) -%}
{%- set source_relation = adapter.get_relation(
database=source_table.database,
schema=source_table.schema,
identifier=source_table.name) -%}
{# When source table does not exist, create an empty table in the target schema that will be queried instead. #}
{%- if source_relation is not none -%}
{{ source_table }}
{%- else -%}
{% set query %}
{% if target.type == 'snowflake' %}
create or replace table "{{ target.database }}"."{{ target.schema }}"."{{ source_table.name }}" (dummy int)
{% elif target.type == 'sqlserver' %}
if object_id('"{{ target.database }}"."{{ target.schema }}"."{{ source_table.name }}"', 'U') is null
create table "{{ target.database }}"."{{ target.schema }}"."{{ source_table.name }}" (dummy int)
{% endif %}
{% endset %}
{% do run_query(query) %}
"{{ target.database }}"."{{ target.schema }}"."{{ source_table.name }}"
{%- endif -%}
{%- endmacro -%}