"""Add domain_goals and repo_goals; add repo_goal_id FK to workstreams Revision ID: c5d6e7f8a9b0 Revises: b4c5d6e7f8a9 Create Date: 2026-03-08 00:00:00.000000 """ from typing import Sequence, Union import sqlalchemy as sa from alembic import op from sqlalchemy.dialects import postgresql revision: str = "c5d6e7f8a9b0" down_revision: Union[str, None] = "b4c5d6e7f8a9" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.create_table( "domain_goals", sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("domain_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("title", sa.String(500), nullable=False), sa.Column("description", sa.Text(), nullable=False), sa.Column("status", sa.String(20), nullable=False, server_default="active"), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["domain_id"], ["domains.id"], ondelete="RESTRICT"), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_domain_goals_domain_id", "domain_goals", ["domain_id"]) op.create_index("ix_domain_goals_status", "domain_goals", ["status"]) # Partial unique index: only one active goal per domain at a time op.execute( "CREATE UNIQUE INDEX ix_domain_goals_one_active " "ON domain_goals (domain_id) WHERE status = 'active'" ) op.create_table( "repo_goals", sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("repo_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("domain_goal_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("title", sa.String(500), nullable=False), sa.Column("description", sa.Text(), nullable=False), sa.Column("priority", sa.Integer(), nullable=False, server_default="100"), sa.Column("status", sa.String(20), nullable=False, server_default="active"), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["repo_id"], ["managed_repos.id"], ondelete="RESTRICT"), sa.ForeignKeyConstraint(["domain_goal_id"], ["domain_goals.id"], ondelete="SET NULL"), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_repo_goals_repo_id", "repo_goals", ["repo_id"]) op.create_index("ix_repo_goals_domain_goal_id", "repo_goals", ["domain_goal_id"]) op.create_index("ix_repo_goals_status", "repo_goals", ["status"]) op.create_index("ix_repo_goals_priority", "repo_goals", ["priority"]) op.add_column( "workstreams", sa.Column( "repo_goal_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("repo_goals.id", ondelete="SET NULL"), nullable=True, ), ) op.create_index("ix_workstreams_repo_goal_id", "workstreams", ["repo_goal_id"]) def downgrade() -> None: op.drop_index("ix_workstreams_repo_goal_id", table_name="workstreams") op.drop_column("workstreams", "repo_goal_id") op.drop_index("ix_repo_goals_priority", table_name="repo_goals") op.drop_index("ix_repo_goals_status", table_name="repo_goals") op.drop_index("ix_repo_goals_domain_goal_id", table_name="repo_goals") op.drop_index("ix_repo_goals_repo_id", table_name="repo_goals") op.drop_table("repo_goals") op.execute("DROP INDEX IF EXISTS ix_domain_goals_one_active") op.drop_index("ix_domain_goals_status", table_name="domain_goals") op.drop_index("ix_domain_goals_domain_id", table_name="domain_goals") op.drop_table("domain_goals")