Skip to content

Commit

Permalink
fix package-defined schema test macros (#562)
Browse files Browse the repository at this point in the history
* fix package-defined schema test macros

* create a dummy Relation in parsing

* fix for bq quoting (#565)
  • Loading branch information
cmcarthur authored and drewbanin committed Oct 20, 2017
1 parent 3489e90 commit a552e3a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dbt/context/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def generate(model, project, flat_graph, provider=None):
"sql_now": adapter.date_function(),
"fromjson": fromjson(model),
"target": target,
"this": dbt.utils.Relation(adapter, model, use_temp=True)
"this": dbt.utils.Relation(profile, adapter, model, use_temp=True)
})

context = _add_tracking(context)
Expand Down
5 changes: 5 additions & 0 deletions dbt/context/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import dbt.context.common

from dbt.adapters.factory import get_adapter


execute = False

Expand All @@ -16,6 +18,9 @@ def ref(*args):
else:
dbt.exceptions.ref_invalid_args(model, args)

adapter = get_adapter(profile)
return dbt.utils.Relation(profile, adapter, model)

return ref


Expand Down
2 changes: 1 addition & 1 deletion dbt/context/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def do_ref(*args):
model['extra_ctes'][target_model_id] = None

adapter = get_adapter(profile)
return dbt.utils.Relation(adapter, target_model)
return dbt.utils.Relation(profile, adapter, target_model)

return do_ref

Expand Down
9 changes: 8 additions & 1 deletion dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,16 @@ def parse_schema_test(test_base, model_name, test_config, test_type,
# sort the dict so the keys are rendered deterministically (for tests)
kwargs = [as_kwarg(key, test_args[key]) for key in sorted(test_args)]

macro_name = "test_{}".format(test_type)

if package_project_config.get('name') != \
root_project_config.get('name'):
macro_name = "{}.{}".format(package_project_config.get('name'),
macro_name)

raw_sql = "{{{{ {macro}(model=ref('{model}'), {kwargs}) }}}}".format(**{
'model': model_name,
'macro': "test_{}".format(test_type),
'macro': macro_name,
'kwargs': ", ".join(kwargs)
})

Expand Down
27 changes: 18 additions & 9 deletions dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class ExitCodes(object):


class Relation(object):
def __init__(self, adapter, node, use_temp=False):
self._adapter = adapter

def __init__(self, profile, adapter, node, use_temp=False):
self.node = node
self.schema = node.get('schema')
self.name = node.get('name')
Expand All @@ -48,6 +46,21 @@ def __init__(self, adapter, node, use_temp=False):
self.materialized = get_materialization(node)
self.sql = node.get('injected_sql')

self.do_quote = self._get_quote_function(profile, adapter)

def _get_quote_function(self, profile, adapter):

# make a closure so we don't need to store the profile
# on the `Relation` object. That shouldn't be accessible in user-land
def quote(schema, table):
return adapter.quote_schema_and_table(
profile=profile,
schema=schema,
table=table
)

return quote

def _get_table_name(self, node):
return model_immediate_name(node, dbt.flags.NON_DESTRUCTIVE)

Expand All @@ -56,17 +69,13 @@ def final_name(self):
msg = "final_name() was called on an ephemeral model"
dbt.exceptions.raise_compiler_error(msg, self.node)
else:
return self._adapter.quote_schema_and_table(profile=None,
schema=self.schema,
table=self.name)
return self.do_quote(self.schema, self.name)

def __repr__(self):
if self.materialized == 'ephemeral':
return '__dbt__CTE__{}'.format(self.name)
else:
return self._adapter.quote_schema_and_table(profile=None,
schema=self.schema,
table=self.table)
return self.do_quote(self.schema, self.table)


def coalesce(*args):
Expand Down

0 comments on commit a552e3a

Please sign in to comment.