Skip to content

Commit 3b39689

Browse files
Fix inspect diff totals with separate count query
1 parent 7f5af8f commit 3b39689

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

sqlcompare/analysis/utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _display(
5555
*,
5656
is_stats: bool = False,
5757
list_columns: bool = False,
58+
total_differences: int | None = None,
5859
):
5960
if not columns:
6061
log.info("No results returned.")
@@ -79,7 +80,8 @@ def _display(
7980
log.info(format_table(columns, filtered_rows[:limit]))
8081
return
8182

82-
log.info(f"📂 Loaded diff data: {len(rows)} total differences")
83+
effective_total = total_differences if total_differences is not None else len(rows)
84+
log.info(f"📂 Loaded diff data: {effective_total} total differences")
8385
if list_columns:
8486
if column_idx is None:
8587
log.warning("⚠️ Column metadata not available.")
@@ -94,23 +96,24 @@ def _display(
9496
return
9597

9698
filtered_rows = rows
99+
filtered_total = effective_total
97100
if column and column_idx is not None:
98101
column_upper = column.upper()
99102
filtered_rows = [
100103
row for row in rows if str(row[column_idx]).upper() == column_upper
101104
]
105+
if total_differences is None:
106+
filtered_total = len(filtered_rows)
102107
if not filtered_rows:
103108
log.warning(f"⚠️ No differences found for column '{column}'")
104109
return
105-
log.info(
106-
f"🔍 Filtered to {len(filtered_rows)} differences for column '{column}'"
107-
)
110+
log.info(f"🔍 Filtered to {filtered_total} differences for column '{column}'")
108111

109112
display_rows = filtered_rows[:limit]
110-
log.info(f"📊 Showing {len(display_rows)} of {len(filtered_rows)} differences:")
113+
log.info(f"📊 Showing {len(display_rows)} of {filtered_total} differences:")
111114
log.info(format_table(columns, display_rows))
112-
if len(filtered_rows) > limit:
113-
log.info(f"💡 Use --limit {len(filtered_rows)} to see all results")
115+
if filtered_total > limit:
116+
log.info(f"💡 Use --limit {filtered_total} to see all results")
114117

115118

116119
def _column_index(columns: list[str], name: str) -> int | None:

sqlcompare/compare/comparator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ def get_diff_query(self, column: str = None, limit: int = None) -> str:
181181
query += f" LIMIT {limit}"
182182
return query
183183

184+
def get_diff_count_query(self, column: str = None) -> str:
185+
return (
186+
"SELECT COUNT(*) AS diff_count FROM ("
187+
+ self.get_diff_query(column=column, limit=None)
188+
+ ") AS diff_rows"
189+
)
190+
184191
def get_stats_query(self, column: str = None) -> str:
185192
cols_to_stat = self.common_cols
186193
if column:

sqlcompare/inspect.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,26 @@ def _inspect_db_run(
253253
_display(columns, rows, column, limit, diff_id, list_columns=list_columns)
254254
return
255255

256-
sql_limit = limit if not list_columns else None
257-
rows, columns = db.query(
258-
comp.get_diff_query(column=column, limit=sql_limit),
259-
include_columns=True,
260-
)
261-
_display(columns, rows, column, limit, diff_id, list_columns=list_columns)
256+
if save_mode == "none":
257+
total_differences: int | None = None
258+
if not list_columns:
259+
count_rows = db.query(comp.get_diff_count_query(column=column))
260+
count_value = count_rows[0][0] if count_rows and count_rows[0] else 0
261+
total_differences = int(count_value or 0)
262+
sql_limit = limit if not list_columns else None
263+
rows, columns = db.query(
264+
comp.get_diff_query(column=column, limit=sql_limit),
265+
include_columns=True,
266+
)
267+
_display(
268+
columns,
269+
rows,
270+
column,
271+
limit,
272+
diff_id,
273+
list_columns=list_columns,
274+
total_differences=total_differences,
275+
)
262276

263277
if save_mode != "none":
264278
_save_inspect_report(

tests/test_cli_analyze.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from sqlcompare.cli import app
99
from sqlcompare.config import load_test_runs
10+
from sqlcompare.db import DBConnection
1011
from tests.cli_helpers import seed_duckdb, set_cli_env
1112

1213

@@ -82,6 +83,7 @@ def test_analyze_diff_save_modes_and_missing_filters(tmp_path, monkeypatch) -> N
8283
)
8384
assert save_summary.exit_code == 0, save_summary.output
8485
assert "Report saved to:" in save_summary.output
86+
assert "Loaded diff data" not in save_summary.output
8587
summary_path = Path.cwd() / "reports" / "inspect_summary.xlsx"
8688
assert summary_path.exists()
8789

@@ -128,3 +130,44 @@ def test_analyze_diff_save_mode_validation(tmp_path, monkeypatch) -> None:
128130
)
129131
assert incompatible.exit_code != 0
130132
assert "Report export (--save summary/complete)" in incompatible.output
133+
134+
135+
def test_analyze_diff_column_limit_uses_total_count(tmp_path, monkeypatch) -> None:
136+
db_path = tmp_path / "sqlcompare_many.duckdb"
137+
with DBConnection(f"duckdb:///{db_path}") as db:
138+
db.execute("CREATE TABLE previous (id INTEGER, value INTEGER)")
139+
db.execute("CREATE TABLE current (id INTEGER, value INTEGER)")
140+
db.execute("INSERT INTO previous VALUES (1, 10), (2, 20), (3, 30), (4, 40), (5, 50)")
141+
db.execute("INSERT INTO current VALUES (1, 11), (2, 21), (3, 31), (4, 41), (5, 51)")
142+
143+
config_dir = tmp_path / "config"
144+
set_cli_env(
145+
monkeypatch,
146+
config_dir,
147+
"duckdb_many",
148+
f"duckdb:///{db_path}",
149+
)
150+
runner = CliRunner()
151+
compare_result = runner.invoke(
152+
app,
153+
[
154+
"table",
155+
"previous",
156+
"current",
157+
"id",
158+
"--connection",
159+
"duckdb_many",
160+
],
161+
)
162+
assert compare_result.exit_code == 0, compare_result.output
163+
164+
runs = load_test_runs()
165+
diff_id = next(iter(runs.keys()))
166+
167+
inspect_result = runner.invoke(
168+
app, ["inspect", diff_id, "--column", "value", "--limit", "2"]
169+
)
170+
assert inspect_result.exit_code == 0, inspect_result.output
171+
assert "Loaded diff data: 5 total differences" in inspect_result.output
172+
assert "Filtered to 5 differences for column 'value'" in inspect_result.output
173+
assert "Showing 2 of 5 differences" in inspect_result.output

0 commit comments

Comments
 (0)