Skip to content

Commit

Permalink
fix var precedence from cli, add tests (#739)
Browse files Browse the repository at this point in the history
* fix var precedence from cli, add tests

* updated from PR
  • Loading branch information
drewbanin authored Apr 26, 2018
1 parent 7046452 commit 0a797af
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
15 changes: 11 additions & 4 deletions dbt/context/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,23 @@ class Var(object):
NoneVarError = "Supplied var '{}' is undefined in config:\nVars supplied "\
"to {} = {}"

def __init__(self, model, context):
def __init__(self, model, context, overrides):
self.model = model
self.context = context

# These are hard-overrides (eg. CLI vars) that should take
# precedence over context-based var definitions
self.overrides = overrides

if isinstance(model, dict) and model.get('unique_id'):
self.local_vars = model.get('config', {}).get('vars')
local_vars = model.get('config', {}).get('vars')
self.model_name = model.get('name')
else:
# still used for wrapping
self.model_name = model.nice_name
self.local_vars = model.config.get('vars', {})
local_vars = model.config.get('vars', {})

self.local_vars = dbt.utils.merge(local_vars, overrides)

def pretty_dict(self, data):
return json.dumps(data, sort_keys=True, indent=4)
Expand Down Expand Up @@ -297,6 +303,7 @@ def generate(model, project, flat_graph, provider=None):
post_hooks = model.get('config', {}).get('post-hook')

db_wrapper = DatabaseWrapper(model, adapter, profile)
cli_var_overrides = project.get('cli_vars', {})

context = dbt.utils.merge(context, {
"adapter": db_wrapper,
Expand Down Expand Up @@ -337,7 +344,7 @@ def generate(model, project, flat_graph, provider=None):

context["write"] = write(model, project.get('target-path'), 'run')
context["render"] = render(context, model)
context["var"] = Var(model, context=context)
context["var"] = Var(model, context=context, overrides=cli_var_overrides)
context['context'] = context

return context
5 changes: 1 addition & 4 deletions dbt/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ def __init__(self, cfg, profiles, profiles_dir, profile_to_load=None,
self.cfg['models']['vars'] = {}

global_vars = dbt.utils.parse_cli_vars(getattr(args, 'vars', '{}'))
if 'vars' not in self.cfg['models']:
self.cfg['models']['vars'] = {}

self.cfg['models']['vars'].update(global_vars)
self.cfg['cli_vars'] = global_vars

def __str__(self):
return pprint.pformat({'project': self.cfg, 'profiles': self.profiles})
Expand Down
6 changes: 6 additions & 0 deletions test/integration/028_cli_vars/models_override/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

test_vars:
constraints:
accepted_values:
- {field: 'field', values: ['override']}

3 changes: 3 additions & 0 deletions test/integration/028_cli_vars/models_override/test_vars.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


select '{{ var("required") }}'::varchar as field
63 changes: 63 additions & 0 deletions test/integration/028_cli_vars/test_cli_var_override.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from nose.plugins.attrib import attr
from test.integration.base import DBTIntegrationTest
import yaml


class TestCLIVarOverride(DBTIntegrationTest):
@property
def schema(self):
return "cli_vars_028"

@property
def models(self):
return "test/integration/028_cli_vars/models_override"

@property
def project_config(self):
return {
"models": {
"vars": {
"required": "present"
}
}
}

@attr(type='postgres')
def test__overriden_vars_global(self):
self.use_default_project()
self.use_profile('postgres')

# This should be "override"
self.run_dbt(["run", "--vars", "{required: override}"])
self.run_dbt(["test"])


class TestCLIVarOverridePorject(DBTIntegrationTest):
@property
def schema(self):
return "cli_vars_028"

@property
def models(self):
return "test/integration/028_cli_vars/models_override"

@property
def project_config(self):
return {
"models": {
"test": {
"vars": {
"required": "present"
}
}
}
}

@attr(type='postgres')
def test__overriden_vars_project_level(self):
self.use_default_project()
self.use_profile('postgres')

# This should be "override"
self.run_dbt(["run", "--vars", "{required: override}"])
self.run_dbt(["test"])
6 changes: 3 additions & 3 deletions test/integration/028_cli_vars/test_cli_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test__cli_vars_longform(self):
}
}
self.run_dbt(["run", "--vars", yaml.dump(cli_vars)])
self.run_dbt(["test"])
self.run_dbt(["test", "--vars", yaml.dump(cli_vars)])


class TestCLIVarsSimple(DBTIntegrationTest):
Expand All @@ -43,12 +43,12 @@ def test__cli_vars_shorthand(self):
self.use_profile('postgres')

self.run_dbt(["run", "--vars", "simple: abc"])
self.run_dbt(["test"])
self.run_dbt(["test", "--vars", "simple: abc"])

@attr(type='postgres')
def test__cli_vars_longer(self):
self.use_default_project()
self.use_profile('postgres')

self.run_dbt(["run", "--vars", "{simple: abc, unused: def}"])
self.run_dbt(["test"])
self.run_dbt(["test", "--vars", "{simple: abc, unused: def}"])

0 comments on commit 0a797af

Please sign in to comment.