Database sqlite

From Niller2005/PolyFlup · View interactive version

Database sqlite is an AI agent skill from the GitHub repository Niller2005/PolyFlup. SQLite best practices, connection management, and migration system for PolyFlup.

How to use this skill

Skill file

Database Best Practices

Connection Management

ALWAYS use the db_connection() context manager:

from src.data.db_connection import db_connection

with db_connection() as conn:
    c = conn.cursor()
    c.execute("SELECT * FROM trades")
    # Commit happens automatically on success

Migration System

The migration system tracks versions in the schema_version table.

Adding a New Migration:
  1. Create a migration function in src/data/migrations.py.
  2. Register it in the MIGRATIONS list.
  3. Update the base schema in src/data/database.py.
Rules:

Schema Overview

Main table: trades

Key Database Patterns

Position Queries (Atomic Hedging)
# Get open positions (both entry and hedge sides)
c.execute("""
    SELECT id, symbol, token_id, side, entry_price, size, bet_usd, 
           edge, window_end, order_id, order_status
    FROM trades 
    WHERE settled = 0 AND exited_early = 0 
    AND datetime(window_end) > datetime(?)
""", (now.isoformat(),))
Trade Updates (Emergency Liquidation)
# Update position after emergency sell
c.execute("""
    UPDATE trades 
    SET exited_early = 1, exit_price = ?, 
        pnl_usd = ?, roi_pct = ?, settled_at = ?
    WHERE id = ?
""", (exit_price, pnl_usd, roi_pct, now.isoformat(), trade_id))
Settlement
# Settle position with exit data
c.execute("""
    UPDATE trades 
    SET settled = 1, exited_early = 1, exit_price = ?, 
        pnl_usd = ?, roi_pct = ?, settled_at = ?
    WHERE id = ?
""", (exit_price, pnl_usd, roi_pct, now.isoformat(), trade_id))

WAL Mode

Migration History

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.