Skip to content

Commit

Permalink
add Var functor to allow for arbitrary configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Banin committed Sep 20, 2016
1 parent 94b6e82 commit 0abb21c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import defaultdict
import dbt.project
from dbt.source import Source
from dbt.utils import find_model_by_fqn, find_model_by_name, dependency_projects, split_path, This
from dbt.utils import find_model_by_fqn, find_model_by_name, dependency_projects, split_path, This, Var
from dbt.linker import Linker
import time
import sqlparse
Expand Down Expand Up @@ -136,6 +136,7 @@ def compile_model(self, linker, model, models):
context['ref'] = self.__ref(linker, context, model, models)
context['config'] = self.__model_config(model, linker)
context['this'] = This(context['env']['schema'], model.immediate_name, model.name)
context['var'] = Var(model)
context['compiled_at'] = time.strftime('%Y-%m-%d %H:%M:%S')

hook_keys = ['pre-hook', 'post-hook']
Expand Down
7 changes: 5 additions & 2 deletions dbt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from dbt.utils import split_path
import dbt.schema_tester
import dbt.project
from dbt.utils import This
from dbt.utils import This, DBTConfigKeys

class SourceConfig(object):
Materializations = ['view', 'table', 'incremental', 'ephemeral']
ConfigKeys = ['enabled', 'materialized', 'dist', 'sort', 'sql_where', 'unique_key', 'sort_type', 'pre-hook', 'post-hook']
ConfigKeys = DBTConfigKeys

def __init__(self, active_project, own_project, fqn):
self.active_project = active_project
Expand Down Expand Up @@ -201,6 +201,9 @@ def rename_query(self, schema):

return 'alter table "{schema}"."{tmp_name}" rename to "{final_name}"'.format(**opts)

@property
def nice_name(self):
return "{}.{}".format(self.fqn[0], self.fqn[-1])

class Model(DBTSource):
dbt_run_type = 'run'
Expand Down
37 changes: 37 additions & 0 deletions dbt/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@

import os
import dbt.project
import pprint
import json

DBTConfigKeys = [
'enabled',
'materialized',
'dist',
'sort',
'sql_where',
'unique_key',
'sort_type',
'pre-hook',
'post-hook',
'vars'
]

class This(object):
def __init__(self, schema, table, name=None):
Expand All @@ -14,6 +29,28 @@ def schema_table(self, schema, table):
def __repr__(self):
return self.schema_table(self.schema, self.table)

class Var(object):
UndefinedVarError = "Required var '{}' not found in config:\nVars supplied to {} = {}"

def __init__(self, model):
self.model = model
self.local_vars = model.config.get('vars', {})

def compiler_error(self, model, msg):
raise RuntimeError("Compilation error while compiling model {}\n{}".format(model.nice_name, msg))

def pretty_dict(self, data):
return json.dumps(data, sort_keys=True, indent=4)

def __call__(self, var_name, default=None):
if var_name not in self.local_vars and default is None:
pretty_vars = self.pretty_dict(self.local_vars)
self.compiler_error(self.model, self.UndefinedVarError.format(var_name, self.model.nice_name, pretty_vars))
elif var_name in self.local_vars:
return self.local_vars[var_name]
else:
return default

def find_model_by_name(models, name, package_namespace=None):
found = []
for model in models:
Expand Down

0 comments on commit 0abb21c

Please sign in to comment.