feat: database models and alembic migrations — all tables per design
- shared/db.py: async engine + session factory - shared/models/base.py: DeclarativeBase + TimestampMixin - shared/models/trading.py: Strategy, Signal, Trade, Position, StrategyWeightHistory - shared/models/news.py: Article, ArticleSentiment - shared/models/learning.py: TradeOutcome, LearningAdjustment - shared/models/auth.py: User, UserCredential - shared/models/timeseries.py: MarketData, PortfolioSnapshot, StrategyMetric - Alembic async env.py with initial migration including TimescaleDB hypertables - 21 model tests covering enums, instantiation, metadata registration
This commit is contained in:
parent
ae5b3f89d1
commit
72cb1b6fe5
23 changed files with 1283 additions and 0 deletions
1
alembic/README
Normal file
1
alembic/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Generic single-database configuration.
|
||||
BIN
alembic/__pycache__/env.cpython-314.pyc
Normal file
BIN
alembic/__pycache__/env.cpython-314.pyc
Normal file
Binary file not shown.
71
alembic/env.py
Normal file
71
alembic/env.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
"""Alembic environment — async-aware, imports all shared models."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
# Import all models so metadata is populated for autogenerate.
|
||||
from shared.models import Base # noqa: F401
|
||||
import shared.models # noqa: F401 — triggers side-effect imports of every model
|
||||
|
||||
config = context.config
|
||||
|
||||
# Override sqlalchemy.url from environment if available.
|
||||
db_url = os.environ.get("TRADING_DATABASE_URL")
|
||||
if db_url:
|
||||
config.set_main_option("sqlalchemy.url", db_url)
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode (emit SQL without a live connection)."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection) -> None: # noqa: ANN001
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""Run migrations in 'online' mode using an async engine."""
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Entry-point for online migrations — delegates to the async helper."""
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
28
alembic/script.py.mako
Normal file
28
alembic/script.py.mako
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
Binary file not shown.
284
alembic/versions/a1b2c3d4e5f6_initial_schema.py
Normal file
284
alembic/versions/a1b2c3d4e5f6_initial_schema.py
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
"""initial schema
|
||||
|
||||
Revision ID: a1b2c3d4e5f6
|
||||
Revises:
|
||||
Create Date: 2026-02-22 15:15:15.661206
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "a1b2c3d4e5f6"
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Create all tables for the trading bot."""
|
||||
|
||||
# --- Core trading tables ---
|
||||
|
||||
op.create_table(
|
||||
"strategies",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("name", sa.String(255), unique=True, nullable=False),
|
||||
sa.Column("description", sa.Text, nullable=True),
|
||||
sa.Column("current_weight", sa.Float, nullable=False, server_default="0.333"),
|
||||
sa.Column("active", sa.Boolean, nullable=False, server_default=sa.text("true")),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"signals",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("ticker", sa.String(20), nullable=False, index=True),
|
||||
sa.Column(
|
||||
"direction",
|
||||
sa.Enum("LONG", "SHORT", "NEUTRAL", name="signaldirection"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("strength", sa.Float, nullable=False),
|
||||
sa.Column("strategy_sources", postgresql.JSON, nullable=True),
|
||||
sa.Column("sentiment_score", sa.Float, nullable=True),
|
||||
sa.Column("acted_on", sa.Boolean, nullable=False, server_default=sa.text("false")),
|
||||
sa.Column(
|
||||
"strategy_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("strategies.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"trades",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("ticker", sa.String(20), nullable=False, index=True),
|
||||
sa.Column(
|
||||
"side",
|
||||
sa.Enum("BUY", "SELL", name="tradeside"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("qty", sa.Float, nullable=False),
|
||||
sa.Column("price", sa.Float, nullable=False),
|
||||
sa.Column("timestamp", sa.String, nullable=True),
|
||||
sa.Column(
|
||||
"strategy_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("strategies.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"signal_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("signals.id"),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"status",
|
||||
sa.Enum("PENDING", "FILLED", "CANCELLED", "REJECTED", name="tradestatus"),
|
||||
nullable=False,
|
||||
server_default="PENDING",
|
||||
),
|
||||
sa.Column("pnl", sa.Float, nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"positions",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("ticker", sa.String(20), unique=True, nullable=False),
|
||||
sa.Column("qty", sa.Float, nullable=False),
|
||||
sa.Column("avg_entry", sa.Float, nullable=False),
|
||||
sa.Column("unrealized_pnl", sa.Float, nullable=True),
|
||||
sa.Column("stop_loss", sa.Float, nullable=True),
|
||||
sa.Column("take_profit", sa.Float, nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"strategy_weight_history",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column(
|
||||
"strategy_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("strategies.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("old_weight", sa.Float, nullable=False),
|
||||
sa.Column("new_weight", sa.Float, nullable=False),
|
||||
sa.Column("reason", sa.String(500), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
# --- News & sentiment ---
|
||||
|
||||
op.create_table(
|
||||
"articles",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("source", sa.String(100), nullable=False),
|
||||
sa.Column("url", sa.Text, nullable=False),
|
||||
sa.Column("title", sa.Text, nullable=False),
|
||||
sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("fetched_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("content_hash", sa.String(64), unique=True, nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"article_sentiments",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column(
|
||||
"article_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("articles.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("ticker", sa.String(20), nullable=False, index=True),
|
||||
sa.Column("score", sa.Float, nullable=False),
|
||||
sa.Column("confidence", sa.Float, nullable=False),
|
||||
sa.Column("model_used", sa.String(50), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
# --- Learning ---
|
||||
|
||||
op.create_table(
|
||||
"trade_outcomes",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column(
|
||||
"trade_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("trades.id"),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("hold_duration", sa.Interval, nullable=True),
|
||||
sa.Column("realized_pnl", sa.Float, nullable=False),
|
||||
sa.Column("roi_pct", sa.Float, nullable=False),
|
||||
sa.Column("was_profitable", sa.Boolean, nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"learning_adjustments",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column(
|
||||
"strategy_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("strategies.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("old_weight", sa.Float, nullable=False),
|
||||
sa.Column("new_weight", sa.Float, nullable=False),
|
||||
sa.Column("reason", sa.Text, nullable=True),
|
||||
sa.Column("reward_signal", sa.Float, nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
# --- Auth ---
|
||||
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("username", sa.String(100), unique=True, nullable=False),
|
||||
sa.Column("display_name", sa.String(255), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"user_credentials",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column(
|
||||
"user_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("credential_id", sa.String(512), unique=True, nullable=False),
|
||||
sa.Column("public_key", sa.LargeBinary, nullable=False),
|
||||
sa.Column("sign_count", sa.Integer, nullable=False, server_default="0"),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
# --- Timeseries (TimescaleDB hypertables) ---
|
||||
|
||||
op.create_table(
|
||||
"market_data",
|
||||
sa.Column("timestamp", sa.DateTime(timezone=True), primary_key=True),
|
||||
sa.Column("ticker", sa.String(20), primary_key=True),
|
||||
sa.Column("open", sa.Float, nullable=False),
|
||||
sa.Column("high", sa.Float, nullable=False),
|
||||
sa.Column("low", sa.Float, nullable=False),
|
||||
sa.Column("close", sa.Float, nullable=False),
|
||||
sa.Column("volume", sa.Float, nullable=False),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"portfolio_snapshots",
|
||||
sa.Column("timestamp", sa.DateTime(timezone=True), primary_key=True),
|
||||
sa.Column("total_value", sa.Float, nullable=False),
|
||||
sa.Column("cash", sa.Float, nullable=False),
|
||||
sa.Column("positions_value", sa.Float, nullable=False),
|
||||
sa.Column("daily_pnl", sa.Float, nullable=False),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"strategy_metrics",
|
||||
sa.Column("timestamp", sa.DateTime(timezone=True), primary_key=True),
|
||||
sa.Column(
|
||||
"strategy_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("strategies.id"),
|
||||
primary_key=True,
|
||||
),
|
||||
sa.Column("win_rate", sa.Float, nullable=False),
|
||||
sa.Column("total_pnl", sa.Float, nullable=False),
|
||||
sa.Column("trade_count", sa.Integer, nullable=False),
|
||||
sa.Column("sharpe_ratio", sa.Float, nullable=True),
|
||||
)
|
||||
|
||||
# Convert timeseries tables to TimescaleDB hypertables.
|
||||
# These calls are idempotent-safe when the extension is loaded.
|
||||
op.execute("SELECT create_hypertable('market_data', 'timestamp', if_not_exists => TRUE)")
|
||||
op.execute("SELECT create_hypertable('portfolio_snapshots', 'timestamp', if_not_exists => TRUE)")
|
||||
op.execute("SELECT create_hypertable('strategy_metrics', 'timestamp', if_not_exists => TRUE)")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Drop all tables in reverse dependency order."""
|
||||
op.drop_table("strategy_metrics")
|
||||
op.drop_table("portfolio_snapshots")
|
||||
op.drop_table("market_data")
|
||||
op.drop_table("user_credentials")
|
||||
op.drop_table("users")
|
||||
op.drop_table("learning_adjustments")
|
||||
op.drop_table("trade_outcomes")
|
||||
op.drop_table("article_sentiments")
|
||||
op.drop_table("articles")
|
||||
op.drop_table("strategy_weight_history")
|
||||
op.drop_table("positions")
|
||||
op.drop_table("trades")
|
||||
op.drop_table("signals")
|
||||
op.drop_table("strategies")
|
||||
|
||||
# Drop enums
|
||||
sa.Enum(name="signaldirection").drop(op.get_bind(), checkfirst=True)
|
||||
sa.Enum(name="tradeside").drop(op.get_bind(), checkfirst=True)
|
||||
sa.Enum(name="tradestatus").drop(op.get_bind(), checkfirst=True)
|
||||
Loading…
Add table
Add a link
Reference in a new issue