Skip to content

Commit 758df12

Browse files
committed
Refactor test runs handling in config module to improve directory structure and YAML file management
1 parent c434892 commit 758df12

2 files changed

Lines changed: 24 additions & 32 deletions

File tree

sqlcompare/compare/comparator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,14 @@ def compare(
353353
runs = load_test_runs()
354354

355355
# Save metadata for later analysis
356-
# For DuckDB connections (file-based tests), save "duckdb"
357-
# For remote databases (Snowflake, etc.), save the connection name
358-
conn_name = self.connection if isinstance(self.connection, str) else "duckdb"
359-
356+
# Save the connection as-is (can be None, connection ID, or URL)
357+
# When None, it will resolve to the default connection on inspection
360358
run_data = {
361359
"tables": tables,
362360
"index_cols": list(self.index_cols),
363361
"cols_prev": self.cols_prev,
364362
"cols_new": self.cols_new,
365-
"conn": conn_name,
363+
"conn": self.connection,
366364
}
367365

368366
runs[diff_id] = run_data

sqlcompare/config.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import os
44
from pathlib import Path
55
from typing import Any
6+
import yaml
67

78

89
def _config_dir() -> Path:
910
return Path(
10-
os.getenv("SQLCOMPARE_CONFIG_DIR", Path.home() / ".config" / "data-toolkit")
11+
os.getenv("SQLCOMPARE_CONFIG_DIR", Path.home() / ".config" / "sqlcompare")
1112
)
1213

1314

@@ -23,8 +24,8 @@ def load_config() -> dict[str, Any]:
2324
}
2425

2526

26-
def _runs_file() -> Path:
27-
return _config_dir() / "db_test_runs.yaml"
27+
def _runs_dir() -> Path:
28+
return _config_dir() / "runs"
2829

2930

3031
def get_tests_folder() -> Path:
@@ -35,39 +36,32 @@ def get_tests_folder() -> Path:
3536

3637

3738
def load_test_runs() -> dict[str, Any]:
38-
path = _runs_file()
39-
if not path.exists():
39+
runs_dir = _runs_dir()
40+
if not runs_dir.exists():
4041
return {}
41-
payload = _read_yaml(path)
42-
if not payload:
43-
return {}
44-
if not isinstance(payload, dict):
45-
raise ValueError("Expected mapping in db_test_runs.yaml")
46-
return payload
42+
43+
runs = {}
44+
for yaml_file in runs_dir.glob("*.yaml"):
45+
run_id = yaml_file.stem # filename without extension
46+
payload = _read_yaml(yaml_file)
47+
if payload:
48+
runs[run_id] = payload
49+
50+
return runs
4751

4852

4953
def save_test_runs(runs: dict[str, Any]) -> None:
50-
path = _runs_file()
51-
path.parent.mkdir(parents=True, exist_ok=True)
52-
_write_yaml(path, runs)
54+
runs_dir = _runs_dir()
55+
runs_dir.mkdir(parents=True, exist_ok=True)
5356

57+
for run_id, run_data in runs.items():
58+
file_path = runs_dir / f"{run_id}.yaml"
59+
_write_yaml(file_path, run_data)
5460

55-
def _read_yaml(path: Path) -> dict[str, Any] | None:
56-
try:
57-
import yaml
58-
except ImportError:
59-
import json
6061

61-
return json.loads(path.read_text(encoding="utf-8"))
62+
def _read_yaml(path: Path) -> dict[str, Any] | None:
6263
return yaml.safe_load(path.read_text(encoding="utf-8"))
6364

6465

6566
def _write_yaml(path: Path, payload: dict[str, Any]) -> None:
66-
try:
67-
import yaml
68-
except ImportError:
69-
import json
70-
71-
path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
72-
return
7367
path.write_text(yaml.safe_dump(payload, sort_keys=True), encoding="utf-8")

0 commit comments

Comments
 (0)