Files
trumpsignal-backend/alembic/versions/010_add_invalidation_price_fields.py
T
k 5fb1d52026 Pre-launch hardening: KOL module, Telegram, scanners, WS resilience
Big-picture changes since b941223:

KOL pipeline (new) — Substack/podcast/blog RSS → AI ticker extraction →
on-chain wallet diff → talks-vs-trades divergence detection. Daily polls,
19 feeds, divergence emits Post + Telegram fan-out.

Telegram push (new) — walletless free tier + wallet-linked Pro upgrade,
in-bot preference commands (/trump /btc /funding /kol /conf /quiet),
signed-envelope API for dashboard. Disconnect-wallet keeps free
subscription.

BTC funding-rate reversal scanner (new) — hourly cron, 30d cumulative
funding threshold + mean-revert + 7d price confirm, emits via
/api/signals/ingest. BTC bottom-reversal scanner promoted to System 2.

WS broadcast rewrite — per-client send timeout + parallel fan-out
(asyncio.gather). Fixes "Binance WS no close frame" reconnect storms +
APScheduler 11-min job misses, both caused by one slow client stalling
the kline loop.

Error visibility — three silent-error sites (trumpstruth/truth_social
fetchers, funding_reversal scanner) now include exception type name so
httpx ConnectError-style empty-message errors stop logging blank lines.
Telegram bot loop now classifies ReadTimeout vs network vs unknown +
logger.exception for the unknown bucket.

Security hygiene — trumpsignal.db untracked from git (held subscriber
wallets + encrypted HL keys + 22 bot trades); .gitignore now blocks
*.db/.next/backups. CORS only allows FRONTEND_URL in production.

New ops scripts —
  - scripts/preflight.py: env/DB/Telegram/AI auth verification gate
  - scripts/backup_db.sh: cron-friendly daily DB backup (SQLite + Postgres)
  - scripts/seed_kol_wallets.py: idempotent KOL on-chain wallet seeder

15 new Alembic migrations (007-021) covering convex strategy fields,
phase-1 safety, two-system frozen exits, invalidation prices, dynamic
SYS2 leverage, staged de-risk + pyramiding, peak gain tracking, risk
mode, auto-trade + grow flags, KOL module, KOL on-chain, KOL divergence,
Telegram bindings + walletless.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 00:52:56 +08:00

53 lines
2.0 KiB
Python

"""Add invalidation_price fields for BTC bottom-reversal trades.
Revision ID: 010
Revises: 009
Create Date: 2026-05-16 00:00:00.000000
NOTE: `bot_trades.eff_invalidation_price` is ALSO created by migration 009
(two_system_and_frozen_exits). The original 010 added it unconditionally,
which made `alembic upgrade head` on a FRESH database fail at 010 with
"duplicate column name: eff_invalidation_price" (009 already added it).
This revision is now IDEMPOTENT: each column is added only if it is not
already present, so it applies cleanly on both fresh DBs (009 already made
eff_invalidation_price → skip; posts.invalidation_price is new → add) and
DBs that pre-date 009's column. The genuinely-new column introduced by 010
is `posts.invalidation_price`; `eff_invalidation_price` is owned by 009 and
left to 009's downgrade.
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision: str = "010"
down_revision: Union[str, None] = "009"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def _columns(table: str) -> set:
return {c["name"] for c in inspect(op.get_bind()).get_columns(table)}
def upgrade() -> None:
if "invalidation_price" not in _columns("posts"):
with op.batch_alter_table("posts") as b:
b.add_column(sa.Column("invalidation_price", sa.Float(), nullable=True))
# eff_invalidation_price may already exist (created by 009). Only add it
# if some older DB reached 010 without it.
if "eff_invalidation_price" not in _columns("bot_trades"):
with op.batch_alter_table("bot_trades") as b:
b.add_column(sa.Column("eff_invalidation_price", sa.Float(), nullable=True))
def downgrade() -> None:
# Only undo 010's genuine addition. eff_invalidation_price is owned by
# 009 — dropping it here would double-drop in `downgrade base`.
if "invalidation_price" in _columns("posts"):
with op.batch_alter_table("posts") as b:
b.drop_column("invalidation_price")