From ed4187970cdfb30e7e28df2d25113a065244fcc3 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Wed, 12 Nov 2025 18:29:49 +0100 Subject: [PATCH] feat: Implement SQLite support with environment-driven backend switching --- changelog.md | 1 + config/database.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/changelog.md b/changelog.md index bc1519e..87a83b3 100644 --- a/changelog.md +++ b/changelog.md @@ -15,6 +15,7 @@ - Resolved Ruff E402 warnings by moving module docstrings ahead of `from __future__ import annotations` across currency and pricing service modules, dropped the unused `HTTPException` import in `monitoring/__init__.py`, and confirmed a clean `ruff check .` run. - Enhanced the deploy job in `.gitea/workflows/cicache.yml` to capture Kubernetes pod, deployment, and container logs into `/logs/deployment/` for staging/production rollouts and publish them via a `deployment-logs` artifact, updating CI/CD documentation with retrieval instructions. - Fixed CI dashboard template lookup failures by renaming `templates/Dashboard.html` to `templates/dashboard.html` and verifying `tests/test_dashboard_route.py` locally to ensure TemplateNotFound no longer occurs on case-sensitive filesystems. +- Implemented SQLite support as primary local database with environment-driven backend switching (`CALMINER_USE_SQLITE=true`), updated `scripts/init_db.py` for database-agnostic DDL generation (PostgreSQL enums vs SQLite CHECK constraints), tested compatibility with both backends, and verified application startup and seeded data initialization work seamlessly across SQLite and PostgreSQL. ## 2025-11-11 diff --git a/config/database.py b/config/database.py index 149d0f6..e4d7dd4 100644 --- a/config/database.py +++ b/config/database.py @@ -11,12 +11,21 @@ def _build_database_url() -> str: """Construct the SQLAlchemy database URL from granular environment vars. Falls back to `DATABASE_URL` for backward compatibility. + Supports SQLite when CALMINER_USE_SQLITE is set. """ legacy_url = os.environ.get("DATABASE_URL", "") if legacy_url and legacy_url.strip() != "": return legacy_url + use_sqlite = os.environ.get("CALMINER_USE_SQLITE", "").lower() in ("true", "1", "yes") + if use_sqlite: + # Use SQLite database + db_path = os.environ.get("DATABASE_PATH", "./data/calminer.db") + # Ensure the directory exists + os.makedirs(os.path.dirname(db_path), exist_ok=True) + return f"sqlite:///{db_path}" + driver = os.environ.get("DATABASE_DRIVER", "postgresql") host = os.environ.get("DATABASE_HOST") port = os.environ.get("DATABASE_PORT", "5432")