- confirm-req-implemented: REQ-001~018 (17건, REQ-013 제외) Approved → Implemented - BAT-001(REQ-009/011/014), BAT-002(REQ-010/012/015), BAT-003(REQ-001/002/003/006) - BAT-004(REQ-004/005), BAT-005(REQ-007/008), BAT-006(REQ-016/017/018) - CHG-20260521-01 entry 각 REQ에 추가; SRS index Recent Change Log 갱신 - REQ-010 CHG-20260521-02: GET /v1/assets admin catalog 전용 재정의 - 기존 token_type 분기 portfolio 조회 코드 제거, admin permission + list_assets() 호출로 대체 - AssetCatalogListResponse 스키마 추가 (items/total) - REQ-010 Status: Implemented → Approved (재검증 필요) - 테스트 343건 전체 통과: service/user token 403, admin token 200 + catalog schema 검증 - migration 보강: portfolios_asset_migration market 컬럼 부재 fallback, portfolio_tags 빈 테이블 early-return, service_token index fix autocommit_block 정리 - docs/releases/rel-001: REL-001 draft 추가 (BAT-001~011 포함) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
124 lines
4.5 KiB
Python
124 lines
4.5 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).
|
|
# portfolios.market 컬럼은 이전 BAT-008 migration(20260502_0014) 또는
|
|
# ensure_schema() 경로에 따라 존재하지 않을 수 있다. REQ-014 본문 "market 값이
|
|
# NULL인 기존 행은 빈 배열로 처리"에 따라 컬럼 부재 시 markets=[]로 fallback.
|
|
if _column_exists(inspector, "portfolios", "symbol"):
|
|
if _column_exists(inspector, "portfolios", "market"):
|
|
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
|
|
"""))
|
|
else:
|
|
bind.execute(text("""
|
|
INSERT INTO assets (symbol, markets)
|
|
SELECT DISTINCT symbol, ARRAY[]::text[]
|
|
FROM portfolios
|
|
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")
|