feat: add portfolio ownership model, auth guards, and migration
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""add portfolio owner user id
|
||||
|
||||
Revision ID: 20260413_0010
|
||||
Revises: 20260410_0009
|
||||
Create Date: 2026-04-13 01:00:00
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260413_0010"
|
||||
down_revision = "20260410_0009"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _column_exists(inspector, table_name: str, column_name: str) -> bool:
|
||||
return any(column["name"] == column_name for column in inspector.get_columns(table_name))
|
||||
|
||||
|
||||
def _index_exists(inspector, table_name: str, index_name: str) -> bool:
|
||||
return any(index["name"] == index_name for index in inspector.get_indexes(table_name))
|
||||
|
||||
|
||||
def _fk_exists(inspector, table_name: str, fk_name: str) -> bool:
|
||||
return any(fk.get("name") == fk_name for fk in inspector.get_foreign_keys(table_name))
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = inspect(bind)
|
||||
|
||||
if not _column_exists(inspector, "portfolios", "owner_user_id"):
|
||||
op.add_column("portfolios", sa.Column("owner_user_id", sa.Text(), nullable=True))
|
||||
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
WITH fallback_owner AS (
|
||||
SELECT id
|
||||
FROM users
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 1
|
||||
)
|
||||
UPDATE portfolios p
|
||||
SET owner_user_id = f.id
|
||||
FROM fallback_owner f
|
||||
WHERE p.owner_user_id IS NULL
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
unresolved = bind.execute(sa.text("SELECT COUNT(*) FROM portfolios WHERE owner_user_id IS NULL")).scalar()
|
||||
if unresolved and int(unresolved) > 0:
|
||||
raise RuntimeError("owner_user_id backfill failed: users 테이블에 fallback 소유주가 필요합니다")
|
||||
|
||||
op.alter_column("portfolios", "owner_user_id", existing_type=sa.Text(), nullable=False)
|
||||
|
||||
inspector = inspect(bind)
|
||||
if not _index_exists(inspector, "portfolios", "idx_portfolios_owner_updated"):
|
||||
op.create_index(
|
||||
"idx_portfolios_owner_updated",
|
||||
"portfolios",
|
||||
["owner_user_id", sa.text("updated_at DESC")],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
inspector = inspect(bind)
|
||||
if not _fk_exists(inspector, "portfolios", "fk_portfolios_owner_user"):
|
||||
op.create_foreign_key(
|
||||
"fk_portfolios_owner_user",
|
||||
"portfolios",
|
||||
"users",
|
||||
["owner_user_id"],
|
||||
["id"],
|
||||
ondelete="RESTRICT",
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = inspect(bind)
|
||||
|
||||
if _fk_exists(inspector, "portfolios", "fk_portfolios_owner_user"):
|
||||
op.drop_constraint("fk_portfolios_owner_user", "portfolios", type_="foreignkey")
|
||||
|
||||
inspector = inspect(bind)
|
||||
if _index_exists(inspector, "portfolios", "idx_portfolios_owner_updated"):
|
||||
op.drop_index("idx_portfolios_owner_updated", table_name="portfolios")
|
||||
|
||||
inspector = inspect(bind)
|
||||
if _column_exists(inspector, "portfolios", "owner_user_id"):
|
||||
op.drop_column("portfolios", "owner_user_id")
|
||||
Reference in New Issue
Block a user