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

make ref work like this #530

Merged
merged 2 commits into from
Sep 21, 2017
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
6 changes: 1 addition & 5 deletions dbt/context/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,7 @@ def generate(model, project, flat_graph, provider=None):
"sql_now": adapter.date_function(),
"fromjson": fromjson(model),
"target": target,
"this": dbt.utils.This(
model.get('schema', schema),
dbt.utils.model_immediate_name(model, dbt.flags.NON_DESTRUCTIVE),
model.get('name')
)
"this": dbt.utils.Relation(adapter, model, use_temp=True)
})

context = _add_tracking(context)
Expand Down
8 changes: 2 additions & 6 deletions dbt/context/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ def do_ref(*args):

if dbt.utils.get_materialization(target_model) == 'ephemeral':
model['extra_ctes'][target_model_id] = None
return '__dbt__CTE__{}'.format(target_model.get('name'))
else:
adapter = get_adapter(profile)
table = target_model.get('name')
schema = target_model.get('schema')

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

return do_ref

Expand Down
32 changes: 24 additions & 8 deletions dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import itertools

import dbt.exceptions
import dbt.flags

from dbt.include import GLOBAL_DBT_MODULES_PATH
from dbt.compat import basestring
Expand Down Expand Up @@ -31,17 +32,32 @@ class ExitCodes(object):
UnhandledError = 2


class This(object):
def __init__(self, schema, table, name):
self.schema = schema
self.table = table
self.name = table if name is None else name
class Relation(object):
def __init__(self, adapter, node, use_temp=False):
self._adapter = adapter

def schema_table(self, schema, table):
return '"{}"."{}"'.format(schema, table)
self.node = node
self.schema = node.get('schema')
self.name = node.get('name')

if use_temp:
self.table = self._get_table_name(node)
else:
self.table = self.name

self.materialized = get_materialization(node)
self.sql = node.get('injected_sql')

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

def __repr__(self):
return self.schema_table(self.schema, self.table)
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)


def coalesce(*args):
Expand Down