"""add pmflow launch tables Revision ID: 20260425_add_pmflow_launch_tables Revises: 20260421_finalize_v1_productization Create Date: 2026-04-25 """ from __future__ import annotations from alembic import op import sqlalchemy as sa try: from sqlalchemy.dialects.postgresql import JSONB except Exception: # pragma: no cover JSONB = sa.JSON revision = "20260425_add_pmflow_launch_tables" down_revision = "20260421_finalize_v1_productization" branch_labels = None depends_on = None def _json_type(bind): return JSONB() if bind.dialect.name == "postgresql" else sa.JSON() def _table_exists(bind, table_name: str) -> bool: return sa.inspect(bind).has_table(table_name) def upgrade() -> None: bind = op.get_bind() json_type = _json_type(bind) if not _table_exists(bind, "pmflow_launch_drafts"): op.create_table( "pmflow_launch_drafts", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("users.id"), nullable=False), sa.Column("draft_id", sa.String(length=64), nullable=False), sa.Column("input_text", sa.Text(), nullable=False), sa.Column("input_type", sa.String(length=32), nullable=False, server_default="text"), sa.Column("source", sa.String(length=32), nullable=False, server_default="web"), sa.Column("attachments", json_type, nullable=False), sa.Column("draft_payload", json_type, nullable=False), sa.Column("edited_payload", json_type, nullable=True), sa.Column("status", sa.String(length=32), nullable=False, server_default="draft"), sa.Column("confidence", sa.Float(), nullable=True), sa.Column("warnings", json_type, nullable=False), sa.Column("confirmed_project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=True), sa.Column("expires_at", sa.TIMESTAMP(), nullable=False), sa.Column("confirmed_at", sa.TIMESTAMP(), nullable=True), sa.Column("confirm_request_hash", sa.String(length=128), nullable=True), sa.UniqueConstraint("tenant_id", "draft_id", name="uq_pmflow_launch_drafts_tenant_draft_id"), ) op.create_index("ix_pmflow_launch_drafts_tenant_id", "pmflow_launch_drafts", ["tenant_id"], unique=False) op.create_index("ix_pmflow_launch_drafts_user_id", "pmflow_launch_drafts", ["user_id"], unique=False) op.create_index("ix_pmflow_launch_drafts_draft_id", "pmflow_launch_drafts", ["draft_id"], unique=False) op.create_index("ix_pmflow_launch_drafts_status", "pmflow_launch_drafts", ["status"], unique=False) op.create_index( "ix_pmflow_launch_drafts_confirmed_project_id", "pmflow_launch_drafts", ["confirmed_project_id"], unique=False, ) op.create_index("ix_pmflow_launch_drafts_expires_at", "pmflow_launch_drafts", ["expires_at"], unique=False) if not _table_exists(bind, "pmflow_threads"): op.create_table( "pmflow_threads", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=False), sa.Column("launch_draft_id", sa.BigInteger(), sa.ForeignKey("pmflow_launch_drafts.id"), nullable=True), sa.Column("title", sa.String(length=255), nullable=False), sa.Column("objective", sa.Text(), nullable=False), sa.Column("status", sa.String(length=32), nullable=False, server_default="active"), sa.Column("source", sa.String(length=32), nullable=False, server_default="project_launch"), sa.UniqueConstraint("tenant_id", "project_id", "source", name="uq_pmflow_threads_tenant_project_source"), ) op.create_index("ix_pmflow_threads_tenant_id", "pmflow_threads", ["tenant_id"], unique=False) op.create_index("ix_pmflow_threads_project_id", "pmflow_threads", ["project_id"], unique=False) op.create_index("ix_pmflow_threads_launch_draft_id", "pmflow_threads", ["launch_draft_id"], unique=False) op.create_index("ix_pmflow_threads_status", "pmflow_threads", ["status"], unique=False) op.create_index("ix_pmflow_threads_source", "pmflow_threads", ["source"], unique=False) if not _table_exists(bind, "pmflow_thread_messages"): op.create_table( "pmflow_thread_messages", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("thread_id", sa.BigInteger(), sa.ForeignKey("pmflow_threads.id"), nullable=False), sa.Column("project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=False), sa.Column("launch_draft_id", sa.BigInteger(), sa.ForeignKey("pmflow_launch_drafts.id"), nullable=True), sa.Column("role", sa.String(length=32), nullable=False), sa.Column("content", sa.Text(), nullable=False), sa.Column("source", sa.String(length=32), nullable=False, server_default="project_launch"), sa.Column("attachments", json_type, nullable=False), sa.Column("message_metadata", json_type, nullable=False), sa.Column("status", sa.String(length=32), nullable=False, server_default="sent"), ) op.create_index("ix_pmflow_thread_messages_tenant_id", "pmflow_thread_messages", ["tenant_id"], unique=False) op.create_index("ix_pmflow_thread_messages_thread_id", "pmflow_thread_messages", ["thread_id"], unique=False) op.create_index("ix_pmflow_thread_messages_project_id", "pmflow_thread_messages", ["project_id"], unique=False) op.create_index( "ix_pmflow_thread_messages_launch_draft_id", "pmflow_thread_messages", ["launch_draft_id"], unique=False, ) op.create_index("ix_pmflow_thread_messages_status", "pmflow_thread_messages", ["status"], unique=False) def downgrade() -> None: bind = op.get_bind() for index_name, table_name in ( ("ix_pmflow_thread_messages_status", "pmflow_thread_messages"), ("ix_pmflow_thread_messages_launch_draft_id", "pmflow_thread_messages"), ("ix_pmflow_thread_messages_project_id", "pmflow_thread_messages"), ("ix_pmflow_thread_messages_thread_id", "pmflow_thread_messages"), ("ix_pmflow_thread_messages_tenant_id", "pmflow_thread_messages"), ("ix_pmflow_threads_source", "pmflow_threads"), ("ix_pmflow_threads_status", "pmflow_threads"), ("ix_pmflow_threads_launch_draft_id", "pmflow_threads"), ("ix_pmflow_threads_project_id", "pmflow_threads"), ("ix_pmflow_threads_tenant_id", "pmflow_threads"), ("ix_pmflow_launch_drafts_expires_at", "pmflow_launch_drafts"), ("ix_pmflow_launch_drafts_confirmed_project_id", "pmflow_launch_drafts"), ("ix_pmflow_launch_drafts_status", "pmflow_launch_drafts"), ("ix_pmflow_launch_drafts_draft_id", "pmflow_launch_drafts"), ("ix_pmflow_launch_drafts_user_id", "pmflow_launch_drafts"), ("ix_pmflow_launch_drafts_tenant_id", "pmflow_launch_drafts"), ): if _table_exists(bind, table_name): try: op.drop_index(index_name, table_name=table_name) except Exception: pass for table_name in ("pmflow_thread_messages", "pmflow_threads", "pmflow_launch_drafts"): if _table_exists(bind, table_name): op.drop_table(table_name)