from __future__ import annotations from datetime import date, datetime from decimal import Decimal from sqlalchemy import ( BigInteger, Date, DateTime, ForeignKey, Integer, UniqueConstraint, func, ) from sqlalchemy.orm import Mapped, mapped_column from app.core.config import settings from app.db.base import Base from app.db.types import AuditMixin, IdMixin, Rate, qualified class ExchangeRate(IdMixin, AuditMixin, Base): """Официальный курс ЦБ на дату. rate — курс за `nominal` единиц валюты, как публикует ЦБ (). Курс за одну единицу = rate / nominal (для CNY nominal = 1). """ __tablename__ = "exchange_rate" __table_args__ = ( UniqueConstraint( "currency_id", "rate_date", name="uq_exchange_rate_currency_date" ), {"schema": settings.DATABASE_SCHEMA} if settings.DATABASE_SCHEMA else {}, ) currency_id: Mapped[int] = mapped_column( BigInteger, ForeignKey(qualified("currency.id")), nullable=False ) rate_date: Mapped[date] = mapped_column(Date, nullable=False) nominal: Mapped[int] = mapped_column(Integer, nullable=False) rate: Mapped[Decimal] = mapped_column(Rate, nullable=False) loaded_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False )