feat(reset): cutover 도구 폐기 → trex-core-reset(DB 재초기화) 대체 (REQ-064 CHG-20260610-03)
실 DB 점검 중 마이그레이션 009 부분 유니크 인덱스가 legacy 풀 데이터(같은 자산이 closed+재진입 holding 으로 portfolio_id 중복)에 적용 불가 → cutover· 데몬 부팅 모두 차단되는 결함 확인. 데이터 보존 cutover 를 폐기하고 DB wipe + 자산 중심 재초기화로 전환(사용자 결정). 빈 테이블에는 009 인덱스가 정상 생성되고 자산 중심에서 (instance_key, portfolio_id)는 설계상 유일. - trex_core/runtime/reset.py 신규(trex-core-reset): slot-core 7테이블 wipe + apply_migrations 재적용 → 자산 중심 스키마 착지 검증. dry-run 기본/--yes 적용, 파괴적·DB 전역. 보유 carry-over 없음(재초기화 후 reconcile_core_holdings 재편입). - runtime/cutover.py + trex-core-cutover console script + test_cutover_rehearsal.py 삭제. - pyproject: trex-core-reset 등록. - tests/runtime/test_reset.py 신규 5건(PG-gated): wipe+착지·dry-run 무변경·멱등· wipe 후 admission·테이블 커버리지. - change-req REQ-064 CHG-20260610-03(cutover→재초기화 재정의) + srs index 갱신. - baseline 4문서(data-model/runtime-flows/project-structure/interface-contract) + BAT-030 design/planning/index/implementation/verification 도구 서술 reset 으로 교체. 검증: ruff clean, 단위 367 passed, PG+RabbitMQ 통합 411 passed/1 skip(KIS sandbox). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,124 +0,0 @@
|
||||
"""Cutover rehearsal tests (dcy-020 / REQ-064, BAT-030 M4).
|
||||
|
||||
PostgreSQL-gated (TREX_POSTGRESQL_TEST_DSN) — the rehearsal the design
|
||||
requires before the operator runs the live cutover: full rebuild, broker
|
||||
holding carry-over, in-flight abort, unmapped abort/skip, dry-run rollback.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from trex_core.adapters.persistence.postgresql import PostgreSQLSlotRepository
|
||||
from trex_core.domain.slot import SlotService, SlotState
|
||||
from trex_core.runtime.cutover import CutoverAbort, run_cutover
|
||||
|
||||
INST = "default"
|
||||
UNIVERSE = [("pf-a", "005930"), ("pf-b", "000660"), ("pf-c", "035720")]
|
||||
|
||||
|
||||
def _legacy_rows(dsn):
|
||||
"""Seed pre-cutover state: an old pool-ish slot + a holding slot."""
|
||||
repo = PostgreSQLSlotRepository(dsn)
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, {"pf-old", "pf-b"})
|
||||
held = repo.find_slot_by_portfolio(INST, "pf-b")
|
||||
svc.transit_slot(held.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="r", actor="t", symbol="000660", qty=7.0)
|
||||
svc.transit_slot(held.slot_id, SlotState.PENDING_BUY, SlotState.HOLDING,
|
||||
reason="r", actor="t", avg_price=100.0)
|
||||
return repo, svc
|
||||
|
||||
|
||||
def test_full_rebuild_with_broker_carry_over(pg_clean):
|
||||
repo, _ = _legacy_rows(pg_clean)
|
||||
report = run_cutover(
|
||||
pg_clean, INST, UNIVERSE, [("000660", 12)], # broker ground truth qty=12
|
||||
)
|
||||
assert report.deleted == 2 # pf-old + pf-b wiped
|
||||
assert report.created == 3 # one slot per universe asset
|
||||
assert report.holdings == 1
|
||||
slots = {s.portfolio_id: s for s in repo.list_slots(INST)}
|
||||
assert set(slots) == {"pf-a", "pf-b", "pf-c"}
|
||||
assert slots["pf-a"].status is SlotState.INACTIVE
|
||||
held = slots["pf-b"]
|
||||
assert held.status is SlotState.HOLDING
|
||||
assert held.qty == 12.0 # broker qty, NOT the old mirror (7)
|
||||
assert held.avg_price is None # balance API has no cost basis (WARN)
|
||||
assert held.symbol == "000660"
|
||||
# old pf-old row is gone
|
||||
assert repo.find_slot_by_portfolio(INST, "pf-old") is None
|
||||
|
||||
|
||||
def test_aborts_on_in_flight_pending(pg_clean):
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, {"pf-a"})
|
||||
s = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
svc.transit_slot(s.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="r", actor="t", symbol="005930")
|
||||
with pytest.raises(CutoverAbort, match="in-flight"):
|
||||
run_cutover(pg_clean, INST, UNIVERSE, [])
|
||||
# nothing was changed
|
||||
assert repo.fetch_slot(s.slot_id).status is SlotState.PENDING_BUY
|
||||
|
||||
|
||||
def test_aborts_on_unmapped_broker_holding(pg_clean):
|
||||
with pytest.raises(CutoverAbort, match="outside the universe"):
|
||||
run_cutover(pg_clean, INST, UNIVERSE, [("999999", 3)])
|
||||
# DB untouched (abort happened before any write)
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
assert repo.list_slots(INST) == []
|
||||
|
||||
|
||||
def test_allow_unmapped_skips_with_warning(pg_clean):
|
||||
report = run_cutover(
|
||||
pg_clean, INST, UNIVERSE, [("999999", 3), ("005930", 5)],
|
||||
allow_unmapped=True,
|
||||
)
|
||||
assert report.unmapped == ["999999"]
|
||||
assert report.holdings == 1 # only the mapped one carried over
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
held = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
assert held.status is SlotState.HOLDING and held.qty == 5.0
|
||||
|
||||
|
||||
def test_dry_run_rolls_back(pg_clean):
|
||||
repo, _ = _legacy_rows(pg_clean)
|
||||
before = {s.slot_id for s in repo.list_slots(INST)}
|
||||
report = run_cutover(pg_clean, INST, UNIVERSE, [("000660", 12)], dry_run=True)
|
||||
assert report.dry_run and report.created == 3
|
||||
after = {s.slot_id for s in repo.list_slots(INST)}
|
||||
assert after == before # rolled back — nothing changed
|
||||
|
||||
|
||||
def test_instance_isolation_other_instance_untouched(pg_clean):
|
||||
other = PostgreSQLSlotRepository(pg_clean)
|
||||
other.reconcile_universe("inst-other", {"pf-z"})
|
||||
run_cutover(pg_clean, INST, UNIVERSE, [])
|
||||
assert other.find_slot_by_portfolio("inst-other", "pf-z") is not None
|
||||
|
||||
|
||||
def test_promote_unique_sets_not_null(pg_clean):
|
||||
import psycopg
|
||||
run_cutover(pg_clean, INST, UNIVERSE, [], promote_unique=True)
|
||||
with psycopg.connect(pg_clean) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT is_nullable FROM information_schema.columns "
|
||||
"WHERE table_name='daily_slot' AND column_name='portfolio_id'"
|
||||
)
|
||||
assert cur.fetchone()[0] == "NO"
|
||||
# restore for other tests (pg_clean truncates but keeps schema)
|
||||
cur.execute("ALTER TABLE daily_slot ALTER COLUMN portfolio_id DROP NOT NULL")
|
||||
conn.commit()
|
||||
|
||||
|
||||
def test_audit_rows_written(pg_clean):
|
||||
import psycopg
|
||||
run_cutover(pg_clean, INST, UNIVERSE, [("005930", 5)])
|
||||
with psycopg.connect(pg_clean) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM slot_transition_log WHERE reason='cutover_rebuild'"
|
||||
)
|
||||
assert int(cur.fetchone()[0]) == 3
|
||||
@@ -0,0 +1,89 @@
|
||||
"""DB reset tool tests (dcy-020 / REQ-064, BAT-030 M4).
|
||||
|
||||
PostgreSQL-gated (TREX_POSTGRESQL_TEST_DSN) — the wipe-and-reinitialize tool
|
||||
that replaced the data-preserving cutover: wipes the slot-core tables and
|
||||
reapplies migrations so the DB lands on the clean asset-centric schema.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from trex_core.adapters.persistence.postgresql import PostgreSQLSlotRepository
|
||||
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS, SlotService, SlotState
|
||||
from trex_core.runtime.reset import SLOT_CORE_TABLES, run_reset
|
||||
|
||||
INST = "default"
|
||||
|
||||
|
||||
def _seed(dsn):
|
||||
"""Create asset slots + drive one into holding so there are rows to wipe."""
|
||||
repo = PostgreSQLSlotRepository(dsn)
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, {"pf-a", "pf-b"})
|
||||
s = repo.find_slot_by_portfolio(INST, "pf-a")
|
||||
svc.transit_slot(s.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="r", actor="t", symbol="005930", qty=10)
|
||||
return repo
|
||||
|
||||
|
||||
def test_reset_wipes_and_lands_asset_centric(pg_clean):
|
||||
repo = _seed(pg_clean)
|
||||
assert len(repo.list_slots(INST)) == 2
|
||||
|
||||
report = run_reset(pg_clean, dry_run=False)
|
||||
|
||||
assert report.dry_run is False
|
||||
assert report.schema_ok is True
|
||||
assert report.wiped["daily_slot"] == 2
|
||||
assert repo.list_slots(INST) == [] # wiped
|
||||
# schema is the clean asset-centric form
|
||||
import psycopg
|
||||
with psycopg.connect(pg_clean) as c, c.cursor() as cur:
|
||||
cur.execute("SELECT column_name FROM information_schema.columns "
|
||||
"WHERE table_name='daily_slot'")
|
||||
cols = {r[0] for r in cur.fetchall()}
|
||||
assert cols == set(EXPECTED_SLOT_FIELDS)
|
||||
cur.execute("SELECT pg_get_constraintdef(oid) FROM pg_constraint "
|
||||
"WHERE conname='daily_slot_status_check'")
|
||||
assert "inactive" in cur.fetchone()[0]
|
||||
cur.execute("SELECT indexname FROM pg_indexes WHERE tablename='daily_slot'")
|
||||
assert "uq_daily_slot_instance_portfolio" in {r[0] for r in cur.fetchall()}
|
||||
|
||||
|
||||
def test_dry_run_reports_without_wiping(pg_clean):
|
||||
repo = _seed(pg_clean)
|
||||
report = run_reset(pg_clean, dry_run=True)
|
||||
assert report.dry_run is True
|
||||
assert report.wiped["daily_slot"] == 2 # reported
|
||||
assert len(repo.list_slots(INST)) == 2 # but NOT wiped
|
||||
|
||||
|
||||
def test_reset_is_idempotent(pg_clean):
|
||||
_seed(pg_clean)
|
||||
run_reset(pg_clean, dry_run=False)
|
||||
report2 = run_reset(pg_clean, dry_run=False) # second pass on clean DB
|
||||
assert report2.schema_ok is True
|
||||
assert report2.wiped["daily_slot"] == 0
|
||||
|
||||
|
||||
def test_reset_usable_for_admission_after_wipe(pg_clean):
|
||||
"""After reset the DB is immediately usable: reconcile + activate work."""
|
||||
_seed(pg_clean)
|
||||
run_reset(pg_clean, dry_run=False)
|
||||
repo = PostgreSQLSlotRepository(pg_clean)
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
repo.reconcile_universe(INST, {"pf-x"})
|
||||
s = repo.find_slot_by_portfolio(INST, "pf-x")
|
||||
assert s.status is SlotState.INACTIVE
|
||||
svc.transit_slot(s.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
||||
reason="r", actor="t", symbol="005930")
|
||||
assert repo.count_active(INST) == 1
|
||||
|
||||
|
||||
def test_covers_all_slot_core_tables(pg_clean):
|
||||
"""Sanity: the tool targets every slot-core table the migrations create."""
|
||||
import psycopg
|
||||
with psycopg.connect(pg_clean) as c, c.cursor() as cur:
|
||||
cur.execute("SELECT table_name FROM information_schema.tables "
|
||||
"WHERE table_schema='public' AND table_name = ANY(%s)",
|
||||
(list(SLOT_CORE_TABLES),))
|
||||
present = {r[0] for r in cur.fetchall()}
|
||||
assert present == set(SLOT_CORE_TABLES)
|
||||
Reference in New Issue
Block a user