"""add study_setup_configs table Revision ID: 20260206_02 Revises: 20260206_01 Create Date: 2026-02-06 19:10: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 = "20260206_02" down_revision: Union[str, None] = "20260206_01" 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) if "study_setup_configs" not in inspector.get_table_names(): op.create_table( "study_setup_configs", sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("study_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("version", sa.Integer(), nullable=False, server_default="1"), sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=False), sa.Column("saved_by", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), sa.ForeignKeyConstraint(["saved_by"], ["users.id"]), sa.ForeignKeyConstraint(["study_id"], ["studies.id"]), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("study_id", name="uq_study_setup_config_study"), ) indexes = {idx["name"] for idx in inspector.get_indexes("study_setup_configs")} index_name = op.f("ix_study_setup_configs_study_id") if index_name not in indexes: op.create_index(index_name, "study_setup_configs", ["study_id"], unique=False) uniques = {uq["name"] for uq in inspector.get_unique_constraints("study_setup_configs") if uq.get("name")} if "uq_study_setup_config_study" not in uniques: op.create_unique_constraint("uq_study_setup_config_study", "study_setup_configs", ["study_id"]) op.execute("ALTER TABLE study_setup_configs ALTER COLUMN version DROP DEFAULT") def downgrade() -> None: op.drop_index(op.f("ix_study_setup_configs_study_id"), table_name="study_setup_configs") op.drop_table("study_setup_configs")