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

possible fix for on-run-end hook closed conns #658

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
21 changes: 8 additions & 13 deletions dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,33 +276,28 @@ 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)

compiled_hooks = []
for hook in hooks:
compiled = cls.compile_node(adapter, project, hook, flat_graph)
model_name = compiled.get('name')
statement = compiled['wrapped_sql']

hook_dict = dbt.hooks.get_hook_dict(statement)
hook_dict['unique_id'] = hook['unique_id']
compiled_hooks.append(hook_dict)

for hook in compiled_hooks:

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

hook_name = hook['unique_id']
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:
adapter.clear_transaction(profile, hook_name)
adapter.execute_one(profile, sql, model_name=hook_name,
auto_begin=False)
adapter.release_connection(profile, hook_name)

@classmethod
def safe_run_hooks(cls, project, adapter, flat_graph, hook_type):
Expand Down