Skip to content

Commit 8df3ffc

Browse files
authored
query_type: shorten the SQL query when logging the exception (#552)
Resolves #543
1 parent cc9017d commit 8df3ffc

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

sql_metadata/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ def query_type(self) -> str:
122122
switch = tokens[index].normalized
123123
self._query_type = SUPPORTED_QUERY_TYPES.get(switch, "UNSUPPORTED")
124124
if self._query_type == "UNSUPPORTED":
125-
self._logger.error("Not supported query type: %s", self._raw_query)
125+
# do not log the full query
126+
# https://github.com/macbre/sql-metadata/issues/543
127+
shorten_query = " ".join(self._raw_query.split(" ")[:3])
128+
129+
self._logger.error("Not supported query type: %s", shorten_query)
126130
raise ValueError("Not supported query type!")
127131
return self._query_type
128132

test/test_query_type.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def test_drop_table_query():
4848
assert "DROP TABLE" == Parser(query.format(comment)).query_type
4949

5050

51-
def test_unsupported_query():
51+
def test_unsupported_query(caplog):
5252
queries = [
53-
"FOO BAR",
54-
"DO SOMETHING",
53+
"FOO BAR LONG QUERY WITH MANY TOKENS",
54+
"DO SOMETHING LONG QUERY",
5555
]
5656

5757
for query in queries:
@@ -60,6 +60,15 @@ def test_unsupported_query():
6060

6161
assert "Not supported query type!" in str(ex.value)
6262

63+
# assert the SQL query is not logged
64+
# https://docs.pytest.org/en/stable/how-to/logging.html#caplog-fixture
65+
assert (
66+
f"Not supported query type: {query}" not in caplog.text
67+
), "The SQL query should not be logged"
68+
assert (
69+
f"Not supported query type: {query[:8]}" in caplog.text
70+
), "The SQL query should be trimmed when logged"
71+
6372

6473
def test_empty_query():
6574
queries = ["", "/* empty query */"]

0 commit comments

Comments
 (0)