Files
ctms/backend/app/models/study_setup_config_version.py
T
Cheng Zhou f40e979c53 refactor(db): 抽出 GUID 类型并兼容 SQLite 的 JSONB 字段
- base_class.py 内置 GUID(TypeDecorator),PostgreSQL 走原生 UUID,
  SQLite 走 CHAR(36),UUID 在绑定/读取时统一转换。
- study_setup_config 与 study_setup_config_version 改用 JSONB().with_variant
  以便测试库使用 JSON 字段,生产仍保留 JSONB。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 12:36:02 +08:00

45 lines
2.4 KiB
Python

from __future__ import annotations
from typing import Optional
import uuid
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, JSON, UniqueConstraint, func
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base_class import Base
JSONB_TYPE = JSON().with_variant(JSONB, "postgresql")
class StudySetupConfigVersion(Base):
__tablename__ = "study_setup_config_versions"
__table_args__ = (
UniqueConstraint("study_id", "version", name="uq_setup_config_versions_study_version"),
UniqueConstraint("study_id", "branch_name", "branch_seq", name="uq_setup_config_versions_study_branch_seq"),
)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
study_setup_config_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("study_setup_configs.id"), nullable=False, index=True
)
study_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("studies.id"), nullable=False, index=True)
version: Mapped[int] = mapped_column(Integer, nullable=False)
display_version: Mapped[int] = mapped_column(Integer, nullable=False)
branch_name: Mapped[str] = mapped_column(nullable=False, default="main")
branch_seq: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
version_label: Mapped[str] = mapped_column(nullable=False, default="")
source_version: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
parent_version_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True), ForeignKey("study_setup_config_versions.id", ondelete="SET NULL"), nullable=True
)
merged_from_version_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True), ForeignKey("study_setup_config_versions.id", ondelete="SET NULL"), nullable=True
)
config: Mapped[dict] = mapped_column(JSONB_TYPE, nullable=False)
published_project_snapshot: Mapped[dict | None] = mapped_column(JSONB_TYPE, nullable=True)
published_by: Mapped[Optional[uuid.UUID]] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True)
published_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())