Skip to content

Commit 16a8d94

Browse files
authored
Merge pull request #2440 from Kernel-Hunter/fix-destructive-leading-comment
Don't flag a safe UPDATE as destructive when it starts with a comment
2 parents 375bf41 + 6494a69 commit 16a8d94

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Upcoming (TBD)
44
Bug Fixes
55
--------
66
* Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment.
7+
* Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment.
78

89

910
Internal

‎mycli/packages/utils/sql_utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def query_has_where_clause(query: str) -> bool:
362362
def query_is_single_table_update(query: str) -> bool:
363363
"""Check if a query is a simple single-table UPDATE."""
364364
cleaned_query_for_parsing_only = sqlparse.format(query, strip_comments=True)
365-
cleaned_query_for_parsing_only = re.sub(r'\s+', ' ', cleaned_query_for_parsing_only)
365+
cleaned_query_for_parsing_only = re.sub(r'\s+', ' ', cleaned_query_for_parsing_only).strip()
366366
if not cleaned_query_for_parsing_only:
367367
return False
368368
parsed = sqlparse.parse(cleaned_query_for_parsing_only)

‎mycli_test/pytests/test_sql_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def test_find_prev_keyword(sql, expected_keyword, expected_text):
354354
('update test set x = 1', True),
355355
('update test t set x = 1', True),
356356
('update /* inline comment */ test set x = 1', True),
357+
('/* leading comment */ update test set x = 1', True),
357358
('select 1', False),
358359
('', False),
359360
('update', False),
@@ -456,6 +457,11 @@ def test_is_destructive_update_with_where_clause_and_comment():
456457
assert is_destructive(["update"], sql) is False
457458

458459

460+
def test_is_destructive_update_with_where_clause_and_leading_comment():
461+
sql = "/* leading comment */ UPDATE test SET x = 1 WHERE id = 1;"
462+
assert is_destructive(["update"], sql) is False
463+
464+
459465
def test_is_destructive_checks_queries_after_safe_update():
460466
sql = "UPDATE test SET x = 1 WHERE id = 1;\ndrop database foo;"
461467
assert is_destructive(["update", "drop"], sql) is True

0 commit comments

Comments
 (0)