diff --git a/dbt/context/common.py b/dbt/context/common.py index 3223ed53f47..776d3b07128 100644 --- a/dbt/context/common.py +++ b/dbt/context/common.py @@ -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) diff --git a/dbt/context/parser.py b/dbt/context/parser.py index 1720c01a46f..bf254bb3636 100644 --- a/dbt/context/parser.py +++ b/dbt/context/parser.py @@ -3,6 +3,8 @@ import dbt.context.common +from dbt.adapters.factory import get_adapter + execute = False @@ -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 diff --git a/dbt/context/runtime.py b/dbt/context/runtime.py index bac4f5c40b3..1a67ab1f5b1 100644 --- a/dbt/context/runtime.py +++ b/dbt/context/runtime.py @@ -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 diff --git a/dbt/parser.py b/dbt/parser.py index f2b2247f176..e2506e59395 100644 --- a/dbt/parser.py +++ b/dbt/parser.py @@ -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) }) diff --git a/dbt/utils.py b/dbt/utils.py index 1b5913e7065..16fb1e585a8 100644 --- a/dbt/utils.py +++ b/dbt/utils.py @@ -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') @@ -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) @@ -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):