Skip to content

Commit 11f18ed

Browse files
jdclaude
andauthored
feat: add diff() and diff_files() functions (#253)
Add functions that return the set of statements that differ between two SQL strings or files, using symmetric difference. This provides more detail than the boolean compare() function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 96be573 commit 11f18ed

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

sql_compare/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,19 @@ def compare_files(first_file: pathlib.Path, second_file: pathlib.Path) -> bool:
5252
return compare(first_file.read_text(), second_file.read_text())
5353

5454

55+
def diff_files(first_file: pathlib.Path, second_file: pathlib.Path) -> set[Statement]:
56+
"""Return the set of statements that differ between two SQL files."""
57+
return diff(first_file.read_text(), second_file.read_text())
58+
59+
5560
def compare(first_sql: str, second_sql: str) -> bool:
5661
"""Compare two SQL strings."""
57-
return _parse_statements(first_sql) == _parse_statements(second_sql)
62+
return not diff(first_sql, second_sql)
63+
64+
65+
def diff(first_sql: str, second_sql: str) -> set[Statement]:
66+
"""Return the set of statements that differ between two SQL strings."""
67+
return _parse_statements(first_sql) ^ _parse_statements(second_sql)
5868

5969

6070
def get_diff(

tests/test_sql_compare.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
)
103103
def test_compare_eq(first_sql: str, second_sql: str) -> None:
104104
assert sql_compare.compare(first_sql, second_sql)
105+
assert not sql_compare.diff(first_sql, second_sql)
105106

106107

107108
@pytest.mark.parametrize(
@@ -164,6 +165,7 @@ def test_compare_eq(first_sql: str, second_sql: str) -> None:
164165
)
165166
def test_compare_neq(first_sql: str, second_sql: str) -> None:
166167
assert not sql_compare.compare(first_sql, second_sql)
168+
assert sql_compare.diff(first_sql, second_sql)
167169

168170

169171
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)