Skip to content

Commit

Permalink
Merge pull request #5 from edx/hassan/fix-transaction-issue
Browse files Browse the repository at this point in the history
chore: wrap truncate/insert in a transaction.
  • Loading branch information
HassanJaveed84 authored Aug 1, 2022
2 parents 11228c7 + 7ef6952 commit 92b2ffd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config-version: 2

name: 'snowflake_timetravel_table'
version: '0.0.3'
version: '0.0.4'
2 changes: 2 additions & 0 deletions integration_tests/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
(
"snowflake_timetravel_table_integration_tests_columns_no_change",
(
"begin",
"truncate table",
"insert into",
"commit",
),
),
(
Expand Down
31 changes: 15 additions & 16 deletions macros/snowflake_timetravel_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}


Expand All @@ -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) }}
Expand Down Expand Up @@ -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) %}

Expand Down

0 comments on commit 92b2ffd

Please sign in to comment.