Skip to content

Commit

Permalink
Snapshot strategies: newline for subquery (#6780)
Browse files Browse the repository at this point in the history
* Snapshot strategies: newline for subquery

* add changie output

* add test for snapshot ending in comment

* remove import to be flake8 compliant

* add seed import

* add newlines for flake8 compliance

* typo fix

* Fixing up a test, adding a comment or two

* removed un-needed test fixtures

* removed even more un-needed fixtures, collapsed test to single class

* removed errant breakpoint()

* Fix a little typo

---------

Co-authored-by: Ian Knox <ian.knox@dbtlabs.com>
Co-authored-by: Mila Page <67295367+VersusFacit@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 11, 2023
1 parent 2971b9a commit b450a57
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230130-231456.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: 'Snapshot strategies: add a newline for subquery'
time: 2023-01-30T23:14:56.131767823+01:00
custom:
Author: RobbertDM
Issue: "6781"
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@
{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %}
{#-- query for proper casing/quoting, to support comparison below --#}
{%- set select_check_cols_from_target -%}
select {{ check_cols_config | join(', ') }} from ({{ node['compiled_code'] }}) subq
{#-- N.B. The whitespace below is necessary to avoid edge case issue with comments --#}
{#-- See: https://github.com/dbt-labs/dbt-core/issues/6781 --#}
select {{ check_cols_config | join(', ') }} from (
{{ node['compiled_code'] }}
) subq
{%- endset -%}
{% set query_columns = get_columns_in_query(select_check_cols_from_target) %}

Expand Down
34 changes: 34 additions & 0 deletions tests/functional/simple_snapshot/test_comment_ending_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from dbt.tests.util import run_dbt
import os

snapshots_with_comment_at_end__snapshot_sql = """
{% snapshot snapshot_actual %}
{{
config(
target_database=var('target_database', database),
target_schema=schema,
unique_key='id',
strategy='check',
check_cols=['email'],
)
}}
select * from {{target.database}}.{{schema}}.seed
-- Test comment to prevent recurrence of https://github.com/dbt-labs/dbt-core/issues/6781
{% endsnapshot %}
"""


class TestSnapshotsWithCommentAtEnd:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_with_comment_at_end__snapshot_sql}

def test_comment_ending(self, project):
path = os.path.join(project.test_data_dir, "seed_pg.sql")
project.run_sql_file(path)
# N.B. Snapshot is run twice to ensure snapshot_check_all_get_existing_columns is fully run
# (it exits early if the table doesn't already exist)
run_dbt(["snapshot"])
results = run_dbt(["snapshot"])
assert len(results) == 1

0 comments on commit b450a57

Please sign in to comment.