Skip to content

Commit

Permalink
Be less clever. Update create_or_replace_view()
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcohen6 committed Jul 21, 2021
1 parent 84d8964 commit 3fa9b97
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
and DBT_INTERNAL_SOURCE.dbt_change_type = 'insert'
then insert ({{ insert_cols_csv }})
values ({{ insert_cols_csv }})
;

{% endmacro %}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% endmacro %}

{% macro default__handle_existing_table(full_refresh, old_relation) %}
{{ log("Dropping relation " ~ old_relation ~ " because it is of type " ~ old_relation.type) }}
{{ adapter.drop_relation(old_relation) }}
{% endmacro %}

Expand All @@ -19,7 +20,7 @@
*/
#}
{% macro create_or_replace_view(run_outside_transaction_hooks=True) %}
{% macro create_or_replace_view() %}
{%- set identifier = model['alias'] -%}
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
Expand All @@ -30,13 +31,7 @@
identifier=identifier, schema=schema, database=database,
type='view') -%}
{% if run_outside_transaction_hooks %}
-- no transactions on BigQuery
{{ run_hooks(pre_hooks, inside_transaction=False) }}
{% endif %}
-- `BEGIN` happens here on Snowflake
{{ run_hooks(pre_hooks, inside_transaction=True) }}
{{ run_hooks(pre_hooks) }}
-- If there's a table with the same name and we weren't told to full refresh,
-- that's an error. If we were told to full refresh, drop it. This behavior differs
Expand All @@ -50,14 +45,7 @@
{{ create_view_as(target_relation, sql) }}
{%- endcall %}

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

{{ adapter.commit() }}

{% if run_outside_transaction_hooks %}
-- No transactions on BigQuery
{{ run_hooks(post_hooks, inside_transaction=False) }}
{% endif %}
{{ run_hooks(post_hooks) }}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


{% materialization view, adapter='bigquery' -%}
{% set to_return = create_or_replace_view(run_outside_transaction_hooks=False) %}
{% set to_return = create_or_replace_view() %}

{% set target_relation = this.incorporate(type='view') %}
{% do persist_docs(target_relation, model) %}
Expand Down
8 changes: 6 additions & 2 deletions plugins/snowflake/dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@


{% macro snowflake_dml_explicit_transaction(dml) %}

{#
Use this macro to wrap all INSERT, MERGE, UPDATE, DELETE, and TRUNCATE
statements before passing them into run_query(), or calling in the 'main' statement
of a materialization
#}
{% set dml_transaction %}
begin;
{{ dml }} {{ ";" if not (dml | trim).endswith(";") }}
{{ dml }};
commit;
{% endset %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(
select {{ dest_cols_csv }}
from {{ source_sql }}
);
)

{%- else -%}

Expand Down

0 comments on commit 3fa9b97

Please sign in to comment.