mirror of
https://github.com/elisiariocouto/leggen.git
synced 2026-01-31 06:38:23 +00:00
Split the monolithic DatabaseService (1,492 lines) into focused repository modules using the repository pattern for better maintainability and separation of concerns. Changes: - Create new repositories/ directory with 5 focused repositories: - TransactionRepository: transaction data operations (264 lines) - AccountRepository: account data operations (128 lines) - BalanceRepository: balance data operations (107 lines) - MigrationRepository: all database migrations (629 lines) - SyncRepository: sync operation tracking (132 lines) - BaseRepository: shared database connection logic (28 lines) - Refactor DatabaseService into a clean facade (287 lines): - Delegates data access to repositories - Maintains public API (no breaking changes) - Keeps data processors in service layer - Preserves require_sqlite decorator - Update tests to mock repository methods instead of private methods - Fix test references to internal methods (_persist_*, _get_*) Benefits: - Clear separation of concerns (one repository per domain) - Easier maintenance (changes isolated to specific repositories) - Better testability (repositories can be mocked individually) - Improved code organization (from 1 file to 7 focused files) All 114 tests passing.
29 lines
825 B
Python
29 lines
825 B
Python
import sqlite3
|
|
from contextlib import contextmanager
|
|
|
|
from leggen.utils.paths import path_manager
|
|
|
|
|
|
class BaseRepository:
|
|
"""Base repository with shared database connection logic"""
|
|
|
|
@contextmanager
|
|
def _get_db_connection(self, row_factory: bool = False):
|
|
"""Context manager for database connections with proper cleanup"""
|
|
db_path = path_manager.get_database_path()
|
|
conn = sqlite3.connect(str(db_path))
|
|
if row_factory:
|
|
conn.row_factory = sqlite3.Row
|
|
try:
|
|
yield conn
|
|
except Exception as e:
|
|
conn.rollback()
|
|
raise e
|
|
finally:
|
|
conn.close()
|
|
|
|
def _db_exists(self) -> bool:
|
|
"""Check if database file exists"""
|
|
db_path = path_manager.get_database_path()
|
|
return db_path.exists()
|