Skip to content

Commit

Permalink
Merge pull request #1146 from fishtown-analytics/feature/hook-config-…
Browse files Browse the repository at this point in the history
…aliases

add pre_hook/post_hook aliases to config (#1124)
  • Loading branch information
beckjake authored Nov 26, 2018
2 parents d16ca86 + 71a2398 commit b751ed6
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 6 deletions.
15 changes: 15 additions & 0 deletions dbt/context/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ def __init__(self, model, source_config):
self.model = model
self.source_config = source_config

def _transform_config(self, config):
for oldkey in ('pre_hook', 'post_hook'):
if oldkey in config:
newkey = oldkey.replace('_', '-')
if newkey in config:
dbt.exceptions.raise_compiler_error(
'Invalid config, has conflicting keys "{}" and "{}"'
.format(oldkey, newkey),
self.model
)
config[newkey] = config.pop(oldkey)
return config

def __call__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0:
opts = args[0]
Expand All @@ -60,6 +73,8 @@ def __call__(self, *args, **kwargs):
"Invalid inline model config",
self.model)

opts = self._transform_config(opts)

self.source_config.update_in_model_config(opts)
return ''

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{{
config({
"pre-hook": "\
"pre_hook": "\
insert into {{this.schema}}.on_model_hook (\
\"state\",\
\"target.dbname\",\
Expand Down
91 changes: 91 additions & 0 deletions test/integration/014_hook_tests/error-models/hooks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

{{
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 }}'\
)",
"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
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
34 changes: 29 additions & 5 deletions test/integration/014_hook_tests/test_model_hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from nose.plugins.attrib import attr
from test.integration.base import DBTIntegrationTest
from dbt.exceptions import CompilationException


MODEL_PRE_HOOK = """
Expand Down Expand Up @@ -64,7 +65,6 @@

class BaseTestPrePost(DBTIntegrationTest):
def setUp(self):
self.adapter_type = 'bigquery'
DBTIntegrationTest.setUp(self)

self.run_sql_file("test/integration/014_hook_tests/seed_model.sql")
Expand Down 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,27 @@ 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"



class TestDuplicateHooksInConfigs(DBTIntegrationTest):
@property
def schema(self):
return "model_hooks_014"

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

@attr(type='postgres')
def test_postgres_run_duplicate_hook_defs(self):
with self.assertRaises(CompilationException) as exc:
self.run_dbt(['run'])

self.assertIn('pre_hook', str(exc.exception))
self.assertIn('pre-hook', str(exc.exception))

0 comments on commit b751ed6

Please sign in to comment.