Files
plm-lottery/migrations/versions/274efdcbfbcc_initial_schema.py
T
davideandClaude Sonnet 5 d2db762d96 Scaffold project layout, DB schema and settings
Package skeleton, pyproject/alembic config, env-driven settings
(app/config.py), and the SQLAlchemy models + initial Alembic migration
covering users, UTXO events, rounds/participants, round config,
pending transactions, withdrawals and the audit log.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 10:25:23 +02:00

154 lines
7.2 KiB
Python

"""initial schema
Revision ID: 274efdcbfbcc
Revises:
Create Date: 2026-07-20 22:11:47.766165
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '274efdcbfbcc'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('round_config',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('fee_address', sa.String(length=128), nullable=False),
sa.Column('bet_amount_sats', sa.BigInteger(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sa.String(length=64), nullable=False),
sa.Column('password_hash', sa.String(length=256), nullable=False),
sa.Column('derivation_index', sa.Integer(), nullable=False),
sa.Column('address', sa.String(length=128), nullable=False),
sa.Column('cached_balance_sats', sa.BigInteger(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('address'),
sa.UniqueConstraint('derivation_index')
)
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
op.create_table('rounds',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('status', sa.String(length=16), nullable=False),
sa.Column('opened_at', sa.DateTime(), nullable=False),
sa.Column('closed_at', sa.DateTime(), nullable=True),
sa.Column('draw_block_height', sa.Integer(), nullable=True),
sa.Column('draw_block_hash', sa.String(length=64), nullable=True),
sa.Column('seed_int', sa.String(length=128), nullable=True),
sa.Column('winner_user_id', sa.Integer(), nullable=True),
sa.Column('pool_amount_sats', sa.BigInteger(), nullable=True),
sa.Column('winner_amount_sats', sa.BigInteger(), nullable=True),
sa.Column('fee_amount_sats', sa.BigInteger(), nullable=True),
sa.Column('payout_txid', sa.String(length=64), nullable=True),
sa.ForeignKeyConstraint(['winner_user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('utxo_events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('txid', sa.String(length=64), nullable=False),
sa.Column('vout', sa.Integer(), nullable=False),
sa.Column('amount_sats', sa.BigInteger(), nullable=False),
sa.Column('confirmed_height', sa.Integer(), nullable=False),
sa.Column('confirmed_at', sa.DateTime(), nullable=False),
sa.Column('spent_txid', sa.String(length=64), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('txid', 'vout')
)
op.create_index(op.f('ix_utxo_events_user_id'), 'utxo_events', ['user_id'], unique=False)
op.create_table('withdrawals',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('external_address', sa.String(length=128), nullable=False),
sa.Column('amount_requested_sats', sa.BigInteger(), nullable=False),
sa.Column('amount_sent_sats', sa.BigInteger(), nullable=True),
sa.Column('txid', sa.String(length=64), nullable=True),
sa.Column('status', sa.String(length=16), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('confirmed_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_withdrawals_user_id'), 'withdrawals', ['user_id'], unique=False)
op.create_table('audit_log',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('event_type', sa.String(length=32), nullable=False),
sa.Column('payload_json', sa.String(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('round_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['round_id'], ['rounds.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('pending_transactions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('kind', sa.String(length=16), nullable=False),
sa.Column('round_id', sa.Integer(), nullable=True),
sa.Column('withdrawal_id', sa.Integer(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('current_txid', sa.String(length=64), nullable=False),
sa.Column('fee_rate_sat_vb', sa.Integer(), nullable=False),
sa.Column('raw_tx_hex', sa.String(), nullable=False),
sa.Column('broadcast_at', sa.DateTime(), nullable=False),
sa.Column('status', sa.String(length=16), nullable=False),
sa.Column('replaced_by_txid', sa.String(length=64), nullable=True),
sa.Column('attempt_count', sa.Integer(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['round_id'], ['rounds.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['withdrawal_id'], ['withdrawals.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('round_participants',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('round_id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('bet_amount_sats', sa.BigInteger(), nullable=False),
sa.Column('bet_txid', sa.String(length=64), nullable=False),
sa.Column('broadcast_at', sa.DateTime(), nullable=False),
sa.Column('confirmed_at', sa.DateTime(), nullable=True),
sa.Column('status', sa.String(length=16), nullable=False),
sa.ForeignKeyConstraint(['round_id'], ['rounds.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('round_id', 'user_id')
)
op.create_index(op.f('ix_round_participants_round_id'), 'round_participants', ['round_id'], unique=False)
op.create_index(op.f('ix_round_participants_user_id'), 'round_participants', ['user_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_round_participants_user_id'), table_name='round_participants')
op.drop_index(op.f('ix_round_participants_round_id'), table_name='round_participants')
op.drop_table('round_participants')
op.drop_table('pending_transactions')
op.drop_table('audit_log')
op.drop_index(op.f('ix_withdrawals_user_id'), table_name='withdrawals')
op.drop_table('withdrawals')
op.drop_index(op.f('ix_utxo_events_user_id'), table_name='utxo_events')
op.drop_table('utxo_events')
op.drop_table('rounds')
op.drop_index(op.f('ix_users_username'), table_name='users')
op.drop_table('users')
op.drop_table('round_config')
# ### end Alembic commands ###