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
113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
"""Portfolios asset migration: data migration + asset_id FK + symbol/market DROP (REQ-011, REQ-014)
|
|
|
|
Revision ID: 20260519_0018
|
|
Revises: 20260519_0017
|
|
Create Date: 2026-05-19 00:00:00
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy import inspect, text
|
|
|
|
revision = "20260519_0018"
|
|
down_revision = "20260519_0017"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _column_exists(inspector: sa.engine.Inspector, table_name: str, column_name: str) -> bool:
|
|
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
|
|
|
|
|
|
def _constraint_exists(inspector: sa.engine.Inspector, table_name: str, constraint_name: str) -> bool:
|
|
fk_constraints = inspector.get_foreign_keys(table_name)
|
|
return any(fk.get("name") == constraint_name for fk in fk_constraints)
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = inspect(bind)
|
|
|
|
# Step 1: Populate assets catalog from existing portfolios (REQ-014)
|
|
# Only if portfolios.symbol column still exists (idempotent guard)
|
|
if _column_exists(inspector, "portfolios", "symbol"):
|
|
bind.execute(text("""
|
|
INSERT INTO assets (symbol, markets)
|
|
SELECT
|
|
symbol,
|
|
COALESCE(
|
|
ARRAY_AGG(DISTINCT market) FILTER (WHERE market IS NOT NULL),
|
|
ARRAY[]::text[]
|
|
)
|
|
FROM portfolios
|
|
GROUP BY symbol
|
|
ON CONFLICT (symbol) DO NOTHING
|
|
"""))
|
|
|
|
# Step 2: Add portfolios.asset_id column (nullable, temporary)
|
|
if not _column_exists(inspector, "portfolios", "asset_id"):
|
|
op.add_column("portfolios", sa.Column("asset_id", sa.UUID(), nullable=True))
|
|
|
|
# Step 3: Back-fill asset_id from assets table
|
|
if _column_exists(inspector, "portfolios", "symbol") and _column_exists(inspector, "portfolios", "asset_id"):
|
|
bind.execute(text("""
|
|
UPDATE portfolios p
|
|
SET asset_id = a.asset_id
|
|
FROM assets a
|
|
WHERE a.symbol = p.symbol
|
|
AND p.asset_id IS NULL
|
|
"""))
|
|
|
|
# Step 4: Set NOT NULL constraint on asset_id
|
|
if _column_exists(inspector, "portfolios", "asset_id"):
|
|
op.alter_column("portfolios", "asset_id", nullable=False)
|
|
|
|
# Step 5: Add FK constraint (if not already present)
|
|
if not _constraint_exists(inspector, "portfolios", "fk_portfolios_asset_id"):
|
|
op.create_foreign_key(
|
|
"fk_portfolios_asset_id",
|
|
"portfolios",
|
|
"assets",
|
|
["asset_id"],
|
|
["asset_id"],
|
|
ondelete="RESTRICT",
|
|
)
|
|
|
|
# Step 6: Drop legacy columns
|
|
if _column_exists(inspector, "portfolios", "symbol"):
|
|
op.drop_column("portfolios", "symbol")
|
|
|
|
if _column_exists(inspector, "portfolios", "market"):
|
|
op.drop_column("portfolios", "market")
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = inspect(bind)
|
|
|
|
# Restore symbol and market columns
|
|
if not _column_exists(inspector, "portfolios", "symbol"):
|
|
op.add_column("portfolios", sa.Column("symbol", sa.Text(), nullable=True))
|
|
|
|
if not _column_exists(inspector, "portfolios", "market"):
|
|
op.add_column("portfolios", sa.Column("market", sa.Text(), nullable=True))
|
|
|
|
# Back-fill symbol from assets table
|
|
if _column_exists(inspector, "portfolios", "asset_id") and _column_exists(inspector, "portfolios", "symbol"):
|
|
bind.execute(text("""
|
|
UPDATE portfolios p
|
|
SET symbol = a.symbol
|
|
FROM assets a
|
|
WHERE a.asset_id = p.asset_id
|
|
"""))
|
|
|
|
# Remove FK constraint
|
|
if _constraint_exists(inspector, "portfolios", "fk_portfolios_asset_id"):
|
|
op.drop_constraint("fk_portfolios_asset_id", "portfolios", type_="foreignkey")
|
|
|
|
# Remove asset_id column
|
|
if _column_exists(inspector, "portfolios", "asset_id"):
|
|
op.drop_column("portfolios", "asset_id")
|