Skip to content

Commit

Permalink
Merge pull request #1168 from fishtown-analytics/fix/snowflake-drops-…
Browse files Browse the repository at this point in the history
…wrong-backup-type

drop the correct relation type on snowflake when building tables [#1103]
  • Loading branch information
beckjake authored Dec 5, 2018
2 parents d80b378 + 8769118 commit 17f3f24
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
/*
See ../view/view.sql for more information about this relation.
*/

-- drop the backup relation if it exists, then make a new one that uses the old relation's type
{%- set backup_relation = adapter.get_relation(schema=schema, identifier=backup_identifier) -%}
{% if backup_relation is not none -%}
{{ adapter.drop_relation(backup_relation) }}
{%- endif %}
{%- set backup_relation = api.Relation.create(identifier=backup_identifier,
schema=schema, type=(old_relation.type or 'table')) -%}
schema=schema,
type=(old_relation.type or 'table')) -%}

{%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
{%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}
Expand All @@ -23,7 +30,6 @@

-- drop the temp relations if they exists for some reason
{{ adapter.drop_relation(intermediate_relation) }}
{{ adapter.drop_relation(backup_relation) }}

-- setup: if the target relation already exists, truncate or drop it (if it's a view)
{% if non_destructive_mode -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

create view {schema}.materialized as (
select 1 as id
);

create table {schema}.materialized__dbt_backup (
id BIGSERIAL PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

create view {schema}.view_model__dbt_tmp as (
create view {schema}.view__dbt_tmp as (
select 1 as id
);
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def models(self):
return "test/integration/017_runtime_materialization_tests/models"

@attr(type='postgres')
def test_full_refresh(self):
def test_postgres_full_refresh(self):
# initial full-refresh should have no effect
results = self.run_dbt(['run', '--full-refresh'])
self.assertEqual(len(results), 3)
Expand All @@ -42,7 +42,7 @@ def test_full_refresh(self):
self.assertTablesEqual("seed","materialized")

@attr(type='postgres')
def test_non_destructive(self):
def test_postgres_non_destructive(self):
results = self.run_dbt(['run', '--non-destructive'])
self.assertEqual(len(results), 3)

Expand All @@ -62,7 +62,7 @@ def test_non_destructive(self):
self.assertTablesEqual("seed","materialized")

@attr(type='postgres')
def test_full_refresh_and_non_destructive(self):
def test_postgres_full_refresh_and_non_destructive(self):
results = self.run_dbt(['run', '--full-refresh', '--non-destructive'])
self.assertEqual(len(results), 3)

Expand All @@ -84,11 +84,24 @@ def test_full_refresh_and_non_destructive(self):


@attr(type='postgres')
def test_delete__dbt_tmp_relation(self):
def test_postgres_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")
results = self.run_dbt(['run', '--model', 'view'])
self.assertEqual(len(results), 1)

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


@attr(type='snowflake')
def test_snowflake_backup_different_type(self):
self.run_sql_file(
'test/integration/017_runtime_materialization_tests/create_backup_and_original.sql'
)
results = self.run_dbt(['run', '--model', 'materialized'])
self.assertEqual(len(results), 1)

self.assertTableDoesNotExist('materialized__dbt_tmp')
self.assertTableDoesNotExist('materialized__dbt_backup')
self.assertTablesEqual("seed", "materialized")

0 comments on commit 17f3f24

Please sign in to comment.