Asset Catalog Model Implementation (BAT-001): - Introduce assets table with UUID PK, symbol UNIQUE, markets TEXT[] array - Define MarketCode enum (KRX, NXT, SOR, NASDAQ, NYSE, UPBIT, BITHUMB) - Add Asset domain model with catalog properties - Implement asset CRUD protocols in portfolio_repository - Add PostgreSQL asset CRUD operations with GIN index on markets Portfolio Model Refactoring (REQ-011): - Add portfolios.asset_id UUID FK to assets(asset_id) ON DELETE RESTRICT - Remove portfolios.symbol and portfolios.market columns - Update Portfolio domain model to reference asset_id instead of symbol/market - Refactor all portfolio queries to JOIN assets catalog - Update all repository methods to use asset references Data Migration (REQ-014): - Create 20260519_0017 Alembic revision for assets table creation - Create 20260519_0018 Alembic revision for atomic data migration: * Extract unique symbol combinations from portfolios * Aggregate markets using ARRAY_AGG(DISTINCT market) * Populate assets catalog with extracted data * Update portfolios.asset_id to reference new assets * Remove legacy symbol/market columns in single transaction - Implement migration with rollback support Requirements Approved (REQ-001~008): - REQ-001~008 transitioned from Proposed to Approved - Complete tag model requirement set with implementation readiness New Requirements Approved (REQ-009~015): - REQ-009: assets table schema (approved, included in BAT-001) - REQ-010: admin asset catalog CRUD (approved, future BAT-002) - REQ-011: portfolios asset_id FK (approved, included in BAT-001) - REQ-012: portfolio create catalog validation (approved, future BAT-002) - REQ-013: GET /v1/assets token branching (approved, future BAT-002) - REQ-014: portfolios data migration (approved, included in BAT-001) - REQ-015: asset catalog baseline docs (approved) Code Changes: - app/domain/models.py: Add Asset model, MarketCode enum, Portfolio asset_id FK - app/repositories/portfolio_repository.py: Define asset CRUD protocol - app/repositories/postgres.py: Implement asset operations with full CRUD - app/api/v1_assets.py: Remove symbol/market direct references - app/cli/commands/portfolio.py: Update portfolio commands for asset references - app/schemas/portfolio.py: Update portfolio schema with asset_id - All portfolio operations throughout codebase: Use asset references Testing: - Add test_asset_catalog.py for asset CRUD operations - Verify REQ-009 AC-1~5 (asset catalog creation, lookup, uniqueness) - Verify REQ-011 AC-1~5 (FK constraint, portfolio asset reference) - Verify REQ-014 AC-1~6 (migration atomicity, data correctness, rollback) - Update existing tests for asset model usage Documentation: - Create BAT-001 batch documents (planning, design, implementation, verification) - Update dcy-001 with implementation details and confirmed architecture - Add 8 new requirement documents (REQ-009~015) - Update docs/discovery/index.md with batch entry Impact: - Database: assets catalog table, portfolios refactored to reference assets - Migration: 2 new Alembic revisions (0017 assets, 0018 portfolio migration) - API: Symbol/market now accessed through asset references - CLI: Portfolio commands require asset reference pattern - All existing portfolio data preserved and correctly migrated
4.3 KiB
4.3 KiB
BAT-001 Implementation
- Status: verified
- Batch: BAT-001
Plan Summary
- 이 batch 구현에서 실제 변경할 범위:
app/domain/models.py— MarketCode Enum, Asset 모델 추가; Portfolio 모델 symbol/market 제거, asset_id 추가app/repositories/portfolio_repository.py— asset CRUD Protocol 선언 추가app/repositories/postgres.py— asset CRUD SQL 구현; portfolios JOIN assets 쿼리 반영- Alembic rev-A — assets 테이블 생성 + GIN 인덱스
- Alembic rev-B — portfolios 데이터 이전 + asset_id FK 추가 + symbol/market 컬럼 제거 (단일 트랜잭션)
app/api/portfolios.py,app/api/v1_assets.py,app/cli/commands/portfolio.py,app/schemas/— symbol/market 참조 제거tests/test_asset_catalog.py신규 — REQ-009 AC-15, REQ-011 AC-15, REQ-014 AC-1~6
Changed Files
app/domain/models.pyapp/repositories/portfolio_repository.pyapp/repositories/postgres.pymigrations/versions/<rev-A>_create_assets_table.py(신규)migrations/versions/<rev-B>_portfolios_asset_migration.py(신규)app/api/portfolios.pyapp/api/v1_assets.pyapp/cli/commands/portfolio.pyapp/schemas/(영향 파일 구현 단계에서 확정)tests/test_asset_catalog.py(신규)
Changed Files (확정)
app/domain/models.py— MarketCode Enum, Asset dataclass 추가; Portfolio.market 제거 → asset_id 추가app/repositories/portfolio_repository.py— create_portfolio symbol→asset_id; asset CRUD Protocol 선언 5종app/repositories/postgres.py— _SELECT_PORTFOLIO JOIN assets; save/reset/adjust_buffer/transfer_buffer RETURNING 제거; create_portfolio/update_portfolio 수정; asset CRUD 5종 구현; get_or_create asset 조회 로직 추가migrations/versions/20260519_0017_create_assets_table.py(신규) — assets 테이블 + GIN 인덱스 (rev-A)migrations/versions/20260519_0018_portfolios_asset_migration.py(신규) — 데이터 이전 + asset_id FK + symbol/market DROP (rev-B)app/schemas/v1.py— AssetItem.market 제거app/api/v1_assets.py— market=p.market 제거app/schemas/portfolio.py— PortfolioBase.symbol 제거; CreatePortfolioRequest.asset_id 추가; UpdatePortfolioRequest.symbol 제거app/services/portfolio_service.py— create_portfolio symbol→asset_idapp/cli/commands/portfolio.py— create --symbol→--asset-id; update --symbol 제거tests/test_asset_catalog.py(신규) — REQ-009 AC-4,5 + REQ-011 AC-4,5 + REQ-014 migration 파일 구조 검증tests/test_cli.py— FakeRepo create_portfolio symbol→asset_id; _portfolio() name 공백 제거; 테스트 픽스처 업데이트tests/test_portfolio_uuid.py— FakeRepo create_portfolio symbol→asset_id; --symbol→--asset-id 전환tests/test_v1.py— market 필드 어서션 제거tests/test_portfolio_ownership.py— POST body symbol→asset_idtests/test_daemon_assignment.py— Portfolio() market= 제거
Execution Log
- app/domain/models.py: MarketCode Enum, Asset 모델 추가; Portfolio market→asset_id 필드 교체
- app/repositories/portfolio_repository.py: create_portfolio 시그니처 변경; asset CRUD Protocol 5종 선언
- app/repositories/postgres.py: JOIN 쿼리, RETURNING 제거 패턴, asset CRUD 5종 구현, get_or_create symbol→asset 조회
- Alembic rev-A(0017): assets 테이블 + GIN 인덱스 생성
- Alembic rev-B(0018): 데이터 이전 → asset_id FK → symbol/market DROP (단일 트랜잭션 내 idempotent 가드)
- 스키마/API/CLI 레이어: symbol/market 참조 전수 제거
- 기존 테스트 suite 17개 실패 수정: 공백 포함 이름, --symbol→--asset-id, market 어서션 제거, request body 업데이트
- test_asset_catalog.py 신규 작성: 20개 테스트
Validation
- pytest 162 passed (tests/test_asset_catalog.py 20, tests/test_cli.py 35, tests/test_portfolio_uuid.py 14, tests/test_v1.py 21, tests/test_portfolio_ownership.py 4, 나머지 기존 suite)
- 실행 명령:
.venv/bin/python -m pytest --tb=short
Remaining Risks
- DB-integration ACs(REQ-009 AC-1,2,3; REQ-011 AC-1,2,3; REQ-014 AC-1~6)는 실제 PostgreSQL 연결 없이 검증 불가 — verification.md에 통합 테스트 항목으로 기록
- R-4(운영 환경 대용량 lock): 로컬 테스트 범위 밖; 운영 배포 시 maintenance window 고려 필요