Skip to content

Commit

Permalink
When column config says quote, use quotes in SQL to add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Sep 1, 2020
1 parent f55b257 commit 34d2798
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added 3 more adapter methods that the new dbt-adapter-test suite can use for testing. ([#2492](https://github.com/fishtown-analytics/dbt/issues/2492), [#2721](https://github.com/fishtown-analytics/dbt/pull/2721))
- It is now an error to attempt installing `dbt` with a Python version less than 3.6. (resolves [#2347](https://github.com/fishtown-analytics/dbt/issues/2347))
- Check for Postgres relation names longer than 63 and throw exception. ([#2197](https://github.com/fishtown-analytics/dbt/issues/2197))
- If column config says quote, use quoting in SQL for adding a comment. ([#2539](https://github.com/fishtown-analytics/dbt/issues/2539))


### Fixes
Expand Down
1 change: 1 addition & 0 deletions core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ColumnInfo(
description: str = ''
meta: Dict[str, Any] = field(default_factory=dict)
data_type: Optional[str] = None
quote: Optional[bool] = None
tags: List[str] = field(default_factory=list)
_extra: Dict[str, Any] = field(default_factory=dict)

Expand Down
6 changes: 6 additions & 0 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ def add(
):
tags: List[str] = []
tags.extend(getattr(column, 'tags', ()))
quote: Optional[bool]
if isinstance(column, UnparsedColumn):
quote = column.quote
else:
quote = None
self.column_info[column.name] = ColumnInfo(
name=column.name,
description=description,
data_type=data_type,
meta=meta,
tags=tags,
quote=quote,
_extra=column.extra
)

Expand Down
2 changes: 1 addition & 1 deletion plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,6 @@
{% for column_name in column_dict %}
{% set comment = column_dict[column_name]['description'] %}
{% set escaped_comment = postgres_escape_comment(comment) %}
comment on column {{ relation }}.{{ column_name }} is {{ escaped_comment }};
comment on column {{ relation }}.{{ adapter.quote(column_name) if column_dict[column_name]['quote'] == True else column_name }} is {{ escaped_comment }};
{% endfor %}
{% endmacro %}
79 changes: 79 additions & 0 deletions test/integration/029_docs_generate_tests/test_docs_generate.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/integration/035_docs_blocks/test_docs_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_postgres_valid_doc_ref(self):
'description': 'The user ID number',
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['id']
Expand All @@ -48,6 +49,7 @@ def test_postgres_valid_doc_ref(self):
'description': "The user's first name",
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['first_name']
Expand All @@ -59,6 +61,7 @@ def test_postgres_valid_doc_ref(self):
'description': "The user's last name",
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['last_name']
Expand Down Expand Up @@ -86,6 +89,7 @@ def test_postgres_alternative_docs_path(self):
'description': 'The user ID number with alternative text',
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['id']
Expand All @@ -96,6 +100,7 @@ def test_postgres_alternative_docs_path(self):
'description': "The user's first name",
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['first_name']
Expand All @@ -107,6 +112,7 @@ def test_postgres_alternative_docs_path(self):
'description': "The user's last name in this other file",
'data_type': None,
'meta': {},
'quote': None,
'tags': [],
},
model_data['columns']['last_name']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as "2id"
6 changes: 6 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,6 +43,12 @@ models:
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: quote_model
description: "model to test column quotes and comments"
columns:
- name: 2id
description: "My description"
quote: true

seeds:
- name: seed
Expand Down
4 changes: 2 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 @@ -81,7 +81,7 @@ def run_has_comments_pglike(self):
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 3
assert len(catalog_data['nodes']) == 4
table_node = catalog_data['nodes']['model.test.table_model']
view_node = self._assert_has_table_comments(table_node)

Expand Down Expand Up @@ -125,7 +125,7 @@ def test_redshift_late_binding_view(self):
with open('target/catalog.json') as fp:
catalog_data = json.load(fp)
assert 'nodes' in catalog_data
assert len(catalog_data['nodes']) == 3
assert len(catalog_data['nodes']) == 4
table_node = catalog_data['nodes']['model.test.table_model']
view_node = self._assert_has_table_comments(table_node)

Expand Down

0 comments on commit 34d2798

Please sign in to comment.