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 refs in vars in projects being rendered (#1047) #1048

Merged
merged 2 commits into from
Oct 12, 2018
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
13 changes: 9 additions & 4 deletions dbt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,22 @@ def __init__(self, cli_vars):
self.context['var'] = Var(None, self.context, cli_vars)

@staticmethod
def _is_hook_path(keypath):
def _is_hook_or_model_vars_path(keypath):
if not keypath:
return False

first = keypath[0]
# run hooks
if first in {'on-run-start', 'on-run-end'}:
return True
# model hooks
# models have two things to avoid
if first in {'seeds', 'models'}:
# model-level hooks
if 'pre-hook' in keypath or 'post-hook' in keypath:
return True
# model-level 'vars' declarations
if 'vars' in keypath:
return True

return False

Expand All @@ -126,8 +130,9 @@ def _render_project_entry(self, value, keypath):
:param key str: The key to convert on.
:return Any: The rendered entry.
"""
# hooks should be treated as raw sql, they'll get rendered later
if self._is_hook_path(keypath):
# hooks should be treated as raw sql, they'll get rendered later.
# Same goes for 'vars' declarations inside 'models'/'seeds'.
if self._is_hook_or_model_vars_path(keypath):
return value

return self.render_value(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{
config(
materialized = "view"
)
}}

select gender, count(*) as ct from {{ var('var_ref') }}
group by gender
order by gender asc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ def schema(self):
def models(self):
return "test/integration/003_simple_reference_test/models"

@property
def project_config(self):
return {
'models': {
'vars': {
'var_ref': '{{ ref("view_copy") }}',
}
}
}

@use_profile('postgres')
def test__postgres__simple_reference(self):
self.use_default_project()
Expand All @@ -17,7 +27,7 @@ def test__postgres__simple_reference(self):

results = self.run_dbt()
# ephemeral_copy doesn't show up in results
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)

# Copies should match
self.assertTablesEqual("seed","incremental_copy")
Expand All @@ -29,11 +39,12 @@ def test__postgres__simple_reference(self):
self.assertTablesEqual("summary_expected","materialized_summary")
self.assertTablesEqual("summary_expected","view_summary")
self.assertTablesEqual("summary_expected","ephemeral_summary")
self.assertTablesEqual("summary_expected","view_using_ref")

self.run_sql_file("test/integration/003_simple_reference_test/update.sql")

results = self.run_dbt()
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)

# Copies should match
self.assertTablesEqual("seed","incremental_copy")
Expand All @@ -45,14 +56,15 @@ def test__postgres__simple_reference(self):
self.assertTablesEqual("summary_expected","materialized_summary")
self.assertTablesEqual("summary_expected","view_summary")
self.assertTablesEqual("summary_expected","ephemeral_summary")
self.assertTablesEqual("summary_expected","view_using_ref")

@use_profile('snowflake')
def test__snowflake__simple_reference(self):
self.use_default_project()
self.run_sql_file("test/integration/003_simple_reference_test/seed.sql")

results = self.run_dbt()
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)

# Copies should match
self.assertManyTablesEqual(
Expand All @@ -64,7 +76,7 @@ def test__snowflake__simple_reference(self):
"test/integration/003_simple_reference_test/update.sql")

results = self.run_dbt()
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)

self.assertManyTablesEqual(
["SEED", "INCREMENTAL_COPY", "MATERIALIZED_COPY", "VIEW_COPY"],
Expand Down