Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

3.5 KB
Raw Blame
"""The runtime schema must self-heal additive columns even when `alembic
upgrade head` can't run — the "no such column: consent_audio_path" 500
(#552/#547) and its whole class (kind/vd_states/is_demo/...).

A DB whose alembic_version is stamped at a revision no longer in versions/
(common after running a preview/main build) makes alembic raise; the failure is
swallowed, and CREATE TABLE IF NOT EXISTS never adds columns to a pre-existing
table — so without reconciliation the new columns never land.
"""
import sqlite3

from core.db import _BASE_SCHEMA, _reconcile_additive_columns


def _cols(db_path, table="voice_profiles"):
    with sqlite3.connect(str(db_path)) as conn:
        return {r[1] for r in conn.execute(f"PRAGMA table_info({table})")}


def _base_schema_cols(table="voice_profiles"):
    canon = sqlite3.connect(":memory:")
    try:
        canon.executescript(_BASE_SCHEMA)
        return {r[1] for r in canon.execute(f"PRAGMA table_info({table})")}
    finally:
        canon.close()


# A pre-consent / pre-unification voice_profiles — missing every alembic-era
# additive column.
_LEGACY_PROFILES = """
    CREATE TABLE voice_profiles (
        id TEXT PRIMARY KEY,
        name TEXT NOT NULL,
        ref_audio_path TEXT,
        ref_text TEXT DEFAULT '',
        instruct TEXT DEFAULT '',
        language TEXT DEFAULT 'Auto',
        created_at REAL
    );
"""


def test_init_db_self_heals_missing_columns_when_alembic_fails(tmp_path, monkeypatch):
    db = tmp_path / "legacy.db"
    with sqlite3.connect(str(db)) as conn:
        conn.executescript(_LEGACY_PROFILES)
        conn.execute("INSERT INTO voice_profiles(id, name) VALUES ('vp-1', 'Alice')")
        # alembic stamped at a revision that no longer exists → command.upgrade
        # raises 'Can't locate revision', exercising the swallowed-failure path.
        conn.execute("CREATE TABLE alembic_version (version_num VARCHAR(32) NOT NULL)")
        conn.execute("INSERT INTO alembic_version VALUES ('0003_preview_removed_rev')")
        conn.commit()

    monkeypatch.setattr("core.db.DB_PATH", str(db))
    from core.db import init_db

    init_db()  # must NOT raise, and must converge the schema

    cols = _cols(db)
    for col in ("verified_own_voice", "consent_text", "consent_audio_path",
                "consent_recorded_at", "kind", "vd_states", "is_demo"):
        assert col in cols, f"schema reconcile did not add {col} (the #552 symptom)"
    # the existing row survives
    with sqlite3.connect(str(db)) as conn:
        assert conn.execute("SELECT name FROM voice_profiles WHERE id='vp-1'").fetchone()[0] == "Alice"


def test_reconcile_converges_voice_profiles_to_base_schema(tmp_path):
    db = tmp_path / "stripped.db"
    with sqlite3.connect(str(db)) as conn:
        conn.executescript(_LEGACY_PROFILES)
        conn.commit()
        _reconcile_additive_columns(conn)
    assert _cols(db) == _base_schema_cols(), "reconcile must match the canonical column set"


def test_reconcile_is_idempotent_and_additive_only(tmp_path):
    db = tmp_path / "twice.db"
    with sqlite3.connect(str(db)) as conn:
        conn.executescript(_LEGACY_PROFILES)
        conn.commit()
        _reconcile_additive_columns(conn)
        after_first = _cols(db)
        _reconcile_additive_columns(conn)  # second run must be a clean no-op
        after_second = _cols(db)
    assert after_first == after_second
    # additive only — the original legacy columns are never dropped
    assert {"id", "name", "instruct", "language"} <= after_second