Skip to content

Commit 21350f2

Browse files
committed
Refactor configuration paths and enhance environment variable expansion in YAML loading
1 parent 758df12 commit 21350f2

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

sqlcompare/db/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from pathlib import Path
55

66
lib_name = "sqlcompare"
7-
lib_config_folder = Path.home() / f".{lib_name}"
7+
lib_config_folder = Path.home() / ".config" / f"{lib_name}"
88

99
LIBRARY_CONNECTIONS = [
1010
os.getenv(
11-
"SQLCOMPARE_CONNECTIONS_FILE", str(lib_config_folder / "connections.yml")
11+
"SQLCOMPARE_CONNECTIONS_FILE", str(lib_config_folder / "connections.yaml")
1212
),
1313
os.getenv("DTK_CONNECTIONS_FILE", str(Path.home() / ".dtk" / "connections.yml")),
1414
]

sqlcompare/db/resolver.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,27 @@ def normalize_connection_name(name: str) -> str:
2323
return s
2424

2525

26+
def _expand_env_vars(value: Any) -> Any:
27+
"""
28+
Recursively expand environment variables in strings, dicts, and lists.
29+
30+
Expands ${VAR_NAME} and $VAR_NAME patterns in strings.
31+
"""
32+
if isinstance(value, str):
33+
return os.path.expandvars(value)
34+
elif isinstance(value, dict):
35+
return {k: _expand_env_vars(v) for k, v in value.items()}
36+
elif isinstance(value, list):
37+
return [_expand_env_vars(item) for item in value]
38+
else:
39+
return value
40+
41+
2642
def _load_connections_from_yaml(yaml_path: str) -> dict[str, dict[str, Any]]:
2743
"""
2844
Load connection definitions from YAML file.
2945
Returns a dict mapping conn_id -> URL.create() parameters.
46+
Expands environment variables in all string values.
3047
"""
3148
expanded_path = Path(yaml_path).expanduser()
3249
if not expanded_path.exists():
@@ -35,7 +52,10 @@ def _load_connections_from_yaml(yaml_path: str) -> dict[str, dict[str, Any]]:
3552
try:
3653
with open(expanded_path, "r") as f:
3754
data = yaml.safe_load(f)
38-
return data if isinstance(data, dict) else {}
55+
if isinstance(data, dict):
56+
# Expand environment variables in all values
57+
return _expand_env_vars(data)
58+
return {}
3959
except Exception:
4060
return {}
4161

sqlcompare/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def query(q: str, connection: str | None, output: str) -> None:
3737
if rows:
3838
from tabulate import tabulate
3939

40-
print(tabulate(rows, headers=columns))
40+
print(tabulate(rows, headers=columns, tablefmt="pretty"))
4141
print(f"\nTotal rows: {len(rows)}")
4242
else:
4343
print("Query executed successfully. No rows returned.")

sqlcompare/utils/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def format_table(
3333
"""
3434
n_cols = len(columns)
3535
if n_cols == 0:
36-
return tabulate([], headers=[], tablefmt=tablefmt, **tabulate_kwargs)
36+
return tabulate([], headers=[], tablefmt="pretty", **tabulate_kwargs)
3737

3838
# ---- column trimming ----
3939
use_col_ellipsis = n_cols > max_cols and max_cols >= 2

sqlcompare/utils/test_types/stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def _quote_ident(name: str) -> str:
9-
return f'"{name.replace('"', '""')}"'
9+
return '"' + name.replace('"', '""') + '"'
1010

1111

1212
def _collect_table_stats(

0 commit comments

Comments
 (0)