- shared/db.py: async engine + session factory - shared/models/base.py: DeclarativeBase + TimestampMixin - shared/models/trading.py: Strategy, Signal, Trade, Position, StrategyWeightHistory - shared/models/news.py: Article, ArticleSentiment - shared/models/learning.py: TradeOutcome, LearningAdjustment - shared/models/auth.py: User, UserCredential - shared/models/timeseries.py: MarketData, PortfolioSnapshot, StrategyMetric - Alembic async env.py with initial migration including TimescaleDB hypertables - 21 model tests covering enums, instantiation, metadata registration
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
"""Authentication models: User, UserCredential."""
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import ForeignKey, Integer, LargeBinary, String
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from shared.models.base import Base, TimestampMixin
|
|
|
|
|
|
class User(TimestampMixin, Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
username: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
|
display_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
|
|
# Relationships
|
|
credentials: Mapped[list["UserCredential"]] = relationship(back_populates="user")
|
|
|
|
|
|
class UserCredential(TimestampMixin, Base):
|
|
__tablename__ = "user_credentials"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
|
)
|
|
credential_id: Mapped[str] = mapped_column(String(512), unique=True, nullable=False)
|
|
public_key: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
|
sign_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
user: Mapped[User] = relationship(back_populates="credentials")
|