Skip to content

Commit

Permalink
On bigquery, persist docs for seeds as well as tables/views
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Jun 29, 2020
1 parent e7298d6 commit bdd3e1a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Fixes
- Hash name of local packages ([#2600](https://github.com/fishtown-analytics/dbt/pull/2600))
- On bigquery, also persist docs for seeds ([#2598](https://github.com/fishtown-analytics/dbt/issues/2598), [#2601](https://github.com/fishtown-analytics/dbt/pull/2601))

## dbt 0.17.1rc2 (June 25, 2020)

Expand Down
17 changes: 17 additions & 0 deletions plugins/bigquery/dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,23 @@ def update_column_descriptions(self, relation, columns):
new_table = google.cloud.bigquery.Table(table_ref, schema=new_schema)
conn.handle.update_table(new_table, ['schema'])

@available.parse_none
def update_table_description(self, database, schema, identifier, description):
conn = self.connections.get_thread_connection()
client = conn.handle

table_ref = self.connections.table_ref(
database,
schema,
identifier,
conn
)
logger.critical(f'Updating table description for {database}.{schema}.{identifier}')
logger.critical(f'description={description}')
table = client.get_table(table_ref)
table.description = description
client.update_table(table, ['description'])

@available.parse_none
def alter_table_add_columns(self, relation, columns):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
{%- set column_override = model['config'].get('column_types', {}) -%}
{{ adapter.load_dataframe(model['database'], model['schema'], model['alias'],
agate_table, column_override) }}
{% if config.persist_relation_docs() and 'description' in model %}

{{ adapter.update_table_description(model['database'], model['schema'], model['alias'], model['description']) }}
{% endif %}
{% endmacro %}
3 changes: 3 additions & 0 deletions test/integration/060_persist_docs_tests/data/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name
1,Alice
2,Bob
25 changes: 25 additions & 0 deletions test/integration/060_persist_docs_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ models:
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
seeds:
- name: seed
description: |
Seed model description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
columns:
- name: id
description: |
id Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: name
description: |
Some stuff here and then a call to
{{ doc('my_fun_doc')}}
42 changes: 40 additions & 2 deletions test/integration/060_persist_docs_tests/test_persist_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ def project_config(self):
"columns": True,
},
}
}
},
'seeds': {
'test': {
'+persist_docs': {
"relation": True,
"columns": True,
},
}
},
}

@use_profile('snowflake')
Expand All @@ -157,7 +165,36 @@ def test_snowflake_persist_docs(self):

@use_profile('bigquery')
def test_bigquery_persist_docs(self):
self.run_dbt(['seed'])
self.run_dbt()
self.run_dbt(['docs', 'generate'])
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 4
desc_map = {
'seed': 'Seed model description',
'table_model': 'Table model description',
'view_model': 'View model description',
}
for node_id in ['seed', 'table_model', 'view_model']:
with self.adapter.connection_named('_test'):
client = self.adapter.connections \
.get_thread_connection().handle

table_id = "{}.{}.{}".format(
self.default_database,
self.unique_schema(),
node_id
)
bq_table = client.get_table(table_id)

bq_schema = bq_table.schema

assert bq_table.description.startswith(desc_map[node_id])
assert bq_schema[0].description.startswith('id Column description ')
if not node_id.startswith('view'):
assert bq_schema[1].description.startswith('Some stuff here and then a call to')


class TestPersistDocsNested(BasePersistDocsTest):
Expand Down Expand Up @@ -187,13 +224,14 @@ def test_bigquery_persist_docs(self):
Next, generate the catalog and check if the comments are also included.
"""
self.run_dbt(['seed'])
self.run_dbt()

self.run_dbt(['docs', 'generate'])
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 2 # table and view model
assert len(catalog_data['nodes']) == 3 # seed, table, and view model

for node_id in ['table_model_nested', 'view_model_nested']:
# check the descriptions using the api
Expand Down

0 comments on commit bdd3e1a

Please sign in to comment.