From 7ef695293d8c9f82fd3967748636d27f83780222 Mon Sep 17 00:00:00 2001 From: Hassan Javeed Date: Fri, 29 Jul 2022 02:04:33 +0500 Subject: [PATCH] chore: wrap truncate/insert in a transaction. --- dbt_project.yml | 2 +- integration_tests/tests/test_integration.py | 2 ++ macros/snowflake_timetravel_table.sql | 31 ++++++++++----------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/dbt_project.yml b/dbt_project.yml index 9810679..2733c12 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -1,4 +1,4 @@ config-version: 2 name: 'snowflake_timetravel_table' -version: '0.0.3' +version: '0.0.4' diff --git a/integration_tests/tests/test_integration.py b/integration_tests/tests/test_integration.py index 203cf98..c0b41b5 100644 --- a/integration_tests/tests/test_integration.py +++ b/integration_tests/tests/test_integration.py @@ -29,8 +29,10 @@ ( "snowflake_timetravel_table_integration_tests_columns_no_change", ( + "begin", "truncate table", "insert into", + "commit", ), ), ( diff --git a/macros/snowflake_timetravel_table.sql b/macros/snowflake_timetravel_table.sql index 299e5b6..4031ad0 100644 --- a/macros/snowflake_timetravel_table.sql +++ b/macros/snowflake_timetravel_table.sql @@ -44,12 +44,19 @@ -- dest_columns (list of dict): -- List of column details for the relation, in the form returned by adapter.get_columns_in_relation(). #} - truncate table {{ target_relation }}; + {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%} - insert into {{ target_relation }} ({{ dest_cols_csv }}) - ( - {{ sql }} - ); + + {%- set dml -%} + truncate table {{ target_relation }}; + insert into {{ target_relation }} ({{ dest_cols_csv }}) + ( + {{ sql }} + ) + {%- endset -%} + + {% do return(snowflake_dml_explicit_transaction(dml)) %} + {%- endmacro %} @@ -71,15 +78,12 @@ schema=schema, database=database, type='table') -%} - {{ run_hooks(pre_hooks, inside_transaction=False) }} - - -- `BEGIN` happens here: - {{ run_hooks(pre_hooks, inside_transaction=True) }} + {{ run_hooks(pre_hooks) }} {% if old_relation is none %} {% set build_sql = create_table_as(false, target_relation, sql) %} {% else %} - {% if not old_relation.is_table %} + {% if old_relation is not none and not old_relation.is_table %} {#-- Drop the relation if it was a view to "convert" it in a table. This may lead to -- downtime, but it should be a relatively infrequent occurrence #} {{ log("Dropping relation " ~ old_relation ~ " because it is of type " ~ old_relation.type) }} @@ -119,12 +123,7 @@ {{ build_sql }} {%- endcall %} - {{ run_hooks(post_hooks, inside_transaction=True) }} - - -- `COMMIT` happens here - {{ adapter.commit() }} - - {{ run_hooks(post_hooks, inside_transaction=False) }} + {{ run_hooks(post_hooks) }} {% do persist_docs(target_relation, model) %}