From 7af01b37e8cc7a358b3cf21db2ac1b9af609e854 Mon Sep 17 00:00:00 2001 From: Anton Huck Date: Mon, 15 Nov 2021 21:26:24 +0100 Subject: [PATCH 1/5] Add Query Tag for Seed --- dbt/include/snowflake/macros/materializations/seed.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dbt/include/snowflake/macros/materializations/seed.sql b/dbt/include/snowflake/macros/materializations/seed.sql index ad9c65c4a..66c7348a9 100644 --- a/dbt/include/snowflake/macros/materializations/seed.sql +++ b/dbt/include/snowflake/macros/materializations/seed.sql @@ -35,3 +35,13 @@ {# Return SQL so we can render it out into the compiled files #} {{ return(statements[0]) }} {% endmacro %} + +{% materialization seed, adapter='snowflake' %} + {% set original_query_tag = set_query_tag() %} + + {% set relations = materialization_seed_default() %} + + {% do unset_query_tag(original_query_tag) %} + + {{ return(relations) }} +{% endmaterialization %} From 4542cd5eb1f082fd005468cf81e2516f20b08bf8 Mon Sep 17 00:00:00 2001 From: Anton Huck Date: Mon, 15 Nov 2021 21:26:44 +0100 Subject: [PATCH 2/5] Add Query Tag for Snapshots --- .../snowflake/macros/materializations/snapshot.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 dbt/include/snowflake/macros/materializations/snapshot.sql diff --git a/dbt/include/snowflake/macros/materializations/snapshot.sql b/dbt/include/snowflake/macros/materializations/snapshot.sql new file mode 100644 index 000000000..c115229cd --- /dev/null +++ b/dbt/include/snowflake/macros/materializations/snapshot.sql @@ -0,0 +1,9 @@ +{% materialization snapshot, adapter='snowflake' %} + {% set original_query_tag = set_query_tag() %} + + {% set relations = materialization_snapshot_default() %} + + {% do unset_query_tag(original_query_tag) %} + + {{ return(relations) }} +{% endmaterialization %} From 2d867b76ffc2798dae9c4a746c66a0ab5f08478d Mon Sep 17 00:00:00 2001 From: Anton Huck Date: Mon, 15 Nov 2021 21:53:36 +0100 Subject: [PATCH 3/5] Add Changelog for #48 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58ff9b466..8385d52c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## dbt-snowflake 1.0.0 (Release TBD) +## dbt-snowflake 1.0.0rc2 (TBD) + +### Fixes +- Apply query tags for Seed and Snapshot materialisations ([#48](https://github.com/dbt-labs/dbt-snowflake/issues/48)) + ## dbt-snowflake 1.0.0rc1 (November 10, 2021) ### Features From 1433a5e727ed3084cd768437e20d6a5cab016d96 Mon Sep 17 00:00:00 2001 From: Anton Huck Date: Tue, 16 Nov 2021 09:56:39 +0100 Subject: [PATCH 4/5] Add Snapshot with Query Tag test --- .../check_query_tag.sql | 16 +++++++++ .../check-snapshots-query-tag/snapshot.sql | 13 +++++++ .../test_snapshot_query_tag.py | 34 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 tests/integration/simple_snapshot_test/check-snapshots-query-tag-expected/check_query_tag.sql create mode 100644 tests/integration/simple_snapshot_test/check-snapshots-query-tag/snapshot.sql create mode 100644 tests/integration/simple_snapshot_test/test_snapshot_query_tag.py diff --git a/tests/integration/simple_snapshot_test/check-snapshots-query-tag-expected/check_query_tag.sql b/tests/integration/simple_snapshot_test/check-snapshots-query-tag-expected/check_query_tag.sql new file mode 100644 index 000000000..b193b40c4 --- /dev/null +++ b/tests/integration/simple_snapshot_test/check-snapshots-query-tag-expected/check_query_tag.sql @@ -0,0 +1,16 @@ + + +with query as ( + + -- check that the current value for id=1 is red + select case when ( + select count(*) + from table(information_schema.query_history_by_user()) + where QUERY_TAG = '{{ var('query_tag') }}' + ) > 1 then 0 else 1 end as failures + +) + +select * +from query +where failures = 1 diff --git a/tests/integration/simple_snapshot_test/check-snapshots-query-tag/snapshot.sql b/tests/integration/simple_snapshot_test/check-snapshots-query-tag/snapshot.sql new file mode 100644 index 000000000..6f467cfc2 --- /dev/null +++ b/tests/integration/simple_snapshot_test/check-snapshots-query-tag/snapshot.sql @@ -0,0 +1,13 @@ +{% snapshot snapshot_query_tag %} + {{ + config( + target_database=database, + target_schema=schema, + unique_key='id', + strategy='check', + check_cols=['color'], + query_tag=var('query_tag') + ) + }} + select * from {{target.database}}.{{schema}}.seed +{% endsnapshot %} diff --git a/tests/integration/simple_snapshot_test/test_snapshot_query_tag.py b/tests/integration/simple_snapshot_test/test_snapshot_query_tag.py new file mode 100644 index 000000000..f96b33bff --- /dev/null +++ b/tests/integration/simple_snapshot_test/test_snapshot_query_tag.py @@ -0,0 +1,34 @@ +from tests.integration.base import DBTIntegrationTest, use_profile + +class TestSnapshotWithQueryTag(DBTIntegrationTest): + @property + def schema(self): + return "simple_snapshot_004" + + @property + def models(self): + return "models" + + @property + def project_config(self): + return { + 'config-version': 2, + "snapshot-paths": ['check-snapshots-query-tag'], + "test-paths": ['check-snapshots-query-tag-expected'], + "model-paths": [], + } + + def dbt_run_seed(self): + self.run_sql_file('seed.sql') + + def test_snapshot_with_query_tag(self): + self.run_dbt(["snapshot", "--vars", '{{"query_tag": {}}}'.format(self.prefix)]) + + def assert_query_tag_expected(self): + self.run_dbt(['test', '--select', 'test_type:singular', '--vars', '{{"query_tag": {}}}'.format(self.prefix)]) + + @use_profile('snowflake') + def test__snowflake__snapshot_with_query_tag(self): + self.dbt_run_seed() + self.test_snapshot_with_query_tag() + self.assert_query_tag_expected() From 7acfbd11e028440e2a47c3de58aa9c87f098bcd0 Mon Sep 17 00:00:00 2001 From: Anton Huck Date: Tue, 16 Nov 2021 10:05:48 +0100 Subject: [PATCH 5/5] Add Seed with Query Tag test --- .../check_query_tag.sql | 16 ++++++++ .../test_seed_with_query_tag.py | 41 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/integration/simple_seed_test/check-query-tag-expected/check_query_tag.sql create mode 100644 tests/integration/simple_seed_test/test_seed_with_query_tag.py diff --git a/tests/integration/simple_seed_test/check-query-tag-expected/check_query_tag.sql b/tests/integration/simple_seed_test/check-query-tag-expected/check_query_tag.sql new file mode 100644 index 000000000..b193b40c4 --- /dev/null +++ b/tests/integration/simple_seed_test/check-query-tag-expected/check_query_tag.sql @@ -0,0 +1,16 @@ + + +with query as ( + + -- check that the current value for id=1 is red + select case when ( + select count(*) + from table(information_schema.query_history_by_user()) + where QUERY_TAG = '{{ var('query_tag') }}' + ) > 1 then 0 else 1 end as failures + +) + +select * +from query +where failures = 1 diff --git a/tests/integration/simple_seed_test/test_seed_with_query_tag.py b/tests/integration/simple_seed_test/test_seed_with_query_tag.py new file mode 100644 index 000000000..5d58b5a87 --- /dev/null +++ b/tests/integration/simple_seed_test/test_seed_with_query_tag.py @@ -0,0 +1,41 @@ +import os +import csv +from tests.integration.base import DBTIntegrationTest, use_profile + +class TestSeedWithQueryTag(DBTIntegrationTest): + @property + def schema(self): + return "simple_seed" + + @property + def models(self): + return "models" + + @property + def project_config(self): + return { + 'config-version': 2, + "seed-paths": ['seeds-config'], + "test-paths": ['check-query-tag-expected'], + 'seeds': { + 'test': { + 'enabled': False, + 'quote_columns': True, + 'query_tag': self.prefix, + 'seed_enabled': { + 'enabled': True, + }, + }, + + } + } + + def assert_query_tag_expected(self): + self.run_dbt(['test', '--select', 'test_type:singular', '--vars', '{{"query_tag": {}}}'.format(self.prefix)]) + + @use_profile('snowflake') + def test_snowflake_big_batched_seed(self): + results = self.run_dbt(["seed"]) + self.assertEqual(len(results), 1) + self.assert_query_tag_expected() + \ No newline at end of file