diff --git a/dbt/include/global_project/macros/materializations/helpers.sql b/dbt/include/global_project/macros/materializations/helpers.sql index d81c1c046f3..e717a4457ee 100644 --- a/dbt/include/global_project/macros/materializations/helpers.sql +++ b/dbt/include/global_project/macros/materializations/helpers.sql @@ -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 %} diff --git a/dbt/include/global_project/macros/materializations/table.sql b/dbt/include/global_project/macros/materializations/table.sql index eb15b989ff3..b4414ab4337 100644 --- a/dbt/include/global_project/macros/materializations/table.sql +++ b/dbt/include/global_project/macros/materializations/table.sql @@ -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' -%} @@ -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 %} diff --git a/dbt/include/global_project/macros/materializations/view.sql b/dbt/include/global_project/macros/materializations/view.sql index eeb0c181e3c..cdccde54c4c 100644 --- a/dbt/include/global_project/macros/materializations/view.sql +++ b/dbt/include/global_project/macros/materializations/view.sql @@ -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 @@ -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 %} diff --git a/test/integration/017_runtime_materialization_tests/create_view__dbt_tmp.sql b/test/integration/017_runtime_materialization_tests/create_view__dbt_tmp.sql new file mode 100644 index 00000000000..eeb4d2bca2d --- /dev/null +++ b/test/integration/017_runtime_materialization_tests/create_view__dbt_tmp.sql @@ -0,0 +1,4 @@ + +create view {schema}.view__dbt_tmp as ( + select 1 as id +); diff --git a/test/integration/017_runtime_materialization_tests/test_runtime_materialization.py b/test/integration/017_runtime_materialization_tests/test_runtime_materialization.py index d4b4107ccab..a2acbad9b8e 100644 --- a/test/integration/017_runtime_materialization_tests/test_runtime_materialization.py +++ b/test/integration/017_runtime_materialization_tests/test_runtime_materialization.py @@ -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")