refactor(core): Integrate directory creation with database path retrieval and remove backup file.

Co-authored-by: elisiariocouto <818914+elisiariocouto@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-09-11 22:45:01 +00:00
committed by Elisiário Couto
parent 8654471042
commit 7d9744a40e
4 changed files with 18 additions and 381 deletions

View File

@@ -11,7 +11,6 @@ from leggen.utils.paths import path_manager
def persist_balances(ctx: click.Context, balance: dict):
# Connect to SQLite database
db_path = path_manager.get_database_path()
path_manager.ensure_database_dir_exists()
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
@@ -108,7 +107,6 @@ def persist_balances(ctx: click.Context, balance: dict):
def persist_transactions(ctx: click.Context, account: str, transactions: list) -> list:
# Connect to SQLite database
db_path = path_manager.get_database_path()
path_manager.ensure_database_dir_exists()
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
@@ -404,7 +402,6 @@ def get_transaction_count(account_id=None, **filters):
def persist_account(account_data: dict):
"""Persist account details to SQLite database"""
db_path = path_manager.get_database_path()
path_manager.ensure_database_dir_exists()
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()

View File

@@ -34,17 +34,21 @@ class PathManager:
return self.get_config_dir() / "config.toml"
def get_database_path(self) -> Path:
"""Get the database file path."""
"""Get the database file path and ensure the directory exists."""
if self._database_path is not None:
return self._database_path
db_path = self._database_path
else:
# Check environment variable first
database_path = os.environ.get("LEGGEN_DATABASE_PATH")
if database_path:
db_path = Path(database_path)
else:
# Default to config_dir/leggen.db
db_path = self.get_config_dir() / "leggen.db"
# Check environment variable first
database_path = os.environ.get("LEGGEN_DATABASE_PATH")
if database_path:
return Path(database_path)
# Default to config_dir/leggen.db
return self.get_config_dir() / "leggen.db"
# Ensure the directory exists
db_path.parent.mkdir(parents=True, exist_ok=True)
return db_path
def set_database_path(self, path: Path) -> None:
"""Set the database file path."""
@@ -59,7 +63,11 @@ class PathManager:
self.get_config_dir().mkdir(parents=True, exist_ok=True)
def ensure_database_dir_exists(self) -> None:
"""Ensure the database directory exists."""
"""Ensure the database directory exists.
Note: get_database_path() now automatically ensures the directory exists,
so this method is mainly for explicit directory creation in tests.
"""
self.get_database_path().parent.mkdir(parents=True, exist_ok=True)