Production
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"""Single import point for every ORM model.
|
||||
|
||||
Alembic autogenerate and SQLAlchemy's string-based relationship resolution both need
|
||||
every mapper registered before use. Import this module, never individual model
|
||||
modules, when you need "all of them".
|
||||
"""
|
||||
|
||||
from app.audit.models import AuditLog
|
||||
from app.auth.models import RefreshToken
|
||||
from app.chemists.models import Chemist, ChemistAssignment
|
||||
from app.csv_data.models import FormApproval
|
||||
from app.database.base import Base
|
||||
from app.doctors.models import Doctor, DoctorAssignment
|
||||
from app.organization.models import AMHeadquarters, Company, Headquarters
|
||||
from app.products.models import Product
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall
|
||||
from app.users.models import User
|
||||
|
||||
__all__ = [
|
||||
"Base",
|
||||
"AuditLog",
|
||||
"RefreshToken",
|
||||
"Chemist",
|
||||
"FormApproval",
|
||||
"ChemistAssignment",
|
||||
"Doctor",
|
||||
"DoctorAssignment",
|
||||
"AMHeadquarters",
|
||||
"Company",
|
||||
"Headquarters",
|
||||
"ChemistCall",
|
||||
"DailyCallReport",
|
||||
"DoctorCall",
|
||||
"User",
|
||||
"Product",
|
||||
]
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Declarative base and shared column mixins."""
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, func
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class UUIDMixin:
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
|
||||
|
||||
class TimestampMixin:
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), nullable=False
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
onupdate=func.now(),
|
||||
nullable=False,
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Engine, session factory and the `get_db` request dependency.
|
||||
|
||||
One transaction per request: the dependency opens a session, the service layer
|
||||
commits on success, and any escaping exception rolls the whole request back — so a
|
||||
state change and its audit row can never be persisted separately.
|
||||
"""
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from app.config.settings import settings
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=settings.DB_ECHO,
|
||||
pool_pre_ping=True,
|
||||
)
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
bind=engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
Reference in New Issue
Block a user