"""add context topic workspaces Revision ID: 20260523_context_topic_workspaces Revises: 20260522_context_assemblies Create Date: 2026-05-23 """ from __future__ import annotations from alembic import op import sqlalchemy as sa revision = "20260523_context_topic_workspaces" down_revision = "20260522_context_assemblies" branch_labels = None depends_on = None TABLE_NAME = "pmflow_ai_topic_workspaces" def _table_exists(table_name: str) -> bool: return sa.inspect(op.get_bind()).has_table(table_name) def _index_exists(table_name: str, index_name: str) -> bool: if not _table_exists(table_name): return False return any(item.get("name") == index_name for item in sa.inspect(op.get_bind()).get_indexes(table_name)) def _create_index_once(index_name: str, table_name: str, columns: list[str]) -> None: if _table_exists(table_name) and not _index_exists(table_name, index_name): op.create_index(index_name, table_name, columns) def upgrade() -> None: if not _table_exists(TABLE_NAME): op.create_table( TABLE_NAME, sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), sa.Column("public_topic_id", sa.String(length=80), nullable=False), sa.Column("user_id", sa.BigInteger(), nullable=True), sa.Column("workspace_scope", sa.String(length=80), nullable=False, server_default="space"), sa.Column("status", sa.String(length=32), nullable=False, server_default="active"), sa.Column("topic_signature", sa.String(length=80), nullable=False), sa.Column("topic_summary", sa.Text(), nullable=False, server_default=""), sa.Column("current_objective", sa.Text(), nullable=False, server_default=""), sa.Column("known_assumptions", sa.JSON(), nullable=True), sa.Column("prior_proposals", sa.JSON(), nullable=True), sa.Column("open_questions", sa.JSON(), nullable=True), sa.Column("monetization_hypothesis", sa.Text(), nullable=True), sa.Column("next_actionable_package", sa.Text(), nullable=True), sa.Column("linked_thread_ids", sa.JSON(), nullable=True), sa.Column("linked_run_ids", sa.JSON(), nullable=True), sa.Column("linked_memory_ids", sa.JSON(), nullable=True), sa.Column("confidence_score", sa.Float(), nullable=False, server_default="0.6"), sa.Column("topic_payload", sa.JSON(), nullable=True), sa.Column("last_activated_at", sa.TIMESTAMP(), nullable=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("now()"), nullable=False), sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("now()"), nullable=False), sa.ForeignKeyConstraint(["user_id"], ["users.id"]), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("tenant_id", "public_topic_id", name="uq_pmflow_ai_topic_workspaces_public_id"), ) _create_index_once("ix_pmflow_ai_topic_workspaces_tenant_id", TABLE_NAME, ["tenant_id"]) _create_index_once("ix_pmflow_ai_topic_workspaces_public_topic_id", TABLE_NAME, ["public_topic_id"]) _create_index_once("ix_pmflow_ai_topic_workspaces_user_id", TABLE_NAME, ["user_id"]) _create_index_once("ix_pmflow_ai_topic_workspaces_workspace_scope", TABLE_NAME, ["workspace_scope"]) _create_index_once("ix_pmflow_ai_topic_workspaces_status", TABLE_NAME, ["status"]) _create_index_once("ix_pmflow_ai_topic_workspaces_topic_signature", TABLE_NAME, ["topic_signature"]) _create_index_once("ix_pmflow_ai_topic_workspaces_confidence_score", TABLE_NAME, ["confidence_score"]) _create_index_once("ix_pmflow_ai_topic_workspaces_last_activated_at", TABLE_NAME, ["last_activated_at"]) def downgrade() -> None: if _table_exists(TABLE_NAME): op.drop_table(TABLE_NAME)