Skip to content

Commit

Permalink
Fix/env vars (#543)
Browse files Browse the repository at this point in the history
* fix for bad env_var exception

* overwrite target with compiled values

* fixes env vars, adds test. Auto-compile profile/target args
  • Loading branch information
drewbanin authored Sep 29, 2017
1 parent fd17c2e commit e40ed43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def invoke_dbt(parsed):
targets = proj.cfg.get('outputs', {}).keys()
if parsed.target in targets:
proj.cfg['target'] = parsed.target
# make sure we update the target if this is overriden on the cli
proj.compile_and_update_target()
else:
logger.info("Encountered an error while reading the project:")
logger.info(" ERROR Specified target {} is not a valid option "
Expand Down
9 changes: 7 additions & 2 deletions dbt/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __init__(self, cfg, profiles, profiles_dir, profile_to_load=None,

if self.profile_to_load in self.profiles:
self.cfg.update(self.profiles[self.profile_to_load])
self.compile_and_update_target()

else:
raise DbtProjectError(
"Could not find profile named '{}'"
Expand Down Expand Up @@ -120,15 +122,18 @@ def compile_target(self, target_cfg):
is_str = isinstance(value, dbt.compat.basestring)

if is_str:
node = "config key: '{}'".format(key)
compiled_val = dbt.clients.jinja.get_rendered(value, ctx, node)
compiled_val = dbt.clients.jinja.get_rendered(value, ctx)
else:
compiled_val = value

compiled[key] = compiled_val

return compiled

def compile_and_update_target(self):
target = self.cfg['target']
self.cfg['outputs'][target].update(self.run_environment())

def run_environment(self):
target_name = self.cfg['target']
if target_name in self.cfg['outputs']:
Expand Down
14 changes: 10 additions & 4 deletions test/integration/013_context_var_tests/test_context_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def setUp(self):
DBTIntegrationTest.setUp(self)
os.environ["DBT_TEST_013_ENV_VAR"] = "1"

os.environ["DBT_TEST_013_USER"] = "root"
os.environ["DBT_TEST_013_PASS"] = "password"

self.fields = [
'this',
'this.name',
Expand Down Expand Up @@ -42,13 +45,15 @@ def profile_config(self):
return {
'test': {
'outputs': {
# don't use env_var's here so the integration tests can run
# seed sql statements and the like. default target is used
'dev': {
'type': 'postgres',
'threads': 1,
'host': 'database',
'port': 5432,
'user': 'root',
'pass': 'password',
'user': "root",
'pass': "password",
'dbname': 'dbt',
'schema': self.unique_schema()
},
Expand All @@ -57,8 +62,9 @@ def profile_config(self):
'threads': 1,
'host': 'database',
'port': 5432,
'user': 'root',
'pass': 'password',
# root/password
'user': "{{ env_var('DBT_TEST_013_USER') }}",
'pass': "{{ env_var('DBT_TEST_013_PASS') }}",
'dbname': 'dbt',
'schema': self.unique_schema()
}
Expand Down

0 comments on commit e40ed43

Please sign in to comment.