Files
plm-lottery/migrations/versions/b7d2e41c9a05_add_failed_at_and_paused_reason.py
T

47 lines
1.8 KiB
Python
Raw Normal View History

"""add failed_at to pending_transactions and paused_reason to round_config
Revision ID: b7d2e41c9a05
Revises: a3f5c2d9b7e1
Create Date: 2026-10-14 15:48:12.003915
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b7d2e41c9a05'
down_revision: Union[str, Sequence[str], None] = 'a3f5c2d9b7e1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
_ACTIVE = "'open', 'closing', 'drawing', 'paying_out'"
def upgrade() -> None:
"""Upgrade schema.
B-78: `failed_at` (DAT-36, DAT-72) starts the 14-day watch over a failed row; rows
that are already failed get their last update as the best available estimate.
`paused_reason` (DAT-69) says why the lottery is paused; an instance that is already
paused was paused by its operator.
Both are plain add_column calls — never batch_alter_table, which on SQLite rebuilds
the table and silently drops the expression indexes (B-97, DAT-56).
"""
op.add_column("pending_transactions", sa.Column("failed_at", sa.DateTime(), nullable=True))
op.execute("UPDATE pending_transactions SET failed_at = updated_at WHERE status = 'failed'")
op.add_column("round_config", sa.Column("paused_reason", sa.String(length=16), nullable=True))
op.execute("UPDATE round_config SET paused_reason = 'operator' WHERE paused = 1")
def downgrade() -> None:
"""Downgrade schema."""
# drop_column on SQLite is a table rebuild: recreate the expression indexes (B-97).
op.drop_column("round_config", "paused_reason")
op.drop_column("pending_transactions", "failed_at")
op.execute("DROP INDEX IF EXISTS ix_rounds_single_active")
op.execute(f"CREATE UNIQUE INDEX ix_rounds_single_active ON rounds ((1)) WHERE status IN ({_ACTIVE})")