Files
writer-work-flow/packages/db/ww_db/base.py
Yaojia Wang f7004e8d74 fix(db): 时间列 TIMESTAMPTZ + 热路径索引 + 状态 CHECK 约束
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 往返无漂移。
2026-06-21 19:32:49 +02:00

33 lines
865 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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
)