Skip to content

Commit

Permalink
Removed table_name from sqlserver__make_temp_relation.
Browse files Browse the repository at this point in the history
re-added incremetal.sql and it works without deprication warning.
  • Loading branch information
mikaelene committed Dec 9, 2019
1 parent 1780a6d commit 25d8469
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
3 changes: 1 addition & 2 deletions dbt/include/sqlserver/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@
{% macro sqlserver__make_temp_relation(base_relation, suffix) %}
{% set tmp_identifier = '#' ~ base_relation.identifier ~ suffix %}
{% set tmp_relation = base_relation.incorporate(
path={"identifier": tmp_identifier},
table_name=tmp_identifier) -%}
path={"identifier": tmp_identifier}) -%}

{% do return(tmp_relation) %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{% macro dbt__incremental_delete(target_relation, tmp_relation) -%}
{%- set unique_key = config.require('unique_key') -%}

delete
from {{ target_relation }}
where ({{ unique_key }}) in (
select ({{ unique_key }})
from {{ tmp_relation.include(schema=False, database=False) }}
);

{%- endmacro %}

{% materialization incremental, adapter='sqlserver' -%}
{%- set unique_key = config.get('unique_key') -%}

{%- set identifier = model['alias'] -%}
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
{%- set target_relation = api.Relation.create(identifier=identifier, schema=schema, database=database, type='table') -%}
{%- set tmp_relation = make_temp_relation(target_relation) %}

{%- set full_refresh_mode = (flags.FULL_REFRESH == True) -%}

{%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
{%- set exists_not_as_table = (old_relation is not none and not old_relation.is_table) -%}

{%- set should_drop = (full_refresh_mode or exists_not_as_table) -%}

-- setup
{% if old_relation is none -%}
-- noop
{%- elif should_drop -%}
{{ adapter.drop_relation(old_relation) }}
{%- set old_relation = none -%}
{%- endif %}

{{ run_hooks(pre_hooks, inside_transaction=False) }}

-- `BEGIN` happens here:
{{ run_hooks(pre_hooks, inside_transaction=True) }}

-- build model
{% if full_refresh_mode or old_relation is none -%}
{%- call statement('main') -%}
{{ create_table_as(False, target_relation, sql) }}
{%- endcall -%}
{%- else -%}
{%- call statement() -%}

{{ dbt.create_table_as(True, tmp_relation, sql) }}

{%- endcall -%}

{{ adapter.expand_target_column_types(from_relation=tmp_relation,
to_relation=target_relation) }}

{%- call statement('main') -%}
{% set dest_columns = adapter.get_columns_in_relation(target_relation) %}
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %}

{% if unique_key is not none -%}

{{ dbt__incremental_delete(target_relation, tmp_relation) }}

{%- endif %}

insert into {{ target_relation }} ({{ dest_cols_csv }})
(
select {{ dest_cols_csv }}
from {{ tmp_relation }}
);
{% endcall %}
{%- endif %}

{{ run_hooks(post_hooks, inside_transaction=True) }}

-- `COMMIT` happens here
{{ adapter.commit() }}

{{ run_hooks(post_hooks, inside_transaction=False) }}

{{ return({'relations': [target_relation]}) }}

{%- endmaterialization %}

0 comments on commit 25d8469

Please sign in to comment.