|
1 |
| - |
2 | 1 | {% materialization incremental, adapter='fabric' -%}
|
3 | 2 |
|
4 | 3 | {%- set full_refresh_mode = (should_full_refresh()) -%}
|
5 | 4 | {% set target_relation = this.incorporate(type='table') %}
|
6 |
| - {%- set relations_list = fabric__get_relation_without_caching(target_relation) -%} |
| 5 | + {%- set relation = load_cached_relation(this) -%} |
7 | 6 |
|
8 | 7 | {%- set existing_relation = none %}
|
9 |
| - {% if (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) |
10 |
| - and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] == target_relation.type)%} |
| 8 | + {% if relation.type == 'table' %} |
11 | 9 | {% set existing_relation = target_relation %}
|
12 |
| - {% elif (relations_list|length == 1) and (relations_list[0][2] == target_relation.schema) |
13 |
| - and (relations_list[0][1] == target_relation.identifier) and (relations_list[0][3] != target_relation.type) %} |
14 |
| - {% set existing_relation = get_or_create_relation(relations_list[0][0], relations_list[0][2] , relations_list[0][1] , relations_list[0][3])[1] %} |
| 10 | + {% elif relation.type == 'view' %} |
| 11 | + {% set existing_relation = get_or_create_relation(relation.database, relation.schema, relation.identifier, relation.type)[1] %} |
15 | 12 | {% endif %}
|
16 | 13 |
|
17 |
| - {{ log("Full refresh mode" ~ full_refresh_mode)}} |
18 |
| - {{ log("existing relation : "~existing_relation ~ " type "~ existing_relation.type ~ " is view? "~existing_relation.is_view) }} |
19 |
| - {{ log("target relation: " ~target_relation ~ " type "~ target_relation.type ~ " is view? "~target_relation.is_view) }} |
20 |
| - |
21 | 14 | -- configs
|
22 | 15 | {%- set unique_key = config.get('unique_key') -%}
|
23 | 16 | {% set incremental_strategy = config.get('incremental_strategy') or 'default' %}
|
|
28 | 21 |
|
29 | 22 | {{ run_hooks(pre_hooks, inside_transaction=True) }}
|
30 | 23 |
|
31 |
| - {% if existing_relation is none %} |
| 24 | + -- naming a temp relation |
| 25 | + {% set tmp_relation_view = target_relation.incorporate(path={"identifier": target_relation.identifier ~ '__dbt_tmp_vw'}, type='view')-%} |
32 | 26 |
|
| 27 | + -- Fabric & Synapse adapters use temp relation because of lack of CTE support for CTE in CTAS, Insert |
| 28 | + -- drop temp relation if exists |
| 29 | + {% do adapter.drop_relation(tmp_relation_view) %} |
| 30 | + |
| 31 | + {% if existing_relation is none %} |
33 | 32 | {%- call statement('main') -%}
|
34 |
| - {{ fabric__create_table_as(False, target_relation, sql)}} |
| 33 | + {{ get_create_table_as_sql(False, target_relation, sql)}} |
35 | 34 | {%- endcall -%}
|
36 | 35 |
|
37 | 36 | {% elif existing_relation.is_view %}
|
38 |
| - |
39 | 37 | {#-- Can't overwrite a view with a table - we must drop --#}
|
40 | 38 | {{ log("Dropping relation " ~ target_relation ~ " because it is a view and this model is a table.") }}
|
41 |
| - {{ drop_relation_if_exists(existing_relation) }} |
| 39 | + {% do adapter.drop_relation(existing_relation) %} |
| 40 | + |
42 | 41 | {%- call statement('main') -%}
|
43 |
| - {{ fabric__create_table_as(False, target_relation, sql)}} |
| 42 | + {{ get_create_table_as_sql(False, target_relation, sql)}} |
44 | 43 | {%- endcall -%}
|
45 | 44 |
|
46 | 45 | {% elif full_refresh_mode %}
|
47 |
| - |
48 | 46 | {%- call statement('main') -%}
|
49 |
| - {{ fabric__create_table_as(False, target_relation, sql)}} |
| 47 | + {{ get_create_table_as_sql(False, target_relation, sql)}} |
50 | 48 | {%- endcall -%}
|
51 | 49 |
|
52 | 50 | {% else %}
|
53 |
| - |
54 | 51 | {%- call statement('create_tmp_relation') -%}
|
55 |
| - {{ fabric__create_table_as(True, temp_relation, sql)}} |
| 52 | + {{ get_create_table_as_sql(True, temp_relation, sql)}} |
56 | 53 | {%- endcall -%}
|
57 | 54 | {% do adapter.expand_target_column_types(
|
58 |
| - from_relation=temp_relation, |
59 |
| - to_relation=target_relation) %} |
| 55 | + from_relation=temp_relation, |
| 56 | + to_relation=target_relation) %} |
60 | 57 | {#-- Process schema changes. Returns dict of changes if successful. Use source columns for upserting/merging --#}
|
61 | 58 | {% set dest_columns = process_schema_changes(on_schema_change, temp_relation, existing_relation) %}
|
62 | 59 | {% if not dest_columns %}
|
|
72 | 69 | {%- endcall -%}
|
73 | 70 | {% endif %}
|
74 | 71 |
|
75 |
| - {% do drop_relation_if_exists(temp_relation) %} |
| 72 | + {% do adapter.drop_relation(tmp_relation_view) %} |
| 73 | + {% do adapter.drop_relation(temp_relation) %} |
76 | 74 | {{ run_hooks(post_hooks, inside_transaction=True) }}
|
77 |
| - |
78 | 75 | {% set target_relation = target_relation.incorporate(type='table') %}
|
79 |
| - |
80 | 76 | {% set should_revoke = should_revoke(existing_relation, full_refresh_mode) %}
|
81 | 77 | {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
|
82 |
| - |
83 | 78 | {% do persist_docs(target_relation, model) %}
|
84 | 79 | {% do adapter.commit() %}
|
85 | 80 | {{ return({'relations': [target_relation]}) }}
|
|
0 commit comments