-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add bigquery hooks (#779) #836
Merged
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
68015d8
Add a first attempt at pre/post hooks in bigquery
jwerderits 7f65ce3
Add some tests that I assume won't/can't work
jwerderits 1e0f0fd
Log the actual sql query in BigQuery when we have it instead of alway…
jwerderits ed191c2
Bring back add_query
jwerderits f842420
Have node_runners call execute(..., fetch=False) instead of execute_one
jwerderits 9e0c8a1
improve integration test support for bigquery
jwerderits c3856d0
Split out bigquery tests into their own files
jwerderits 31cf513
Re-order arguments, oops
jwerderits 55f4b79
fix logger import issue
jwerderits a1e987d
Tests expect tuples, not dbt.schema.Column objects
jwerderits a1f2135
Some tests care about order here
jwerderits 7ef42ac
For these tests, we want 'dtype', not 'data_type'
jwerderits 83f3cd1
Merge branch 'development' into bigquery-hooks
jwerderits 4f7644c
Make it so dbt can actually find the seed file so we can actually tes…
jwerderits e0b5112
Added checks to make sure the test actually does anything, fixed the …
jwerderits 8d0bb7c
Update changelog
jwerderits db880ea
Merge branch 'development' into bigquery-hooks
jwerderits 8440f01
Make seed pre-run hooks happen, add tests
jwerderits cdd6ed1
Add some random numbers so our tests don't collide
jwerderits fdd0306
Take up to an hour with no output on snowflake tests before failing
jwerderits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import dbt.compat | ||
import logging | ||
import logging.handlers | ||
import os | ||
import sys | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/integration/014_hook_tests/macros/before-and-after-bq.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
{% macro custom_run_hook_bq(state, target, run_started_at, invocation_id) %} | ||
|
||
insert into {{ target.schema }}.on_run_hook ( | ||
state, | ||
target_dbname, | ||
target_host, | ||
target_name, | ||
target_schema, | ||
target_type, | ||
target_user, | ||
target_pass, | ||
target_threads, | ||
run_started_at, | ||
invocation_id | ||
) VALUES ( | ||
'{{ state }}', | ||
'{{ target.dbname }}', | ||
'{{ target.host }}', | ||
'{{ target.name }}', | ||
'{{ target.schema }}', | ||
'{{ target.type }}', | ||
'{{ target.user }}', | ||
'{{ target.pass }}', | ||
{{ target.threads }}, | ||
'{{ run_started_at }}', | ||
'{{ invocation_id }}' | ||
) | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
drop table if exists {schema}.on_model_hook; | ||
|
||
create table {schema}.on_model_hook ( | ||
state STRING, -- start|end | ||
|
||
target_dbname STRING, | ||
target_host STRING, | ||
target_name STRING, | ||
target_schema STRING, | ||
target_type STRING, | ||
target_user STRING, | ||
target_pass STRING, | ||
target_threads INT64, | ||
|
||
run_started_at STRING, | ||
invocation_id STRING | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
drop table if exists {schema}.on_run_hook; | ||
|
||
create table {schema}.on_run_hook ( | ||
state STRING, -- start|end | ||
|
||
target_dbname STRING, | ||
target_host STRING, | ||
target_name STRING, | ||
target_schema STRING, | ||
target_type STRING, | ||
target_user STRING, | ||
target_pass STRING, | ||
target_threads INT64, | ||
|
||
run_started_at STRING, | ||
invocation_id STRING | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
from nose.plugins.attrib import attr | ||
from test.integration.base import DBTIntegrationTest | ||
|
||
MODEL_PRE_HOOK = """ | ||
insert into {{this.schema}}.on_model_hook ( | ||
state, | ||
target_name, | ||
target_schema, | ||
target_type, | ||
target_threads, | ||
run_started_at, | ||
invocation_id | ||
) VALUES ( | ||
'start', | ||
'{{ target.name }}', | ||
'{{ target.schema }}', | ||
'{{ target.type }}', | ||
{{ target.threads }}, | ||
'{{ run_started_at }}', | ||
'{{ invocation_id }}' | ||
) | ||
""" | ||
|
||
|
||
MODEL_POST_HOOK = """ | ||
insert into {{this.schema}}.on_model_hook ( | ||
state, | ||
target_name, | ||
target_schema, | ||
target_type, | ||
target_threads, | ||
run_started_at, | ||
invocation_id | ||
) VALUES ( | ||
'end', | ||
'{{ target.name }}', | ||
'{{ target.schema }}', | ||
'{{ target.type }}', | ||
{{ target.threads }}, | ||
'{{ run_started_at }}', | ||
'{{ invocation_id }}' | ||
) | ||
""" | ||
|
||
class TestBigqueryPrePostModelHooks(DBTIntegrationTest): | ||
def setUp(self): | ||
DBTIntegrationTest.setUp(self) | ||
self.use_profile('bigquery') | ||
self.use_default_project() | ||
self.run_sql_file("test/integration/014_hook_tests/seed_model_bigquery.sql") | ||
|
||
self.fields = [ | ||
'state', | ||
'target_name', | ||
'target_schema', | ||
'target_threads', | ||
'target_type', | ||
'run_started_at', | ||
'invocation_id' | ||
] | ||
|
||
@property | ||
def schema(self): | ||
return "model_hooks_014" | ||
|
||
@property | ||
def profile_config(self): | ||
profile = self.bigquery_profile() | ||
profile['test']['outputs']['default2']['threads'] = 3 | ||
return profile | ||
|
||
@property | ||
def project_config(self): | ||
return { | ||
'macro-paths': ['test/integration/014_hook_tests/macros'], | ||
'models': { | ||
'test': { | ||
'pre-hook': [MODEL_PRE_HOOK], | ||
|
||
'post-hook':[MODEL_POST_HOOK] | ||
} | ||
} | ||
} | ||
|
||
@property | ||
def models(self): | ||
return "test/integration/014_hook_tests/models" | ||
|
||
def get_ctx_vars(self, state): | ||
field_list = ", ".join(self.fields) | ||
query = "select {field_list} from `{schema}.on_model_hook` where state = '{state}'".format(field_list=field_list, schema=self.unique_schema(), state=state) | ||
|
||
vals = self.run_sql(query, fetch='all') | ||
self.assertFalse(len(vals) == 0, 'nothing inserted into hooks table') | ||
self.assertFalse(len(vals) > 1, 'too many rows in hooks table') | ||
ctx = dict(zip(self.fields, vals[0])) | ||
|
||
return ctx | ||
|
||
def check_hooks(self, state): | ||
ctx = self.get_ctx_vars(state) | ||
print('ctx={}'.format(ctx)) | ||
|
||
self.assertEqual(ctx['state'], state) | ||
self.assertEqual(ctx['target_name'], 'default2') | ||
self.assertEqual(ctx['target_schema'], self.unique_schema()) | ||
self.assertEqual(ctx['target_threads'], 3) | ||
self.assertEqual(ctx['target_type'], 'bigquery') | ||
self.assertTrue(ctx['run_started_at'] is not None and len(ctx['run_started_at']) > 0, 'run_started_at was not set') | ||
self.assertTrue(ctx['invocation_id'] is not None and len(ctx['invocation_id']) > 0, 'invocation_id was not set') | ||
|
||
@attr(type='bigquery') | ||
def test_pre_and_post_model_hooks(self): | ||
self.run_dbt(['run']) | ||
|
||
self.check_hooks('start') | ||
self.check_hooks('end') | ||
|
||
|
||
class TestBigqueryPrePostModelHooksOnSeeds(DBTIntegrationTest): | ||
@property | ||
def schema(self): | ||
return "model_hooks_014" | ||
|
||
@property | ||
def models(self): | ||
return "test/integration/014_hook_tests/seed-models" | ||
|
||
@property | ||
def project_config(self): | ||
return { | ||
'data-paths': ['test/integration/014_hook_tests/data'], | ||
'models': {}, | ||
'seeds': { | ||
'post-hook': [ | ||
'alter table {{ this }} add column new_col int', | ||
'update {{ this }} set new_col = 1' | ||
] | ||
} | ||
} | ||
|
||
@attr(type='bigquery') | ||
def test_hooks_on_seeds(self): | ||
self.run_dbt(['seed']) | ||
self.run_dbt(['test']) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
does this work?
alter table ... add column
is not supported on BigQuery! I think a better test might just update a column or insert a recordThere 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.
It turns out that we weren't loading the seed file on Postgres or BigQuery because the extension is
.sql
instead of.csv
, so these hooks never run and the tests always pass.Good stuff.