first commit

This commit is contained in:
k
2026-04-20 23:05:59 +08:00
commit 9a72566753
33 changed files with 2138 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
import asyncio
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
# Alembic Config object, which provides access to the values within alembic.ini
config = context.config
# Interpret the config file for Python logging.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Override sqlalchemy.url from environment variable if set
db_url = os.environ.get("DATABASE_URL")
if db_url:
config.set_main_option("sqlalchemy.url", db_url)
# Import target metadata from the application models
from app.database import Base # noqa: E402
import app.models # noqa: E402,F401 — ensure all models are registered
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode (no DB connection needed)."""
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: Connection) -> None:
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:
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+114
View File
@@ -0,0 +1,114 @@
"""Initial schema: posts, candles, bot_trades, subscriptions
Revision ID: 001
Revises:
Create Date: 2026-04-20 00:00:00.000000
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ── posts ─────────────────────────────────────────────────────────────
op.create_table(
"posts",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("external_id", sa.String(64), nullable=False),
sa.Column("text", sa.Text(), nullable=False),
sa.Column("source", sa.String(32), nullable=False, server_default="truth"),
sa.Column("published_at", sa.DateTime(), nullable=False),
sa.Column("sentiment", sa.String(16), nullable=False, server_default="neutral"),
sa.Column("ai_confidence", sa.Integer(), nullable=False, server_default="0"),
sa.Column("relevant", sa.Boolean(), nullable=False, server_default="false"),
sa.Column("price_impact_asset", sa.String(8), nullable=True),
sa.Column("price_impact_m5", sa.Float(), nullable=True),
sa.Column("price_impact_m15", sa.Float(), nullable=True),
sa.Column("price_impact_m1h", sa.Float(), nullable=True),
sa.Column("price_at_post", sa.Float(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("NOW()"),
),
)
op.create_index("ix_posts_external_id", "posts", ["external_id"], unique=True)
op.create_index("ix_posts_id", "posts", ["id"])
# ── candles ───────────────────────────────────────────────────────────
op.create_table(
"candles",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("asset", sa.String(8), nullable=False),
sa.Column("timeframe", sa.String(4), nullable=False),
sa.Column("time", sa.BigInteger(), nullable=False),
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),
sa.UniqueConstraint("asset", "timeframe", "time", name="uq_candle_asset_tf_time"),
)
op.create_index("ix_candles_asset", "candles", ["asset"])
op.create_index("ix_candles_id", "candles", ["id"])
op.create_index("ix_candles_time", "candles", ["time"])
# ── bot_trades ────────────────────────────────────────────────────────
op.create_table(
"bot_trades",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("asset", sa.String(8), nullable=False),
sa.Column("side", sa.String(8), nullable=False),
sa.Column("entry_price", sa.Float(), nullable=False),
sa.Column("exit_price", sa.Float(), nullable=True),
sa.Column("pnl_usd", sa.Float(), nullable=True),
sa.Column("hold_seconds", sa.Integer(), nullable=True),
sa.Column("trigger_post_id", sa.Integer(), sa.ForeignKey("posts.id"), nullable=True),
sa.Column("wallet_address", sa.String(64), nullable=False),
sa.Column(
"opened_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("NOW()"),
),
sa.Column("closed_at", sa.DateTime(), nullable=True),
sa.Column("hl_order_id", sa.String(128), nullable=True),
)
op.create_index("ix_bot_trades_id", "bot_trades", ["id"])
op.create_index("ix_bot_trades_trigger_post_id", "bot_trades", ["trigger_post_id"])
op.create_index("ix_bot_trades_wallet_address", "bot_trades", ["wallet_address"])
# ── subscriptions ─────────────────────────────────────────────────────
op.create_table(
"subscriptions",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("wallet_address", sa.String(64), nullable=False),
sa.Column("hl_api_key", sa.String(256), nullable=True),
sa.Column("active", sa.Boolean(), nullable=False, server_default="false"),
sa.Column("subscribed_at", sa.DateTime(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("NOW()"),
),
sa.UniqueConstraint("wallet_address", name="uq_subscriptions_wallet_address"),
)
op.create_index("ix_subscriptions_id", "subscriptions", ["id"])
op.create_index(
"ix_subscriptions_wallet_address", "subscriptions", ["wallet_address"], unique=True
)
def downgrade() -> None:
op.drop_table("subscriptions")
op.drop_table("bot_trades")
op.drop_table("candles")
op.drop_table("posts")