68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
"""add pending_transaction_txids
|
|||
|
|
|
||
|
|
Revision ID: a3f5c2d9b7e1
|
||
|
|
Revises: 477bddae1a84
|
||
|
|
Create Date: 2026-10-14 15:02:41.118204
|
||
|
|
|
||
|
|
"""
|
||
|
|
from typing import Sequence, Union
|
||
|
|
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
|
||
|
|
|
||
|
|
# revision identifiers, used by Alembic.
|
||
|
|
revision: str = 'a3f5c2d9b7e1'
|
||
|
|
down_revision: Union[str, Sequence[str], None] = '477bddae1a84'
|
||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
"""Upgrade schema.
|
||
|
|
|
||
|
|
B-77 (DAT-70): every version a pending transaction has ever had. A new table, so
|
||
|
|
nothing here rebuilds an existing one and the expression indexes (B-97) are not at
|
||
|
|
risk. Rows that already exist are backfilled with what they still remember: their
|
||
|
|
`current_txid` with its raw transaction and, when it differs, the `replaced_by_txid`
|
||
|
|
of their last bump with an empty `raw_hex` (that raw transaction was overwritten).
|
||
|
|
"""
|
||
|
|
op.create_table(
|
||
|
|
"pending_transaction_txids",
|
||
|
|
sa.Column("id", sa.Integer(), nullable=False),
|
||
|
|
sa.Column("pending_transaction_id", sa.Integer(), nullable=False),
|
||
|
|
sa.Column("txid", sa.String(length=64), nullable=False),
|
||
|
|
sa.Column("raw_hex", sa.Text(), nullable=False),
|
||
|
|
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||
|
|
sa.ForeignKeyConstraint(["pending_transaction_id"], ["pending_transactions.id"]),
|
||
|
|
sa.PrimaryKeyConstraint("id"),
|
||
|
|
sa.UniqueConstraint("txid"),
|
||
|
|
)
|
||
|
|
op.create_index(
|
||
|
|
op.f("ix_pending_transaction_txids_pending_transaction_id"),
|
||
|
|
"pending_transaction_txids",
|
||
|
|
["pending_transaction_id"],
|
||
|
|
unique=False,
|
||
|
|
)
|
||
|
|
|
||
|
|
op.execute(
|
||
|
|
"""
|
||
|
|
INSERT INTO pending_transaction_txids (pending_transaction_id, txid, raw_hex, created_at)
|
||
|
|
SELECT id, current_txid, raw_tx_hex, broadcast_at FROM pending_transactions
|
||
|
|
"""
|
||
|
|
)
|
||
|
|
op.execute(
|
||
|
|
"""
|
||
|
|
INSERT INTO pending_transaction_txids (pending_transaction_id, txid, raw_hex, created_at)
|
||
|
|
SELECT id, replaced_by_txid, '', broadcast_at FROM pending_transactions
|
||
|
|
WHERE replaced_by_txid IS NOT NULL
|
||
|
|
AND replaced_by_txid != current_txid
|
||
|
|
AND replaced_by_txid NOT IN (SELECT txid FROM pending_transaction_txids)
|
||
|
|
"""
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
"""Downgrade schema."""
|
||
|
|
op.drop_index(op.f("ix_pending_transaction_txids_pending_transaction_id"), table_name="pending_transaction_txids")
|
||
|
|
op.drop_table("pending_transaction_txids")
|