"""Add market analysis data foundation.""" from alembic import op import sqlalchemy as sa revision = "202607120003" down_revision = "202607120002" branch_labels = None depends_on = None def upgrade() -> None: op.create_table( "market_instruments", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("symbol", sa.String(32), nullable=False), sa.Column("name", sa.String(128), nullable=False), sa.Column("exchange", sa.String(16)), sa.Column("instrument_type", sa.String(16), nullable=False), sa.Column("industry", sa.String(128)), sa.Column("list_date", sa.Date()), sa.Column("is_active", sa.Boolean(), nullable=False), sa.Column("source_updated_at", sa.DateTime()), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), ) for column, unique in ( ("symbol", True), ("name", False), ("exchange", False), ("instrument_type", False), ("industry", False), ("is_active", False), ): op.create_index( op.f(f"ix_market_instruments_{column}"), "market_instruments", [column], unique=unique ) op.create_table( "market_daily_quotes", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("symbol", sa.String(32), nullable=False), sa.Column("trade_date", sa.Date(), nullable=False), sa.Column("open_price", sa.Numeric(18, 4)), sa.Column("high_price", sa.Numeric(18, 4)), sa.Column("low_price", sa.Numeric(18, 4)), sa.Column("close_price", sa.Numeric(18, 4), nullable=False), sa.Column("pre_close", sa.Numeric(18, 4)), sa.Column("pct_change", sa.Numeric(12, 4)), sa.Column("volume", sa.Numeric(20, 2)), sa.Column("amount_cny", sa.Numeric(20, 2)), sa.Column("pe", sa.Numeric(18, 4)), sa.Column("pb", sa.Numeric(18, 4)), sa.Column("total_market_value", sa.Numeric(20, 2)), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), sa.UniqueConstraint("symbol", "trade_date", name="uq_market_quote_day"), ) op.create_index(op.f("ix_market_daily_quotes_symbol"), "market_daily_quotes", ["symbol"]) op.create_index( op.f("ix_market_daily_quotes_trade_date"), "market_daily_quotes", ["trade_date"] ) op.create_table( "market_financial_metrics", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("symbol", sa.String(32), nullable=False), sa.Column("period_end", sa.Date(), nullable=False), sa.Column("revenue_yoy", sa.Numeric(12, 4)), sa.Column("net_profit_yoy", sa.Numeric(12, 4)), sa.Column("roe", sa.Numeric(12, 4)), sa.Column("debt_to_assets", sa.Numeric(12, 4)), sa.Column("operating_cashflow_yoy", sa.Numeric(12, 4)), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), sa.UniqueConstraint("symbol", "period_end", name="uq_market_financial_period"), ) op.create_index( op.f("ix_market_financial_metrics_symbol"), "market_financial_metrics", ["symbol"] ) op.create_index( op.f("ix_market_financial_metrics_period_end"), "market_financial_metrics", ["period_end"] ) op.create_table( "market_macro_indicators", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("code", sa.String(64), nullable=False), sa.Column("name", sa.String(128), nullable=False), sa.Column("period_date", sa.Date(), nullable=False), sa.Column("value", sa.Numeric(20, 6), nullable=False), sa.Column("unit", sa.String(32)), sa.Column("source", sa.String(64), nullable=False), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), sa.UniqueConstraint("code", "period_date", name="uq_market_macro_period"), ) op.create_index(op.f("ix_market_macro_indicators_code"), "market_macro_indicators", ["code"]) op.create_index( op.f("ix_market_macro_indicators_period_date"), "market_macro_indicators", ["period_date"] ) op.create_table( "market_watchlists", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("actor", sa.String(128), nullable=False), sa.Column("symbol", sa.String(32), nullable=False), sa.Column("enabled", sa.Boolean(), nullable=False), sa.Column("created_at", sa.DateTime(), nullable=False), sa.Column("updated_at", sa.DateTime(), nullable=False), sa.UniqueConstraint("actor", "symbol", name="uq_market_watchlist_actor_symbol"), ) op.create_index(op.f("ix_market_watchlists_actor"), "market_watchlists", ["actor"]) op.create_index(op.f("ix_market_watchlists_symbol"), "market_watchlists", ["symbol"]) op.create_index(op.f("ix_market_watchlists_enabled"), "market_watchlists", ["enabled"]) def downgrade() -> None: for table in ( "market_watchlists", "market_macro_indicators", "market_financial_metrics", "market_daily_quotes", "market_instruments", ): op.drop_table(table)