2026-10-14 12:22:08 +02:00
|
|
|
"""B-97 (DAT-75, DAT-56, ARC-62, ARC-64): the Alembic chain is executed, not assumed.
|
|
|
|
|
|
|
|
|
|
The two expression indexes that carry invariants — `ix_rounds_single_active` ("at
|
|
|
|
|
most one active round", B-09) and `ix_users_username_lower` ("usernames are
|
|
|
|
|
case-insensitive", B-57) — are invisible to autogenerate, and `batch_alter_table`
|
|
|
|
|
on SQLite rebuilds the table and drops them with only a warning. Every other test
|
|
|
|
|
builds its schema with `Base.metadata.create_all`, so a migration that lost one of
|
|
|
|
|
them would reach production unnoticed. This one runs `alembic upgrade head` on an
|
|
|
|
|
empty database, exactly as the container does at startup.
|
|
|
|
|
|
|
|
|
|
Alembic runs in a subprocess: env.py calls `asyncio.run` and `fileConfig`, neither of
|
|
|
|
|
which belongs inside the pytest process.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import sqlite3
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
|
def migrated_db(tmp_path_factory) -> Path:
|
|
|
|
|
db_path = tmp_path_factory.mktemp("migrations") / "migrated.db"
|
|
|
|
|
env = {**os.environ, "DATABASE_URL": f"sqlite+aiosqlite:///{db_path}"}
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
[sys.executable, "-m", "alembic", "upgrade", "head"],
|
|
|
|
|
cwd=_ROOT,
|
|
|
|
|
env=env,
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
timeout=120,
|
|
|
|
|
)
|
|
|
|
|
assert result.returncode == 0, f"alembic upgrade head failed:\n{result.stderr[-3000:]}"
|
|
|
|
|
return db_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _index_sql(db_path: Path, name: str) -> str | None:
|
|
|
|
|
with sqlite3.connect(db_path) as conn:
|
|
|
|
|
row = conn.execute("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?", (name,)).fetchone()
|
|
|
|
|
return re.sub(r"\s+", " ", row[0]).lower() if row else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_the_expression_indexes_survive_the_whole_migration_chain(migrated_db):
|
|
|
|
|
"""DAT-75 / ARC-62 / B-97: both indexes exist, with their definition, at head."""
|
|
|
|
|
single_active = _index_sql(migrated_db, "ix_rounds_single_active")
|
|
|
|
|
assert single_active is not None, "ix_rounds_single_active was lost by a migration"
|
|
|
|
|
assert "unique index" in single_active
|
|
|
|
|
assert "on rounds ((1))" in single_active
|
|
|
|
|
for status in ("open", "closing", "drawing", "paying_out"):
|
|
|
|
|
assert f"'{status}'" in single_active
|
|
|
|
|
|
|
|
|
|
username_lower = _index_sql(migrated_db, "ix_users_username_lower")
|
|
|
|
|
assert username_lower is not None, "ix_users_username_lower was lost by a migration"
|
|
|
|
|
assert "unique index" in username_lower
|
|
|
|
|
assert "lower(username)" in username_lower
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_the_migrated_database_still_enforces_both_invariants(migrated_db):
|
|
|
|
|
"""DAT-75 / B-97: a second active round and a case-variant username are refused."""
|
|
|
|
|
with sqlite3.connect(migrated_db) as conn:
|
|
|
|
|
conn.execute("INSERT INTO rounds (status, opened_at) VALUES ('open', '2026-01-01 00:00:00')")
|
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
|
|
|
conn.execute("INSERT INTO rounds (status, opened_at) VALUES ('closing', '2026-01-01 00:00:00')")
|
|
|
|
|
# A closed round is not active, so any number of them may coexist.
|
|
|
|
|
conn.execute("INSERT INTO rounds (status, opened_at) VALUES ('closed', '2026-01-01 00:00:00')")
|
|
|
|
|
|
|
|
|
|
columns = "(username, password_hash, derivation_index, address, cached_balance_sats, token_version, created_at)"
|
|
|
|
|
conn.execute(f"INSERT INTO users {columns} VALUES ('Alice', 'x', 0, 'plm1qa', 0, 0, '2026-01-01 00:00:00')")
|
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
|
|
|
conn.execute(f"INSERT INTO users {columns} VALUES ('aLICE', 'x', 1, 'plm1qb', 0, 0, '2026-01-01 00:00:00')")
|
Track every version of a bumped transaction and write the RBF replacement before broadcasting it (B-77, TX-12, TX-13, TX-19, TX-21, DAT-13, DAT-14, DAT-34, DAT-44, DAT-45, DAT-70, DAT-71)
2026-10-14 14:46:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _alembic(db_path: Path, *args: str) -> None:
|
|
|
|
|
env = {**os.environ, "DATABASE_URL": f"sqlite+aiosqlite:///{db_path}"}
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
[sys.executable, "-m", "alembic", *args], cwd=_ROOT, env=env, capture_output=True, text=True, timeout=120
|
|
|
|
|
)
|
|
|
|
|
assert result.returncode == 0, f"alembic {' '.join(args)} failed:\n{result.stderr[-3000:]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pending_transaction_txids_table_matches_the_model(migrated_db):
|
|
|
|
|
"""B-77 (DAT-70): the migrated table has the model's columns, the unique txid and the
|
|
|
|
|
index on the owning row."""
|
|
|
|
|
from app.db.models import PendingTransactionTxid
|
|
|
|
|
|
|
|
|
|
with sqlite3.connect(migrated_db) as conn:
|
|
|
|
|
columns = {row[1]: row for row in conn.execute("PRAGMA table_info(pending_transaction_txids)")}
|
|
|
|
|
sql = conn.execute("SELECT sql FROM sqlite_master WHERE name = 'pending_transaction_txids'").fetchone()[0]
|
|
|
|
|
indexes = {row[1] for row in conn.execute("PRAGMA index_list(pending_transaction_txids)")}
|
|
|
|
|
assert set(columns) == {c.name for c in PendingTransactionTxid.__table__.columns}
|
|
|
|
|
assert "unique" in sql.lower()
|
|
|
|
|
assert "ix_pending_transaction_txids_pending_transaction_id" in indexes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_existing_pending_transactions_are_backfilled_with_the_txids_they_remember(tmp_path):
|
|
|
|
|
"""B-77 (DAT-70): upgrading a database that already has rows records each one's
|
|
|
|
|
current txid with its raw transaction, and the txid a bump replaced with an empty
|
|
|
|
|
raw_hex — and the expression indexes survive the step."""
|
|
|
|
|
db_path = tmp_path / "old.db"
|
|
|
|
|
_alembic(db_path, "upgrade", "477bddae1a84")
|
|
|
|
|
with sqlite3.connect(db_path) as conn:
|
|
|
|
|
columns = (
|
|
|
|
|
"(kind, current_txid, fee_rate_sat_vb, raw_tx_hex, broadcast_at, last_broadcast_at, status, "
|
|
|
|
|
"replaced_by_txid, attempt_count, updated_at)"
|
|
|
|
|
)
|
|
|
|
|
stamp = "'2026-01-01 00:00:00'"
|
|
|
|
|
conn.execute(
|
|
|
|
|
f"INSERT INTO pending_transactions {columns} VALUES "
|
|
|
|
|
f"('bet', 'cc', 2, 'rawcc', {stamp}, {stamp}, 'pending', 'bb', 2, {stamp})"
|
|
|
|
|
)
|
|
|
|
|
conn.execute(
|
|
|
|
|
f"INSERT INTO pending_transactions {columns} VALUES "
|
|
|
|
|
f"('withdrawal', 'dd', 1, 'rawdd', {stamp}, {stamp}, 'building', NULL, 1, {stamp})"
|
|
|
|
|
)
|
|
|
|
|
_alembic(db_path, "upgrade", "head")
|
|
|
|
|
with sqlite3.connect(db_path) as conn:
|
|
|
|
|
rows = sorted(conn.execute("SELECT pending_transaction_id, txid, raw_hex FROM pending_transaction_txids"))
|
|
|
|
|
assert rows == [(1, "bb", ""), (1, "cc", "rawcc"), (2, "dd", "rawdd")]
|
|
|
|
|
assert _index_sql(db_path, "ix_rounds_single_active") is not None
|
|
|
|
|
assert _index_sql(db_path, "ix_users_username_lower") is not None
|