Files

55 lines
2.1 KiB
Python
Raw Permalink Normal View History

"""add pool_address to rounds
Revision ID: 477bddae1a84
Revises: 283844a44b4a
Create Date: 2026-10-14 12:28:11.529107
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '477bddae1a84'
down_revision: Union[str, Sequence[str], None] = '283844a44b4a'
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-80 (DAT-60): every round gets its own pool address. Rounds that already exist were
all paid to the one shared pool address, the reserve address m/84'/746'/0'/1/0, so
that is what they are backfilled with and what their payout keeps spending.
The column is added with add_column, never batch_alter_table: on SQLite the latter
rebuilds the table and silently drops ix_rounds_single_active (B-97, DAT-56). The
server default is only there to let NOT NULL be added to a table that has rows.
"""
op.add_column("rounds", sa.Column("pool_address", sa.String(length=128), nullable=False, server_default=""))
connection = op.get_bind()
has_rounds = connection.execute(sa.text("SELECT COUNT(*) FROM rounds")).scalar()
if has_rounds:
# Derived only when there is something to backfill, so a fresh database (and the
# migration test) needs no master key. Imported here for the same reason.
from app.wallet.hd import derive_pool_address
connection.execute(
sa.text("UPDATE rounds SET pool_address = :address WHERE pool_address = ''"),
{"address": derive_pool_address()},
)
def downgrade() -> None:
"""Downgrade schema."""
# drop_column on SQLite is a table rebuild too, which loses the expression index:
# recreate it (B-97).
op.drop_column("rounds", "pool_address")
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})")