Skip to content

Commit

Permalink
add pre_hook/post_hook kwargs to config, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Nov 21, 2018
1 parent 029ef17 commit e543dc4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
9 changes: 8 additions & 1 deletion dbt/context/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ def __init__(self, model, source_config):
self.model = model
self.source_config = source_config

@staticmethod
def _transform_kwargs(kwargs):
for k in ('pre_hook', 'post_hook'):
if k in kwargs:
kwargs[k.replace('_', '-')] = kwargs.pop(k)
return kwargs

def __call__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0:
opts = args[0]
elif len(args) == 0 and len(kwargs) > 0:
opts = kwargs
opts = self._transform_kwargs(kwargs)
else:
dbt.exceptions.raise_compiler_error(
"Invalid inline model config",
Expand Down
63 changes: 63 additions & 0 deletions test/integration/014_hook_tests/kwargs-models/hooks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

{{
config(
pre_hook="\
insert into {{this.schema}}.on_model_hook (\
\"state\",\
\"target.dbname\",\
\"target.host\",\
\"target.name\",\
\"target.schema\",\
\"target.type\",\
\"target.user\",\
\"target.pass\",\
\"target.port\",\
\"target.threads\",\
\"run_started_at\",\
\"invocation_id\"\
) VALUES (\
'start',\
'{{ target.dbname }}',\
'{{ target.host }}',\
'{{ target.name }}',\
'{{ target.schema }}',\
'{{ target.type }}',\
'{{ target.user }}',\
'{{ target.pass }}',\
{{ target.port }},\
{{ target.threads }},\
'{{ run_started_at }}',\
'{{ invocation_id }}'\
)",
post_hook="\
insert into {{this.schema}}.on_model_hook (\
\"state\",\
\"target.dbname\",\
\"target.host\",\
\"target.name\",\
\"target.schema\",\
\"target.type\",\
\"target.user\",\
\"target.pass\",\
\"target.port\",\
\"target.threads\",\
\"run_started_at\",\
\"invocation_id\"\
) VALUES (\
'end',\
'{{ target.dbname }}',\
'{{ target.host }}',\
'{{ target.name }}',\
'{{ target.schema }}',\
'{{ target.type }}',\
'{{ target.user }}',\
'{{ target.pass }}',\
{{ target.port }},\
{{ target.threads }},\
'{{ run_started_at }}',\
'{{ invocation_id }}'\
)"
)
}}

select 3 as id
14 changes: 10 additions & 4 deletions test/integration/014_hook_tests/test_model_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def models(self):
return "test/integration/014_hook_tests/models"

@attr(type='postgres')
def test_pre_and_post_model_hooks(self):
def test_postgres_pre_and_post_model_hooks(self):
self.run_dbt(['run'])

self.check_hooks('start')
Expand Down Expand Up @@ -177,7 +177,7 @@ def project_config(self):
}

@attr(type='postgres')
def test_hooks_on_seeds(self):
def test_postgres_hooks_on_seeds(self):
res = self.run_dbt(['seed'])
self.assertEqual(len(res), 1, 'Expected exactly one item')
res = self.run_dbt(['test'])
Expand All @@ -196,14 +196,14 @@ def models(self):
return "test/integration/014_hook_tests/configured-models"

@attr(type='postgres')
def test_pre_and_post_model_hooks_model(self):
def test_postgres_pre_and_post_model_hooks_model(self):
self.run_dbt(['run'])

self.check_hooks('start')
self.check_hooks('end')

@attr(type='postgres')
def test_pre_and_post_model_hooks_model_and_project(self):
def test_postgres_pre_and_post_model_hooks_model_and_project(self):
self.use_default_project({
'models': {
'test': {
Expand All @@ -230,3 +230,9 @@ def test_pre_and_post_model_hooks_model_and_project(self):
self.check_hooks('start', count=2)
self.check_hooks('end', count=2)

class TestPrePostModelHooksInConfigKwargs(TestPrePostModelHooksInConfig):

@property
def models(self):
return "test/integration/014_hook_tests/kwargs-models"

0 comments on commit e543dc4

Please sign in to comment.