-
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
Append query comment #2199
Append query comment #2199
Changes from 12 commits
62b19b5
6910847
65f1472
f18f6af
be80009
5fa59d8
fd8629a
1cf9220
e3c53ee
ab19135
3b90bfc
36bf479
7c34ff1
75e1c1d
65d9c18
ac336b2
574d65a
0eed51d
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,9 @@ | |||||||||
### Fixes | ||||||||||
- When a jinja value is undefined, give a helpful error instead of failing with cryptic "cannot pickle ParserMacroCapture" errors ([#2110](https://github.com/fishtown-analytics/dbt/issues/2110), [#2184](https://github.com/fishtown-analytics/dbt/pull/2184)) | ||||||||||
|
||||||||||
### Features | ||||||||||
- Support for appending query comments to SQL queries. ([#2199](https://github.com/fishtown-analytics/dbt/pull/2199)) | ||||||||||
|
||||||||||
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. Please add a contributors section, and add yourself!
Suggested change
|
||||||||||
## dbt 0.16.0rc2 (March 4, 2020) | ||||||||||
|
||||||||||
### Under the hood | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,12 @@ | |
from dbt.clients.system import path_exists | ||
from dbt.clients.system import load_file_contents | ||
from dbt.clients.yaml_helper import load_yaml_text | ||
from dbt.contracts.connection import QueryComment | ||
from dbt.exceptions import DbtProjectError | ||
from dbt.exceptions import RecursionException | ||
from dbt.exceptions import SemverException | ||
from dbt.exceptions import validator_error_message | ||
from dbt.exceptions import warn_or_error | ||
from dbt.helper_types import NoValue | ||
from dbt.semver import VersionSpecifier | ||
from dbt.semver import versions_compatible | ||
from dbt.version import get_installed_version | ||
|
@@ -202,6 +202,17 @@ def _raw_project_from(project_root: str) -> Dict[str, Any]: | |
return project_dict | ||
|
||
|
||
def _query_comment_from_cfg( | ||
cfg_query_comment: Union[QueryComment, str] | ||
) -> QueryComment: | ||
if isinstance(cfg_query_comment, str): | ||
if cfg_query_comment in ('None', ''): | ||
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.
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.
because 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. I don't think we should catch that case! In yaml, the appropriate thing to do to get
or
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. The docs says that
What is the preferred way of disabling query comments? 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. The docs are wrong! I'll open a PR there to fix that, thank you for pointing that out. The two methods I listed above should both work. 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. Now that I think about it more, this problem is why I had the 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. So you are suggesting something like this:
Is my understanding correct? 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. Yeah, though unions get collapsed by |
||
return QueryComment(comment='') | ||
return QueryComment(comment=cfg_query_comment) | ||
|
||
return cfg_query_comment | ||
|
||
|
||
@dataclass | ||
class PartialProject: | ||
profile_name: Optional[str] | ||
|
@@ -244,7 +255,7 @@ class Project: | |
snapshots: Dict[str, Any] | ||
dbt_version: List[VersionSpecifier] | ||
packages: Dict[str, Any] | ||
query_comment: Optional[Union[str, NoValue]] | ||
query_comment: Optional[Union[QueryComment, str]] | ||
|
||
@property | ||
def all_source_paths(self) -> List[str]: | ||
|
@@ -356,7 +367,10 @@ def from_project_config( | |
dbt_raw_version: Union[List[str], str] = '>=0.0.0' | ||
if cfg.require_dbt_version is not None: | ||
dbt_raw_version = cfg.require_dbt_version | ||
query_comment = cfg.query_comment | ||
|
||
query_comment = None | ||
if cfg.query_comment is not None: | ||
query_comment = _query_comment_from_cfg(cfg.query_comment) | ||
|
||
try: | ||
dbt_version = _parse_versions(dbt_raw_version) | ||
|
@@ -442,10 +456,11 @@ def to_project_config(self, with_packages=False): | |
v.to_version_string() for v in self.dbt_version | ||
], | ||
}) | ||
if self.query_comment is not None: | ||
result['query-comment'] = self.query_comment.to_dict() | ||
|
||
if with_packages: | ||
result.update(self.packages.to_dict()) | ||
if self.query_comment != NoValue(): | ||
result['query-comment'] = self.query_comment | ||
|
||
return result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import re | ||
from unittest import TestCase, mock | ||
|
||
from dbt.adapters.base.query_headers import MacroQueryStringSetter | ||
|
||
from test.unit.utils import config_from_parts_or_dicts | ||
|
||
|
||
class TestQueryHeaders(TestCase): | ||
|
||
def setUp(self): | ||
self.profile_cfg = { | ||
'outputs': { | ||
'test': { | ||
'type': 'postgres', | ||
'dbname': 'postgres', | ||
'user': 'test', | ||
'host': 'test', | ||
'pass': 'test', | ||
'port': 5432, | ||
'schema': 'test' | ||
}, | ||
}, | ||
'target': 'test' | ||
} | ||
self.project_cfg = { | ||
'name': 'query_headers', | ||
'version': '0.1', | ||
'profile': 'test', | ||
} | ||
self.query = "SELECT 1;" | ||
|
||
def test_comment_should_prepend_query_by_default(self): | ||
config = config_from_parts_or_dicts(self.project_cfg, self.profile_cfg) | ||
query_header = MacroQueryStringSetter(config, mock.MagicMock(macros={})) | ||
sql = query_header.add(self.query) | ||
self.assertTrue(re.match(f'^\/\*.*\*\/\n{self.query}$', sql)) | ||
|
||
|
||
def test_append_comment(self): | ||
self.project_cfg.update({ | ||
'query-comment': { | ||
'comment': 'executed by dbt', | ||
'append': True | ||
} | ||
}) | ||
config = config_from_parts_or_dicts(self.project_cfg, self.profile_cfg) | ||
query_header = MacroQueryStringSetter(config, mock.MagicMock(macros={})) | ||
sql = query_header.add(self.query) | ||
self.assertEqual(sql, f'{self.query[:-1]}\n/* executed by dbt */;') | ||
|
||
def test_disable_query_comment(self): | ||
self.project_cfg.update({ | ||
'query-comment': '' | ||
}) | ||
config = config_from_parts_or_dicts(self.project_cfg, self.profile_cfg) | ||
query_header = MacroQueryStringSetter(config, mock.MagicMock(macros={})) | ||
self.assertEqual(query_header.add(self.query), self.query) |
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.
Oops, sorry, one last thing! Can you add the issue to this changelog entry as well?
[#2138](https://github.com/fishtown-analytics/dbt/issues/2138)
should be it