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

Fix: Support individual query tag configuration for Seeds and Snapshots #48

Merged
merged 6 commits into from
Nov 19, 2021
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -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()