52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""add portfolio name column
|
|
|
|
Revision ID: 20260403_0008
|
|
Revises: 20260403_0007
|
|
Create Date: 2026-04-03 14:45:00
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy import inspect
|
|
|
|
|
|
revision = "20260403_0008"
|
|
down_revision = "20260403_0007"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(inspector, table_name: str) -> bool:
|
|
return table_name in inspector.get_table_names()
|
|
|
|
|
|
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 upgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = inspect(bind)
|
|
|
|
if not _table_exists(inspector, "portfolios"):
|
|
return
|
|
|
|
if not _column_exists(inspector, "portfolios", "name"):
|
|
op.add_column("portfolios", sa.Column("name", sa.Text(), nullable=True))
|
|
|
|
op.execute("UPDATE portfolios SET name = symbol WHERE name IS NULL OR name = ''")
|
|
op.alter_column("portfolios", "name", existing_type=sa.Text(), nullable=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = inspect(bind)
|
|
|
|
if not _table_exists(inspector, "portfolios"):
|
|
return
|
|
|
|
if _column_exists(inspector, "portfolios", "name"):
|
|
op.drop_column("portfolios", "name")
|