-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be |
||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we do |
||
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 | ||
|
||
|
There was a problem hiding this comment.
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 theon-run-end
hook after a long dbt run. Do we need to clear this transaction here?There was a problem hiding this comment.
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