Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always delete the __dbt_tmp relation if exists #511

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@
"{{ col.name }}" {{ col.data_type }} {%- if not loop.last %},{% endif %}
{% endfor -%}
{% endmacro %}


{% macro drop_if_exists(existing, name) %}
{% set existing_type = existing.get(name) %}
{% if existing_type is not none %}
{{ adapter.drop(name, existing_type) }}
{% endif %}
{% endmacro %}
7 changes: 3 additions & 4 deletions dbt/include/global_project/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{%- set existing = adapter.query_for_existing(schema) -%}
{%- set existing_type = existing.get(identifier) -%}

{{ drop_if_exists(existing, tmp_identifier) }}

-- setup
{% if non_destructive_mode -%}
{% if existing_type == 'table' -%}
Expand Down Expand Up @@ -43,10 +45,7 @@
{% if non_destructive_mode -%}
-- noop
{%- else -%}
{%- if existing_type is not none -%}
{{ adapter.drop(identifier, existing_type) }}
{%- endif %}

{{ drop_if_exists(existing, identifier) }}
{{ adapter.rename(tmp_identifier, identifier) }}
{%- endif %}

Expand Down
7 changes: 3 additions & 4 deletions dbt/include/global_project/macros/materializations/view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{%- set existing = adapter.query_for_existing(schema) -%}
{%- set existing_type = existing.get(identifier) -%}

{{ drop_if_exists(existing, tmp_identifier) }}

{{ run_hooks(pre_hooks) }}

-- build model
Expand All @@ -23,10 +25,7 @@
{% if non_destructive_mode and existing_type == 'view' -%}
-- noop
{%- else -%}
{% if existing_type is not none -%}
{{ adapter.drop(identifier, existing_type) }}
{%- endif %}

{{ drop_if_exists(existing, identifier) }}
{{ adapter.rename(tmp_identifier, identifier) }}
{%- endif %}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

create view {schema}.view__dbt_tmp as (
select 1 as id
);
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ def test_full_refresh_and_non_destructive(self):
self.assertTablesEqual("seed","view")
self.assertTablesEqual("seed","incremental")
self.assertTablesEqual("seed","materialized")


@attr(type='postgres')
def test_delete__dbt_tmp_relation(self):
# This creates a __dbt_tmp view - make sure it doesn't interfere with the dbt run
self.run_sql_file("test/integration/017_runtime_materialization_tests/create_view__dbt_tmp.sql")
self.run_dbt(['run', '--model', 'view'])

self.assertTableDoesNotExist('view__dbt_tmp')
self.assertTablesEqual("seed","view")