"""remove document workflows Revision ID: 20260116_05 Revises: 20260116_04 Create Date: 2026-01-16 17: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 = "20260116_05" down_revision: Union[str, None] = "20260116_04" 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) tables = set(inspector.get_table_names()) if "workflow_actions" in tables: op.drop_table("workflow_actions") if "version_workflows" in tables: op.drop_table("version_workflows") if "workflow_nodes" in tables: op.drop_table("workflow_nodes") if "workflow_templates" in tables: op.drop_table("workflow_templates") op.execute("DROP TYPE IF EXISTS workflow_action_type") op.execute("DROP TYPE IF EXISTS workflow_status") def downgrade() -> None: workflow_status = sa.Enum("PENDING", "APPROVED", "REJECTED", "CANCELED", name="workflow_status") workflow_action_type = sa.Enum("SUBMIT", "APPROVE", "REJECT", name="workflow_action_type") workflow_status.create(op.get_bind(), checkfirst=True) workflow_action_type.create(op.get_bind(), checkfirst=True) op.create_table( "workflow_templates", sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False), sa.Column("trial_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("name", sa.String(length=200), nullable=False), sa.Column("description", sa.Text(), nullable=True), sa.Column("is_active", sa.Boolean(), server_default="true", nullable=False), sa.Column("created_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(["trial_id"], ["studies.id"]), sa.ForeignKeyConstraint(["created_by"], ["users.id"]), ) op.create_table( "workflow_nodes", sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False), sa.Column("template_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("node_order", sa.Integer(), nullable=False), sa.Column("role", sa.String(length=50), nullable=False), sa.Column("name", sa.String(length=100), nullable=True), sa.Column("is_required", sa.Boolean(), server_default="true", nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), sa.ForeignKeyConstraint(["template_id"], ["workflow_templates.id"]), sa.UniqueConstraint("template_id", "node_order", name="uq_workflow_nodes_order"), ) op.create_table( "version_workflows", sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False), sa.Column("document_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("version_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("template_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("status", workflow_status, server_default="PENDING", nullable=False), sa.Column("current_node", sa.Integer(), nullable=True), sa.Column("submitted_by", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("submitted_at", sa.DateTime(timezone=True), nullable=True), sa.Column("completed_at", sa.DateTime(timezone=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(["document_id"], ["documents.id"]), sa.ForeignKeyConstraint(["version_id"], ["document_versions.id"]), sa.ForeignKeyConstraint(["template_id"], ["workflow_templates.id"]), sa.ForeignKeyConstraint(["submitted_by"], ["users.id"]), sa.UniqueConstraint("version_id", name="uq_version_workflow_version"), ) op.create_table( "workflow_actions", sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False), sa.Column("workflow_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("node_order", sa.Integer(), nullable=True), sa.Column("action", workflow_action_type, nullable=False), sa.Column("actor_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("comment", sa.Text(), nullable=True), sa.Column("acted_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), sa.ForeignKeyConstraint(["workflow_id"], ["version_workflows.id"]), sa.ForeignKeyConstraint(["actor_id"], ["users.id"]), )