Skip to content

Commit

Permalink
Fix: Support individual query tag configuration for Seeds and Snapsho…
Browse files Browse the repository at this point in the history
…ts (#48)

* Add Query Tag for Seed

* Add Query Tag for Snapshots

* Add Changelog for #48

* Add Snapshot with Query Tag test

* Add Seed with Query Tag test
  • Loading branch information
anthu committed Nov 19, 2021
1 parent 47965fe commit 9194552
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
10 changes: 10 additions & 0 deletions dbt/include/snowflake/macros/materializations/seed.sql
Expand Up @@ -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 %}
9 changes: 9 additions & 0 deletions 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 %}
@@ -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
41 changes: 41 additions & 0 deletions 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()

@@ -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
@@ -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 %}
34 changes: 34 additions & 0 deletions 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()

0 comments on commit 9194552

Please sign in to comment.