"""014 generation_tasks.status ENUM 补 failed 终态（B6 错误态）

Revision ID: 014
Revises: 013
Create Date: 2026-06-16

status 是 MySQL ENUM,原7值无失败终态。Celery生成彻底失败(重试耗尽)时
要落 failed,区分"没跑(pending)"和"跑挂了(failed)"。不补 ENUM 会 Data truncated。
"""
from alembic import op

revision = "014"
down_revision = "013"
branch_labels = None
depends_on = None

_ENUM_WITH_FAILED = (
    "ENUM('pending','generating','pending_selection','pending_review',"
    "'approved','rejected','archived','failed') NOT NULL DEFAULT 'pending'"
)
_ENUM_ORIG = (
    "ENUM('pending','generating','pending_selection','pending_review',"
    "'approved','rejected','archived') NOT NULL DEFAULT 'pending'"
)


def upgrade():
    op.execute(f"ALTER TABLE generation_tasks MODIFY COLUMN status {_ENUM_WITH_FAILED}")


def downgrade():
    # 回退前先把 failed 行归位到 pending,避免越界
    op.execute("UPDATE generation_tasks SET status='pending' WHERE status='failed'")
    op.execute(f"ALTER TABLE generation_tasks MODIFY COLUMN status {_ENUM_ORIG}")
