"""Add result_success column to plans table.

This migration adds a dedicated ``result_success`` boolean column to the
``plans`` table to accurately track the final result status of a plan's
apply phase.

Previously, ``PlanRepository._to_domain`` derived ``PlanResult.success``
from ``error_message is None``.  Because ``error_message`` is shared
between the build phase and the result phase, a plan that encountered a
build error but later succeeded in the apply phase would be incorrectly
reconstructed as failed.

The new ``result_success`` column is nullable to preserve backward
compatibility with existing database records.  When ``result_success``
is NULL (pre-migration records), the repository falls back to the legacy
``error_message is None`` heuristic.

Revision ID: m9_003_plan_result_success_column
Revises: m10_001_virtual_builtin_actors
Create Date: 2026-05-05 00:00:00

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "m9_003_plan_result_success_column"
down_revision: str | Sequence[str] | None = "m10_001_virtual_builtin_actors"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Add ``result_success`` column to the ``plans`` table.

    The column is nullable so that existing rows (which have no explicit
    result-phase success signal) are not affected.  New rows written by
    the updated repository will always populate this column.
    """
    op.add_column(
        "plans",
        sa.Column("result_success", sa.Boolean(), nullable=True),
    )


def downgrade() -> None:
    """Remove ``result_success`` column from the ``plans`` table."""
    op.drop_column("plans", "result_success")
