P1-1 base.py 时间列改 timezone=True;迁移把 16 表 created_at/updated_at ALTER 为 TIMESTAMPTZ(手写 USING ... AT TIME ZONE 'UTC')。 P1-3 新增 8 个索引(chapter_reviews/chapter_digests 复合、owner_id、 usage_ledger、rules + jobs/rules 部分索引)。 P2 chapters/foreshadow status CHECK 约束。 迁移链 ad2c4c663daf → b1a2c3d4e5f6 → c2b3d4e5f6a7,upgrade/downgrade 往返无漂移。
33 lines
865 B
Python
33 lines
865 B
Python
"""SQLAlchemy 2.0 声明式基类 + 通用 mixin。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import uuid
|
||
from datetime import datetime
|
||
|
||
from sqlalchemy import DateTime, Uuid, func, text
|
||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||
|
||
|
||
class Base(DeclarativeBase):
|
||
pass
|
||
|
||
|
||
class UuidPk:
|
||
id: Mapped[uuid.UUID] = mapped_column(
|
||
Uuid, primary_key=True, server_default=text("gen_random_uuid()")
|
||
)
|
||
|
||
|
||
class CreatedAt:
|
||
# TIMESTAMPTZ:服务端 `func.now()` 兜底,无 client 端 default(统一存 UTC)。
|
||
created_at: Mapped[datetime] = mapped_column(
|
||
DateTime(timezone=True), server_default=func.now(), nullable=False
|
||
)
|
||
|
||
|
||
class TimestampedMixin(CreatedAt):
|
||
updated_at: Mapped[datetime] = mapped_column(
|
||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
|
||
)
|