from __future__ import annotations

import sqlite3

from backend.database.migrations.runner import Migration


def upgrade(connection: sqlite3.Connection) -> None:
    connection.execute(
        """
        CREATE TABLE screener_run_backtests (
            run_id INTEGER PRIMARY KEY
                REFERENCES screener_runs(id) ON DELETE CASCADE,
            payload_json TEXT NOT NULL,
            created_at TEXT NOT NULL
        )
        """
    )


def downgrade(connection: sqlite3.Connection) -> None:
    connection.execute("DROP TABLE screener_run_backtests")


MIGRATION = Migration(
    version=12,
    name="create_screener_backtests",
    signature="screener:v2:persistent-rolling-backtests",
    upgrade=upgrade,
    downgrade=downgrade,
)
