- 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.
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING, List
|
|
|
|
from .enums import MiningOperationType
|
|
|
|
from sqlalchemy import DateTime, Enum as SQLEnum, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from config.database import Base
|
|
|
|
if TYPE_CHECKING: # pragma: no cover
|
|
from .scenario import Scenario
|
|
from .pricing_settings import PricingSettings
|
|
|
|
|
|
|
|
|
|
class Project(Base):
|
|
"""Top-level mining project grouping multiple scenarios."""
|
|
|
|
__tablename__ = "projects"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
|
location: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
operation_type: Mapped[MiningOperationType] = mapped_column(
|
|
SQLEnum(MiningOperationType, name="miningoperationtype", create_type=False),
|
|
nullable=False,
|
|
default=MiningOperationType.OTHER,
|
|
)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
pricing_settings_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("pricing_settings.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now(), onupdate=func.now()
|
|
)
|
|
|
|
scenarios: Mapped[List["Scenario"]] = relationship(
|
|
"Scenario",
|
|
back_populates="project",
|
|
cascade="all, delete-orphan",
|
|
passive_deletes=True,
|
|
)
|
|
pricing_settings: Mapped["PricingSettings | None"] = relationship(
|
|
"PricingSettings",
|
|
back_populates="projects",
|
|
)
|
|
|
|
def __repr__(self) -> str: # pragma: no cover - helpful for debugging
|
|
return f"Project(id={self.id!r}, name={self.name!r})"
|