Refactor database initialization and remove Alembic migrations
- Removed legacy Alembic migration files and consolidated schema management into a new Pydantic-backed initializer (`scripts/init_db.py`). - Updated `main.py` to ensure the new DB initializer runs on startup, maintaining idempotency. - Adjusted session management in `config/database.py` to prevent DetachedInstanceError. - Introduced new enums in `models/enums.py` for better organization and clarity. - Refactored various models to utilize the new enums, improving code maintainability. - Enhanced middleware to handle JSON validation more robustly, ensuring non-JSON requests do not trigger JSON errors. - Added tests for middleware and enums to ensure expected behavior and consistency. - Updated changelog to reflect significant changes and improvements.
This commit is contained in:
@@ -27,6 +27,11 @@ from services.export_query import ProjectExportFilters, ScenarioExportFilters
|
||||
from services.pricing import PricingMetadata
|
||||
|
||||
|
||||
def _enum_value(e):
|
||||
"""Return the underlying value for Enum members, otherwise return as-is."""
|
||||
return getattr(e, "value", e)
|
||||
|
||||
|
||||
class ProjectRepository:
|
||||
"""Persistence operations for Project entities."""
|
||||
|
||||
@@ -202,7 +207,9 @@ class ScenarioRepository:
|
||||
return self.session.execute(stmt).scalar_one()
|
||||
|
||||
def count_by_status(self, status: ScenarioStatus) -> int:
|
||||
stmt = select(func.count(Scenario.id)).where(Scenario.status == status)
|
||||
status_val = _enum_value(status)
|
||||
stmt = select(func.count(Scenario.id)).where(
|
||||
Scenario.status == status_val)
|
||||
return self.session.execute(stmt).scalar_one()
|
||||
|
||||
def recent(self, limit: int = 5, *, with_project: bool = False) -> Sequence[Scenario]:
|
||||
@@ -219,9 +226,10 @@ class ScenarioRepository:
|
||||
limit: int | None = None,
|
||||
with_project: bool = False,
|
||||
) -> Sequence[Scenario]:
|
||||
status_val = _enum_value(status)
|
||||
stmt = (
|
||||
select(Scenario)
|
||||
.where(Scenario.status == status)
|
||||
.where(Scenario.status == status_val)
|
||||
.order_by(Scenario.updated_at.desc())
|
||||
)
|
||||
if with_project:
|
||||
@@ -311,7 +319,11 @@ class ScenarioRepository:
|
||||
stmt = stmt.where(Scenario.name.ilike(name_pattern))
|
||||
|
||||
if filters.statuses:
|
||||
stmt = stmt.where(Scenario.status.in_(filters.statuses))
|
||||
# Accept Enum members or raw values in filters.statuses
|
||||
status_values = [
|
||||
_enum_value(s) for s in (filters.statuses or [])
|
||||
]
|
||||
stmt = stmt.where(Scenario.status.in_(status_values))
|
||||
|
||||
if filters.start_date_from:
|
||||
stmt = stmt.where(Scenario.start_date >=
|
||||
|
||||
Reference in New Issue
Block a user