"""Create assets catalog table (REQ-009) Revision ID: 20260519_0017 Revises: 20260513_0016 Create Date: 2026-05-19 00:00:00 """ from __future__ import annotations import sqlalchemy as sa from alembic import op from sqlalchemy import inspect revision = "20260519_0017" down_revision = "20260513_0016" branch_labels = None depends_on = None def _table_exists(inspector: sa.engine.Inspector, table_name: str) -> bool: return table_name in inspector.get_table_names() def _index_exists(inspector: sa.engine.Inspector, table_name: str, index_name: str) -> bool: return any(idx["name"] == index_name for idx in inspector.get_indexes(table_name)) def upgrade() -> None: bind = op.get_bind() inspector = inspect(bind) if not _table_exists(inspector, "assets"): op.create_table( "assets", sa.Column("asset_id", sa.UUID(), nullable=False, server_default=sa.text("gen_random_uuid()")), sa.Column("symbol", sa.Text(), nullable=False), sa.Column("markets", sa.ARRAY(sa.Text()), nullable=False, server_default=sa.text("ARRAY[]::text[]")), sa.Column("display_name", sa.Text(), nullable=True), sa.Column("status", sa.Text(), nullable=False, server_default=sa.text("'active'")), sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False, server_default=sa.text("NOW()")), sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=False, server_default=sa.text("NOW()")), sa.PrimaryKeyConstraint("asset_id"), sa.UniqueConstraint("symbol", name="uq_assets_symbol"), ) if _table_exists(inspector, "assets") and not _index_exists(inspector, "assets", "idx_assets_markets_gin"): op.create_index( "idx_assets_markets_gin", "assets", ["markets"], postgresql_using="gin", ) def downgrade() -> None: bind = op.get_bind() inspector = inspect(bind) if _table_exists(inspector, "assets") and _index_exists(inspector, "assets", "idx_assets_markets_gin"): op.drop_index("idx_assets_markets_gin", table_name="assets") if _table_exists(inspector, "assets"): op.drop_table("assets")