Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix statements in on run start hooks #693

Merged
merged 4 commits into from
Apr 9, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,42 +291,50 @@ def run_hooks(cls, project, adapter, flat_graph, hook_type):

nodes = flat_graph.get('nodes', {}).values()
hooks = get_nodes_by_tags(nodes, {hook_type}, NodeType.Operation)

# This will clear out an open transaction if there is one.
# on-run-* hooks should run outside of a transaction. This happens b/c
# psycopg2 automatically begins a transaction when a connection is
# created. TODO : Move transaction logic out of here, and implement
# a for-loop over these sql statements in jinja-land. Also, consider
# configuring psycopg2 (and other adapters?) to ensure that a
# transaction is only created if dbt initiates it.
conn_name = adapter.clear_transaction(profile)
adapter.clear_transaction(profile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmcarthur this will use the master connection, right? I think this can still cause issues with the on-run-end hook after a long dbt run. Do we need to clear this transaction here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not? good call


compiled_hooks = []
for hook in hooks:
model_name = hook.get('name')

# This will clear out an open transaction if there is one.
# on-run-* hooks should run outside of a transaction. This happens
# b/c psycopg2 automatically begins a transaction when a connection
# is created. TODO : Move transaction logic out of here, and
# implement a for-loop over these sql statements in jinja-land.
# Also, consider configuring psycopg2 (and other adapters?) to
# ensure that a transaction is only created if dbt initiates it.
adapter.clear_transaction(profile, model_name)
compiled = cls._compile_node(adapter, project, hook, flat_graph)
model_name = compiled.get('name')
statement = compiled['wrapped_sql']

hook_index = hook.get('index', len(hooks))
hook_dict = dbt.hooks.get_hook_dict(statement, index=hook_index)
compiled_hooks.append(hook_dict)
adapter.release_connection(profile, model_name)

ordered_hooks = sorted(compiled_hooks, key=lambda h: h.get('index', 0))

for hook in ordered_hooks:
model_name = compiled.get('name')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be hook, not compiled I think


if dbt.flags.STRICT_MODE:
dbt.contracts.graph.parsed.validate_hook(hook)

sql = hook.get('sql', '')
adapter.execute_one(profile, sql, model_name=conn_name,
auto_begin=False)
adapter.release_connection(profile, conn_name)

if len(sql) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do sql.strip() here? Unsure if it will make any difference, but I know sometimes people put conditional statements into hooks, and the else clause might contain some empty space

adapter.execute_one(profile, sql, model_name=model_name,
auto_begin=False)

adapter.release_connection(profile, model_name)

@classmethod
def safe_run_hooks(cls, project, adapter, flat_graph, hook_type):
try:
cls.run_hooks(project, adapter, flat_graph, hook_type)
except dbt.exceptions.RuntimeException as e:

except dbt.exceptions.RuntimeException:
logger.info("Database error while running {}".format(hook_type))
raise

Expand Down