Strategy Tracker — Local CLI
All commands run from backend/, venv activated (hard rule 1 — never run modules from
inside their package folder). Target DB is whatever LOCAL_DB_* in backend/.env
points to — tedb on localhost by default.
cd backend
venv\Scripts\activate
1. Apply the migration
alembic upgrade head
Creates st_group, st_master, regime_master if they don't already exist (as
strategy_classification/strategy, then renamed to st_group/st_master by a
later migration in the same chain). Safe to re-run — Alembic tracks the applied
revision in alembic_version and no-ops if already at head.
Check current revision:
alembic current
Roll back (drops all 3 tables):
alembic downgrade base
2. Seed the classification vocabulary
python -m strategies.seed_strategy_classification
Idempotent — inserts the 13 st_group rows (horizon / nature / family) via
ON CONFLICT DO NOTHING. Re-running prints Inserted 0 new ... row(s) once seeded;
regime_master is intentionally left empty by this script.
3. Run the tests
python -m pytest tests/test_strategy_models.py -v
Both tests insert a row missing hypothesis (one for st_master, one for
regime_master) and assert Postgres raises IntegrityError — proving the NOT NULL
constraint is enforced by the schema, not just app-level validation. Each test runs
inside its own SAVEPOINT-backed session (tests/conftest.py) and is rolled back
afterward, so nothing it writes lingers in tedb.
4. Poke it manually (optional)
python -c "
from sqlalchemy import create_engine, text
from app.core.config import settings
engine = create_engine(settings.SYNC_DATABASE_URL)
with engine.connect() as conn:
for row in conn.execute(text('SELECT axis, code, label FROM st_group ORDER BY axis, code')):
print(row)
"
Quick sanity check — everything at once
cd backend
alembic upgrade head
python -m strategies.seed_strategy_classification
python -m pytest tests/test_strategy_models.py -v
Expect: migration applies (or no-ops if already applied), seed reports 13 inserted the
first time / 0 thereafter, both tests PASSED.