Skip to content

Commit

Permalink
Updating drop relation
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed May 24, 2024
1 parent f02a176 commit 709d648
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions dbt/include/synapse/macros/adapters/columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
path={"identifier": relation.identifier.replace("#", "") ~ '_tmp_tbl_hack'},
type='table')-%}

{% do drop_relation(tmp_tbl_hack) %}
{% do adapter.drop_relation(tmp_tbl_hack) %}
{% set sql_create %}
CREATE TABLE {{ tmp_tbl_hack }}
WITH(
Expand All @@ -23,7 +23,7 @@
{% call statement() -%} {{ sql_create }} {%- endcall %}

{% set output = get_columns_in_relation(tmp_tbl_hack) %}
{% do drop_relation(tmp_tbl_hack) %}
{% do adapter.drop_relation(tmp_tbl_hack) %}
{{ return(output) }}
{% endif %}

Expand Down
1 change: 0 additions & 1 deletion dbt/include/synapse/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{% endif %}
if object_id ('{{ relation }}','{{ object_id_type }}') is not null
drop {{ relation.type }} {{ relation }}
{% endif %}
{% endmacro %}

{% macro synapse__rename_relation(from_relation, to_relation) -%}
Expand Down
7 changes: 4 additions & 3 deletions dbt/include/synapse/macros/adapters/replace.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@
{{ get_create_intermediate_sql(target_relation, sql) }};
{{ get_create_backup_sql(existing_relation) }};
{{ get_rename_intermediate_sql(target_relation) }};
{{ synapse__drop_relation(existing_relation) }}
{% do adapter.drop_relation(existing_relation) %}

{# /* create target_relation as an intermediate relation, then swap it out with the existing one without using a backup */ #}
{%- elif target_relation.can_be_renamed -%}
{{ get_create_intermediate_sql(target_relation, sql) }};
{{ synapse__drop_relation(existing_relation) }};
{% do adapter.drop_relation(existing_relation) %}
{{ get_rename_intermediate_sql(target_relation) }}

{# /* create target_relation in place by first backing up the existing relation */ #}
{%- elif existing_relation.can_be_renamed -%}
{{ get_create_backup_sql(existing_relation) }};
{{ get_create_sql(target_relation, sql) }};
{{ synapse__drop_relation(existing_relation) }}
{% do adapter.drop_relation(existing_relation) %}

{# /* no renaming is allowed, so just drop and create */ #}
{%- else -%}
{{ synapse__drop_relation(existing_relation) }};
{% do adapter.drop_relation(existing_relation) %}
{{ get_create_sql(target_relation, sql) }}

{%- endif -%}
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/synapse/macros/adapters/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
identifier=row[1],
type=row[3]
) -%}
{% do drop_relation(schema_relation) %}
{% do adapter.drop_relation(schema_relation) %}
{%- endfor %}

{% call statement('drop_schema') -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
they return none

-- drop the temp relations if they exist already in the database
{{ synapse__drop_relation(preexisting_backup_relation) }}
{{ synapse__drop_relation(preexisting_intermediate_relation) }}
{% do adapter.drop_relation(preexisting_backup_relation) %}
{% do adapter.drop_relation(preexisting_intermediate_relation) %}


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

Expand All @@ -43,8 +44,9 @@
{% macro materialized_view_teardown(backup_relation, intermediate_relation, post_hooks) %}

-- drop the temp relations if they exist to leave the database clean for the next run
{{ synapse__drop_relation_script(backup_relation) }}
{{ synapse__drop_relation_script(intermediate_relation) }}

{% do adapter.drop_relation(backup_relation) %}
{% do adapter.drop_relation(intermediate_relation) %}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
type='view')-%}
{%- set temp_view_sql = sql.replace("'", "''") -%}

{{ synapse__drop_relation_script(tmp_relation) }}

{{ synapse__drop_relation_script(relation) }}
{% do adapter.drop_relation(tmp_relation) %}
{% do adapter.drop_relation(relation) %}

{{ synapse__create_view_as(tmp_relation, sql) }}

Expand Down Expand Up @@ -39,6 +38,6 @@
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}]WITH(DISTRIBUTION = {{dist}},{{index}}) AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
{% endif %}

{{ synapse__drop_relation_script(tmp_relation) }}
{% do adapter.drop_relation(tmp_relation) %}

{% endmacro %}
11 changes: 11 additions & 0 deletions test.env copy.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SYNAPSE_TEST_DRIVER=ODBC Driver 18 for SQL Server
SYNAPSE_TEST_HOST=pvenkat-test-ws.sql.azuresynapse.net
SYNAPSE_TEST_USER=
SYNAPSE_TEST_PASS=
SYNAPSE_TEST_PORT=1433
SYNAPSE_TEST_DWH_NAME=testsqlpool
DBT_TEST_USER_1=DBT_TEST_USER_1
DBT_TEST_USER_2=DBT_TEST_USER_2
DBT_TEST_USER_3=DBT_TEST_USER_3
DBT_TEST_AAD_PRINCIPAL_1=DBT_TEST_AAD_PRINCIPAL_1
DBT_TEST_AAD_PRINCIPAL_2=DBT_TEST_AAD_PRINCIPAL_2

0 comments on commit 709d648

Please sign in to comment.