76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
"""add subject_pds table
|
|
|
|
Revision ID: 20260228_01
|
|
Revises: 20260227_06
|
|
Create Date: 2026-02-28 10:30:00.000000
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "20260228_01"
|
|
down_revision: Union[str, None] = "20260227_06"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
table_names = set(inspector.get_table_names())
|
|
|
|
if "subject_pds" not in table_names:
|
|
op.create_table(
|
|
"subject_pds",
|
|
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("study_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("subject_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("pd_no", sa.String(length=64), nullable=False),
|
|
sa.Column("pd_type", sa.String(length=100), nullable=False),
|
|
sa.Column("pd_level", sa.String(length=20), nullable=False, server_default=sa.text("'GENERAL'")),
|
|
sa.Column("approval_status", sa.String(length=20), nullable=False, server_default=sa.text("'DRAFT'")),
|
|
sa.Column("description", sa.Text(), nullable=True),
|
|
sa.Column("status", sa.String(length=20), nullable=False, server_default=sa.text("'OPEN'")),
|
|
sa.Column("created_by", postgresql.UUID(as_uuid=True), nullable=True),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
|
|
sa.ForeignKeyConstraint(["study_id"], ["studies.id"]),
|
|
sa.ForeignKeyConstraint(["subject_id"], ["subjects.id"]),
|
|
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint("study_id", "pd_no", name="uq_subject_pds_study_pd_no"),
|
|
)
|
|
table_names.add("subject_pds")
|
|
|
|
indexes = {idx["name"] for idx in inspector.get_indexes("subject_pds")} if "subject_pds" in table_names else set()
|
|
study_index_name = op.f("ix_subject_pds_study_id")
|
|
if study_index_name not in indexes:
|
|
op.create_index(study_index_name, "subject_pds", ["study_id"], unique=False)
|
|
|
|
subject_index_name = op.f("ix_subject_pds_subject_id")
|
|
if subject_index_name not in indexes:
|
|
op.create_index(subject_index_name, "subject_pds", ["subject_id"], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
table_names = set(inspector.get_table_names())
|
|
|
|
if "subject_pds" in table_names:
|
|
indexes = {idx["name"] for idx in inspector.get_indexes("subject_pds")}
|
|
study_index_name = op.f("ix_subject_pds_study_id")
|
|
if study_index_name in indexes:
|
|
op.drop_index(study_index_name, table_name="subject_pds")
|
|
|
|
subject_index_name = op.f("ix_subject_pds_subject_id")
|
|
if subject_index_name in indexes:
|
|
op.drop_index(subject_index_name, table_name="subject_pds")
|
|
|
|
op.drop_table("subject_pds")
|