import sqlite3

from polyform_backend.db import Database, SCHEMA


def test_existing_release_schema_adds_project_scope_before_index(tmp_path):
    database_path = tmp_path / "legacy.db"
    legacy_schema = SCHEMA.replace(
        " project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE,\n", ""
    )
    with sqlite3.connect(database_path) as connection:
        connection.executescript(legacy_schema)

    Database(str(database_path)).initialize()

    with sqlite3.connect(database_path) as connection:
        columns = {row[1] for row in connection.execute("PRAGMA table_info(releases)")}
        indexes = {row[1] for row in connection.execute("PRAGMA index_list(releases)")}
    assert "project_id" in columns
    assert "ix_releases_project" in indexes
