Files
Palash Debnath 1f658f844e perf: speed up Electron startup, UI responsiveness, and backend DB/engine boot (#4320)
* perf: speed up Electron startup, UI rendering, and backend engine/DB boot

* chore: add changelog fragment for #4320

* perf: add SQLite indexes/pragmas, selective gzip, and immutable asset cache

* fix: restore the omnivoice-gallery pin to main's commit

The branch moved the submodule pointer back to 22e8e6da8, failing the
forward-only pin check.

* fix(db): create perf indexes after the additive reconcile so legacy DBs upgrade

The history/list sort indexes sat in _BASE_SCHEMA, so a legacy generation_history
without 'starred' aborted init_db's executescript with 'no such column: starred'
before _migrate could add it. Move them to _ensure_perf_indexes(), run after
_reconcile_additive_columns, skipping any index whose table/columns are absent.

* fix(electron): load the voices sidebar eagerly so it precedes page content

Lazy-loading VoicesSidebar made it mount after the page, so on first paint the
clone page's full-card voice button was the first 'Language fixture' button and
a click on it landed on the profile photo label (language-picker-smoke timeout).
It also made the sidebar pop in late. Restore the eager import.

* test(openai): drop the wire encoding when the SDK transport replays a decoded body

TestClient already decompresses res.content, so replaying it with the gzip
Content-Encoding header made httpx decode twice (incorrect header check) once
/v1/models was compressed. Add a wire-level check that the compressed body is valid.

* test(moss): resolve the lazy transformers __version__ before rebinding the module

transformers is a lazy module whose __version__ only enters its __dict__ once read;
the rebound copy lacked it when nothing had read it yet.

* fix(electron): serve app:// static files from a single open handle

Stat then read let the file change in between (CodeQL js/file-system-race). One
FileHandle now provides the stat, the read and the stream, and the content-length
of a buffered file comes from the bytes actually read.

* feat(db): add alembic revision 0019 for the list sort indexes

Schema changes go through alembic with a tested upgrade path. The revision creates
the seven indexes IF NOT EXISTS, only where the table and columns exist; init_db's
_ensure_perf_indexes stays for installs where alembic cannot run, and a test keeps
the two lists equal. Also make the alembic fast-path test fail if alembic is imported.

* fix(api): skip gzip for all media mounts and honour Accept-Encoding q-values

/voice_audio and /demo_audio were compressed on the event loop, and gzip;q=0 was
answered with gzip because Starlette matches the substring. Parse the header per
RFC 9110 before delegating, and require compression in the /v1/models wire test.

* fix(electron): start the backend after agent discovery, defer the dock, harden the palette

- Spawn Python only once registerRepairAgents set VOICESTUDIO_LLM_AGENTS; window,
  tray and updater still overlap with it.
- RepairAgentDock mounted at startup because Boolean(state) is always true; test runs.
- Command palette: LazyBoundary closes on a failed chunk, Escape works while the
  chunk loads, the highlight follows the command id, and loading/error are not
  reported as no matches.
- Tighten the app:// handle test to swap the path after the stat.

* fix(engines): report omnivoice and kittentts ready only when they really import

#4320 replaced their import check with a package-spec lookup, so an installed
package with a missing dependency or broken native library showed as ready on
/engines and /engines/{id}/health. Restore the real import (it is cached in
sys.modules after the first success); the parallel probing is kept. No other
backend used the spec-only pattern.

* fix(engines): probe engine availability serially again

The thread-pool probing in list_backends (tts and asr) and /engines raced the
first imports of heavy, interdependent packages, so the active engine could be
reported unavailable. Restore main's serial loops and pin it with a test.
2026-11-21 18:32:31 +05:30

845 lines
34 KiB
Python

import ast
import os
import re
import sqlite3
import logging
from contextlib import contextmanager
from core.config import DB_PATH
from core import db_backup
from core.version import APP_VERSION
logger = logging.getLogger("omnivoice.db")
_IDENT_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
_TYPE_RE = re.compile(r"^[A-Za-z0-9_ '\"\(\)\-\.]+$")
_WAL_INITIALIZED_PATHS: set[str] = set()
def get_db():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
if DB_PATH not in _WAL_INITIALIZED_PATHS:
conn.execute("PRAGMA journal_mode=WAL")
_WAL_INITIALIZED_PATHS.add(DB_PATH)
conn.execute("PRAGMA synchronous=NORMAL")
conn.execute("PRAGMA busy_timeout=5000")
conn.execute("PRAGMA temp_store=MEMORY")
conn.execute("PRAGMA cache_size=-16000")
conn.execute("PRAGMA foreign_keys=ON")
return conn
@contextmanager
def db_conn():
"""Context-managed SQLite connection that commits on clean exit and always closes."""
conn = get_db()
try:
yield conn
conn.commit()
except Exception:
try:
conn.rollback()
except Exception:
pass
raise
finally:
conn.close()
_BASE_SCHEMA = """
CREATE TABLE IF NOT EXISTS 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',
locked_audio_path TEXT DEFAULT '',
seed INTEGER DEFAULT NULL,
is_locked INTEGER DEFAULT 0,
personality TEXT DEFAULT '',
description TEXT DEFAULT '',
is_demo INTEGER DEFAULT 0,
verified_own_voice INTEGER DEFAULT 0,
consent_text TEXT DEFAULT '',
consent_audio_path TEXT DEFAULT '',
consent_recorded_at REAL DEFAULT NULL,
kind TEXT DEFAULT 'clone',
vd_states TEXT DEFAULT NULL,
design_recipe TEXT DEFAULT NULL,
created_at REAL,
sync_id TEXT DEFAULT NULL,
consent_basis TEXT DEFAULT '',
consent_attested_at TEXT DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS generation_history (
id TEXT PRIMARY KEY,
text TEXT,
mode TEXT,
language TEXT,
instruct TEXT,
profile_id TEXT,
audio_path TEXT,
duration_seconds REAL,
generation_time REAL,
seed INTEGER DEFAULT NULL,
starred INTEGER DEFAULT 0,
design_recipe TEXT DEFAULT NULL,
created_at REAL,
FOREIGN KEY (profile_id) REFERENCES voice_profiles(id)
);
CREATE TABLE IF NOT EXISTS dub_history (
id TEXT PRIMARY KEY,
filename TEXT,
duration REAL,
segments_count INTEGER,
language TEXT,
language_code TEXT,
tracks TEXT DEFAULT '[]',
job_data TEXT,
content_hash TEXT DEFAULT '',
created_at REAL
);
CREATE TABLE IF NOT EXISTS studio_projects (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
video_path TEXT,
audio_path TEXT,
duration REAL,
state_json TEXT,
created_at REAL,
updated_at REAL
);
CREATE TABLE IF NOT EXISTS export_history (
id TEXT PRIMARY KEY,
filename TEXT,
destination_path TEXT,
mode TEXT,
created_at REAL
);
CREATE TABLE IF NOT EXISTS glossary_terms (
id TEXT PRIMARY KEY,
project_id TEXT NOT NULL,
source TEXT NOT NULL,
target TEXT NOT NULL,
note TEXT DEFAULT '',
auto INTEGER DEFAULT 0,
created_at REAL
);
CREATE INDEX IF NOT EXISTS idx_glossary_project ON glossary_terms(project_id);
CREATE TABLE IF NOT EXISTS jobs (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
project_id TEXT,
status TEXT NOT NULL,
created_at REAL NOT NULL,
updated_at REAL NOT NULL,
finished_at REAL,
error TEXT,
meta_json TEXT DEFAULT '{}'
);
CREATE INDEX IF NOT EXISTS idx_jobs_status ON jobs(status);
CREATE INDEX IF NOT EXISTS idx_jobs_project ON jobs(project_id);
CREATE INDEX IF NOT EXISTS idx_jobs_created ON jobs(created_at);
CREATE TABLE IF NOT EXISTS job_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id TEXT NOT NULL,
seq INTEGER NOT NULL,
created_at REAL NOT NULL,
payload TEXT NOT NULL
);
-- The unique (job_id, seq) index is NOT created here: on a DB that still
-- holds duplicate pairs it would fail inside executescript and stop
-- startup. _ensure_job_events_unique_seq renumbers first, then creates it.
-- Phase 1 AUTH-02: encrypted per-install key/value store. Used today
-- for the HF token row + the per-install Fernet salt. Both fresh
-- installs (this CREATE) and v0.2.7 upgrades (alembic
-- 0001_phase1_settings) converge on the same schema.
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at REAL NOT NULL
);
-- Wave 2.2: per-agent MCP voice bindings. An MCP client (Claude Code,
-- Cursor, …) identified by the X-OmniVoice-Client-Id header it sends is
-- bound to a default voice profile / engine. Fresh installs create it
-- here; v0.3.x upgrades get it via alembic 0004.
CREATE TABLE IF NOT EXISTS mcp_client_bindings (
client_id TEXT PRIMARY KEY,
label TEXT NOT NULL DEFAULT '',
profile_id TEXT,
default_engine TEXT,
last_seen_at REAL,
created_at REAL
);
-- Expressive-TTS Spec 01 Phase 1: user pronunciation dictionary. A
-- per-language word→respelling map applied as pure text substitution
-- before synthesis (Settings → Pronunciation). Fresh installs create it
-- here; existing DBs get it via alembic 0008_pronunciation_dictionary.
-- Both paths converge on this identical schema (dual-path discipline).
CREATE TABLE IF NOT EXISTS pronunciation_entries (
id TEXT PRIMARY KEY,
term TEXT NOT NULL,
replacement TEXT NOT NULL DEFAULT '',
type TEXT NOT NULL DEFAULT 'respelling',
language TEXT NOT NULL DEFAULT '*',
enabled INTEGER NOT NULL DEFAULT 1,
created_at REAL,
-- #3873: '' = applies to every voice; else the voice_profiles.id the
-- entry belongs to. Existing DBs get it via alembic 0018 / the
-- additive reconcile. No index here: init_db runs this script before
-- any column is added, so an index on it would break old DBs.
profile_id TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_pron_lang ON pronunciation_entries(language);
-- Phone call agent (docs/integrations/calls.md): one row per call placed
-- or answered by the agent. Full numbers stay in this local DB; the API
-- returns only remote_masked. Existing DBs get it via alembic
-- 0012_call_sessions (dual-path discipline).
CREATE TABLE IF NOT EXISTS call_sessions (
id TEXT NOT NULL PRIMARY KEY,
direction TEXT NOT NULL DEFAULT 'outbound',
remote_number TEXT NOT NULL DEFAULT '',
remote_masked TEXT NOT NULL DEFAULT '',
from_number TEXT NOT NULL DEFAULT '',
provider TEXT NOT NULL DEFAULT 'twilio',
provider_call_id TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'queued',
brief TEXT NOT NULL DEFAULT '',
profile_id TEXT NOT NULL DEFAULT '',
engine TEXT NOT NULL DEFAULT '',
language TEXT NOT NULL DEFAULT '',
disclosure TEXT NOT NULL DEFAULT '',
max_minutes INTEGER NOT NULL DEFAULT 10,
outcome TEXT,
summary TEXT NOT NULL DEFAULT '',
transcript_json TEXT NOT NULL DEFAULT '[]',
timeline_json TEXT NOT NULL DEFAULT '[]',
recording_path TEXT NOT NULL DEFAULT '',
error TEXT NOT NULL DEFAULT '',
created_at REAL,
started_at REAL,
ended_at REAL,
duration_s REAL
);
CREATE INDEX IF NOT EXISTS idx_call_sessions_created ON call_sessions(created_at);
-- Legacy tables kept for existing installs; the feature moved to VoiceStudio Pro.
CREATE TABLE IF NOT EXISTS remote_workers (
id TEXT PRIMARY KEY,
name TEXT NOT NULL DEFAULT '',
key_id TEXT NOT NULL,
public_key BLOB NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
revoked INTEGER NOT NULL DEFAULT 0,
revoked_at REAL,
priority INTEGER NOT NULL DEFAULT 50,
endpoint TEXT NOT NULL DEFAULT '',
host_json TEXT NOT NULL DEFAULT '{}',
capabilities_json TEXT NOT NULL DEFAULT '[]',
max_concurrent_tasks INTEGER NOT NULL DEFAULT 1,
session_epoch INTEGER NOT NULL DEFAULT 0,
consent_granted_at REAL,
created_at REAL NOT NULL,
last_seen_at REAL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_remote_workers_key ON remote_workers(key_id);
CREATE TABLE IF NOT EXISTS remote_worker_enrollments (
token_id TEXT PRIMARY KEY,
secret_hash TEXT NOT NULL,
endpoint TEXT NOT NULL DEFAULT '',
cert_fingerprint TEXT NOT NULL DEFAULT '',
label TEXT NOT NULL DEFAULT '',
created_at REAL NOT NULL,
expires_at REAL NOT NULL,
used_at REAL,
used_by_worker TEXT
);
CREATE TABLE IF NOT EXISTS remote_tasks (
id TEXT PRIMARY KEY,
idempotency_key TEXT,
operation TEXT NOT NULL,
engine TEXT NOT NULL DEFAULT '',
model_id TEXT NOT NULL DEFAULT '',
params_json TEXT NOT NULL DEFAULT '{}',
priority INTEGER NOT NULL DEFAULT 0,
state TEXT NOT NULL DEFAULT 'queued',
max_attempts INTEGER NOT NULL DEFAULT 3,
excluded_json TEXT NOT NULL DEFAULT '[]',
error_json TEXT,
result_ref TEXT,
result_json TEXT,
project_id TEXT,
created_at REAL NOT NULL,
updated_at REAL NOT NULL,
deadline_at REAL,
-- Deliberate additive-reconcile exception to the alembic rule: remote
-- task recovery must work in bundled installs where alembic may be
-- unavailable, and this nullable affinity column is additive-only.
pinned_worker_id TEXT,
finished_at REAL
);
CREATE INDEX IF NOT EXISTS idx_remote_tasks_state ON remote_tasks(state, priority, created_at);
CREATE UNIQUE INDEX IF NOT EXISTS idx_remote_tasks_idem ON remote_tasks(idempotency_key)
WHERE idempotency_key IS NOT NULL;
CREATE TABLE IF NOT EXISTS remote_task_attempts (
id TEXT PRIMARY KEY,
task_id TEXT NOT NULL,
worker_id TEXT NOT NULL,
session_epoch INTEGER NOT NULL DEFAULT 0,
attempt_number INTEGER NOT NULL DEFAULT 1,
state TEXT NOT NULL DEFAULT 'assigned',
progress REAL NOT NULL DEFAULT 0,
stage TEXT NOT NULL DEFAULT '',
error_json TEXT,
created_at REAL NOT NULL,
accepted_at REAL,
started_at REAL,
finished_at REAL,
lease_expires_at REAL,
grace_expires_at REAL,
deadlines_json TEXT
);
CREATE INDEX IF NOT EXISTS idx_remote_attempts_task ON remote_task_attempts(task_id);
CREATE INDEX IF NOT EXISTS idx_remote_attempts_worker ON remote_task_attempts(worker_id, state);
-- VoiceStudio Cloud settings sync (S1). Same DDL as alembic 0014_cloud_sync.
CREATE TABLE IF NOT EXISTS cloud_sync_state (
organization_id TEXT PRIMARY KEY,
device_id TEXT,
cursor INTEGER NOT NULL DEFAULT 0,
enabled INTEGER NOT NULL DEFAULT 0,
choices_json TEXT NOT NULL DEFAULT '{}',
last_full_sync_at TEXT,
needs_update_json TEXT NOT NULL DEFAULT '{}',
full_pull_json TEXT NOT NULL DEFAULT '[]'
);
CREATE TABLE IF NOT EXISTS cloud_sync_items (
organization_id TEXT NOT NULL,
collection TEXT NOT NULL,
item_id TEXT NOT NULL,
revision INTEGER NOT NULL,
deleted INTEGER NOT NULL,
schema_version INTEGER NOT NULL,
content_json TEXT,
PRIMARY KEY (organization_id, collection, item_id)
);
CREATE TABLE IF NOT EXISTS cloud_sync_outbox (
id INTEGER PRIMARY KEY AUTOINCREMENT,
organization_id TEXT NOT NULL,
collection TEXT NOT NULL,
item_id TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('put','delete')),
idempotency_key TEXT NOT NULL,
queued_at TEXT NOT NULL,
attempts INTEGER NOT NULL DEFAULT 0,
last_error TEXT,
gen INTEGER NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX IF NOT EXISTS cloud_sync_outbox_one_per_item
ON cloud_sync_outbox (organization_id, collection, item_id);
CREATE TABLE IF NOT EXISTS cloud_sync_local (
key TEXT PRIMARY KEY,
value_json TEXT,
seq INTEGER NOT NULL,
origin TEXT NOT NULL,
meta_json TEXT NOT NULL DEFAULT '{}'
);
"""
# Only tables/columns this module is allowed to ALTER. Prevents SQL injection via
# the f-string ALTER below if these helpers ever get exposed to user input.
_ALLOWED_MIGRATIONS = {
("voice_profiles", "locked_audio_path"),
("voice_profiles", "seed"),
("voice_profiles", "is_locked"),
("voice_profiles", "personality"),
("generation_history", "seed"),
("dub_history", "content_hash"),
}
def _add_column_if_missing(conn, table: str, column: str, typedef: str):
if (table, column) not in _ALLOWED_MIGRATIONS:
raise ValueError(f"Migration not allowed: {table}.{column}")
if not _IDENT_RE.match(table) or not _IDENT_RE.match(column):
raise ValueError(f"Invalid identifier: {table}.{column}")
if not _TYPE_RE.match(typedef):
raise ValueError(f"Invalid typedef: {typedef!r}")
try:
conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {typedef}")
except sqlite3.OperationalError as e:
if "duplicate column" not in str(e).lower():
logger.warning("ALTER %s.%s failed: %s", table, column, e)
def _migrate(conn, current: int) -> int:
"""Apply migrations sequentially. Return new version."""
if current < 1:
_add_column_if_missing(conn, "voice_profiles", "locked_audio_path", "TEXT DEFAULT ''")
_add_column_if_missing(conn, "voice_profiles", "seed", "INTEGER DEFAULT NULL")
_add_column_if_missing(conn, "voice_profiles", "is_locked", "INTEGER DEFAULT 0")
_add_column_if_missing(conn, "generation_history", "seed", "INTEGER DEFAULT NULL")
current = 1
if current < 2:
_add_column_if_missing(conn, "dub_history", "content_hash", "TEXT DEFAULT ''")
current = 2
# v3: glossary_terms table lives in _BASE_SCHEMA (IF NOT EXISTS), so an old
# DB simply picks it up on the next init — no ALTER needed.
if current < 3:
current = 3
if current < 4:
_add_column_if_missing(conn, "voice_profiles", "personality", "TEXT DEFAULT ''")
current = 4
return current
def _reconcile_additive_columns(conn) -> None:
"""Make the live schema converge to ``_BASE_SCHEMA`` by ADDing any column the
canonical schema declares but an existing table is missing — the belt for
when alembic can't run on an upgraded DB.
``CREATE TABLE IF NOT EXISTS`` (init_db) never adds columns to a table that
already exists, the legacy ``_migrate`` only knows pre-0.3 columns, and
``_run_alembic_upgrade`` swallows failures. So a DB whose ``alembic_version``
is stamped at a removed revision (e.g. after running a preview build), or
where alembic isn't importable in the bundled interpreter, would otherwise
lose every alembic-era additive column forever — the ``no such column:
consent_audio_path`` 500 (#552/#547), and the same class for
``kind``/``vd_states``/``is_demo``/.... Additive only: never drops or retypes
a column, so it is safe and backward-compatible with existing user data. The
canonical names/types/defaults come solely from ``_BASE_SCHEMA`` (developer
controlled), so the ALTER is injection-safe.
"""
canon = sqlite3.connect(":memory:")
try:
canon.executescript(_BASE_SCHEMA)
_tables_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
live_tables = {r[0] for r in conn.execute(_tables_sql)}
for table in (r[0] for r in canon.execute(_tables_sql)):
if table not in live_tables:
continue # whole table missing → init_db's CREATE already made it
have = {r[1] for r in conn.execute(f"PRAGMA table_info({table})")}
# (cid, name, type, notnull, dflt_value, pk)
for _cid, name, ctype, notnull, dflt, _pk in canon.execute(f"PRAGMA table_info({table})"):
if name in have or not _IDENT_RE.match(name):
continue
ddl = f'ALTER TABLE "{table}" ADD COLUMN "{name}" {ctype or "TEXT"}'
if dflt is not None:
ddl += f" DEFAULT {dflt}"
elif notnull:
ddl += " DEFAULT ''" # SQLite requires a default to ADD a NOT NULL column
try:
conn.execute(ddl)
logger.info("schema reconcile: added missing column %s.%s", table, name)
except sqlite3.OperationalError as exc:
if "duplicate column" not in str(exc).lower():
logger.warning("schema reconcile ALTER %s.%s failed: %s", table, name, exc)
conn.commit()
finally:
canon.close()
def _ensure_job_events_unique_seq(conn) -> None:
"""Renumber duplicate (job_id, seq) events, then enforce uniqueness.
Mirrors alembic 0016 for installs where alembic cannot run. Duplicates came
from the old non-atomic writer; each is a real event, so rows are
renumbered in arrival order (seq, id) from the job's lowest seq, never
deleted. Jobs without a duplicate are untouched. Never raises: an index
that cannot be built must not stop startup.
"""
try:
dup_jobs = [
r[0]
for r in conn.execute(
"SELECT DISTINCT job_id FROM job_events GROUP BY job_id, seq HAVING COUNT(*) > 1"
)
]
for job_id in dup_jobs:
rows = conn.execute(
"SELECT id, seq FROM job_events WHERE job_id = ? ORDER BY seq, id", (job_id,)
).fetchall()
base = rows[0][1]
conn.executemany(
"UPDATE job_events SET seq = ? WHERE id = ?",
[(base + offset, row[0]) for offset, row in enumerate(rows)],
)
if dup_jobs:
logger.info("job_events: renumbered duplicate seqs for %d job(s)", len(dup_jobs))
conn.execute("DROP INDEX IF EXISTS idx_job_events_job_seq")
conn.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_job_events_job_seq_unique "
"ON job_events(job_id, seq)"
)
conn.commit()
except sqlite3.Error as exc:
conn.rollback()
logger.warning("job_events unique-seq repair failed: %s", exc)
def _ensure_voice_sync_id_index(conn) -> None:
"""Create the partial unique index on ``voice_profiles.sync_id``.
Alembic 0015 creates ``idx_voice_profiles_sync_id`` for the databases it can
reach. A bundled install whose interpreter cannot run alembic only ever gets
``_BASE_SCHEMA`` plus this reconciler, and the index cannot live in
``_BASE_SCHEMA``: ``CREATE TABLE IF NOT EXISTS`` never adds columns to a
pre-existing table, so an index over ``sync_id`` inside the same
``executescript`` would abort on a legacy table that predates the column.
Create it here instead, after the additive reconcile guarantees the column,
so both paths converge on the same schema (the invariant above).
Duplicate non-null sync ids already on a database (a local merge from before
the index existed) must not brick startup: the index is skipped with a
warning and cloud sync reports the identity conflict instead.
"""
try:
if "sync_id" not in {r[1] for r in conn.execute("PRAGMA table_info(voice_profiles)")}:
return
conn.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_voice_profiles_sync_id "
"ON voice_profiles(sync_id) WHERE sync_id IS NOT NULL"
)
conn.commit()
except sqlite3.Error as exc:
conn.rollback()
logger.warning("voice_profiles sync-id index not created: %s", exc)
_PERF_INDEXES = (
("idx_voice_profiles_created", "voice_profiles", ('created_at',), "created_at DESC"),
("idx_generation_history_created", "generation_history", ('created_at',), "created_at DESC"),
("idx_generation_history_profile", "generation_history", ('profile_id',), "profile_id"),
("idx_generation_history_starred", "generation_history", ('starred', 'created_at'), "starred, created_at DESC"),
("idx_dub_history_created", "dub_history", ('created_at',), "created_at DESC"),
("idx_studio_projects_updated", "studio_projects", ('updated_at',), "updated_at DESC"),
("idx_export_history_created", "export_history", ('created_at',), "created_at DESC"),
)
def _ensure_perf_indexes(conn) -> None:
"""Create the list/history sort indexes after the additive reconcile.
They cannot live in ``_BASE_SCHEMA``: ``CREATE TABLE IF NOT EXISTS`` never
adds columns to a pre-existing table, so an index over a column a legacy DB
predates (``generation_history.starred``) would abort the whole
``executescript`` before ``_migrate`` could add it. Each index is created
only when its table and columns exist; otherwise it is skipped with a log.
"""
for name, table, cols, spec in _PERF_INDEXES:
try:
have = {r[1] for r in conn.execute(f"PRAGMA table_info({table})")}
if not set(cols) <= have:
logger.warning("perf index %s skipped: %s lacks %s", name, table, sorted(set(cols) - have))
continue
conn.execute(f"CREATE INDEX IF NOT EXISTS {name} ON {table}({spec})")
except sqlite3.Error as exc:
logger.warning("perf index %s not created: %s", name, exc)
conn.commit()
def ensure_schema() -> None:
"""Idempotently ensure the base tables + additive columns exist.
A runtime self-heal for a DB that somehow missed init — e.g. a write hitting
``no such table: generation_history`` (#710) because ``init_db()``'s
``executescript`` never took on that DB. Safe to call anytime: it's just
``CREATE ... IF NOT EXISTS`` plus the additive-only column reconcile, so it
never drops or retypes anything and is backward-compatible with user data.
Cheaper than ``init_db()`` (skips the legacy ``_migrate`` + alembic), so a
write path can call it on a schema error and retry without a 500.
"""
conn = get_db()
try:
conn.executescript(_BASE_SCHEMA)
_reconcile_additive_columns(conn)
_ensure_job_events_unique_seq(conn)
_ensure_voice_sync_id_index(conn)
_ensure_perf_indexes(conn)
conn.commit()
finally:
conn.close()
def init_db():
_WAL_INITIALIZED_PATHS.discard(DB_PATH)
conn = get_db()
try:
conn.executescript(_BASE_SCHEMA)
version = conn.execute("PRAGMA user_version").fetchone()[0]
new_version = _migrate(conn, version)
if new_version != version:
conn.execute(f"PRAGMA user_version = {new_version}")
# Converge any alembic-era additive columns that CREATE TABLE IF NOT
# EXISTS + the legacy _migrate don't add to a pre-existing table
# (consent_audio_path, kind, ...). Runs regardless of whether alembic
# below succeeds, so an unrunnable alembic can't leave a 500-ing schema.
_reconcile_additive_columns(conn)
_ensure_job_events_unique_seq(conn)
_ensure_voice_sync_id_index(conn)
_ensure_perf_indexes(conn)
conn.commit()
finally:
conn.close()
# Phase 1: also run any pending alembic migrations. Fresh installs land
# the schema via _BASE_SCHEMA above; v0.2.7 → v0.3.0 upgrades pick up
# the same end-state via the alembic versions/ chain. Both paths
# converge because every migration uses `CREATE TABLE IF NOT EXISTS`
# or explicit existence checks.
_run_alembic_upgrade()
class MigrationError(RuntimeError):
"""A schema migration failed *while executing*. Startup must NOT continue
on a possibly half-migrated database — the caller lets this propagate so
the process stops with an actionable message naming the pre-migration
backup (see ``core.db_backup``). Restore is deliberately manual: silently
auto-restoring the snapshot could itself discard user data."""
def _reconcile_after_alembic_skip() -> None:
"""Converge the schema directly when alembic can't run at all (not
importable, or stamped at a removed revision — #552/#547) so additive
columns still land instead of 500-ing on `no such column`. Only for the
"nothing was applied" classes; a mid-migration failure must NOT reach
here (see MigrationError)."""
try:
conn = get_db()
try:
_reconcile_additive_columns(conn)
finally:
conn.close()
except Exception as exc: # noqa: BLE001
logger.warning("schema reconcile after alembic skip also failed: %s", exc)
def _stamped_revisions(db_path: str) -> set | None:
"""Revisions recorded in ``alembic_version`` (empty set = never stamped),
or None when the DB can't be read."""
try:
conn = sqlite3.connect(db_path)
try:
try:
return {r[0] for r in conn.execute("SELECT version_num FROM alembic_version")}
except sqlite3.OperationalError:
return set() # table absent — nothing ever stamped
finally:
conn.close()
except Exception: # noqa: BLE001
return None
def _plan_alembic(cfg) -> str:
"""Decide what an ``upgrade head`` run would actually do:
- ``up_to_date`` — stamped at head; upgrade is a no-op.
- ``pending`` — migrations WILL execute (snapshot the DB first).
- ``unknown_revision`` — stamped at a revision this build doesn't ship
(preview→stable downgrade, #552/#547); upgrade would fail before
applying anything, so skip it and reconcile additively instead.
- ``indeterminate`` — can't tell; treat like pending (snapshot, run).
"""
try:
from alembic.script import ScriptDirectory
script = ScriptDirectory.from_config(cfg)
known = {rev.revision for rev in script.walk_revisions()}
heads = set(script.get_heads())
stamped = _stamped_revisions(DB_PATH)
if stamped is None:
return "indeterminate"
if stamped and not stamped <= known:
return "unknown_revision"
if stamped == heads:
return "up_to_date"
return "pending"
except Exception: # noqa: BLE001
return "indeterminate"
def _fast_alembic_heads(versions_dir: str) -> set[str] | None:
"""Compute Alembic head revision IDs directly from migration files via AST.
Avoids importing ``alembic`` / ``sqlalchemy`` / ``mako`` (~900 ms) on warm
startups when the database is already stamped at head. Returns ``None`` if
any migration file cannot be parsed unambiguously, signalling the caller to
fall back to Alembic's own ``ScriptDirectory``.
"""
try:
entries = [
f for f in os.listdir(versions_dir)
if f.endswith(".py") and not f.startswith("_")
]
except OSError:
return None
if not entries:
return None
revisions: set[str] = set()
down_revisions: set[str] = set()
for fname in entries:
fpath = os.path.join(versions_dir, fname)
try:
with open(fpath, "r", encoding="utf-8") as fh:
tree = ast.parse(fh.read(), filename=fpath)
except Exception: # noqa: BLE001
return None
rev: str | None = None
down_seen = False
for node in tree.body:
target_name: str | None = None
value_node = None
if isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
target_name = node.targets[0].id
value_node = node.value
elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.value is not None:
target_name = node.target.id
value_node = node.value
if target_name not in ("revision", "down_revision"):
continue
try:
val = ast.literal_eval(value_node)
except Exception: # noqa: BLE001
return None
if target_name == "revision":
if not isinstance(val, str) or not val:
return None
rev = val
else:
down_seen = True
if val is None:
pass
elif isinstance(val, str):
down_revisions.add(val)
elif isinstance(val, (tuple, list)) and all(isinstance(x, str) for x in val):
down_revisions.update(val)
else:
return None
if rev is None or not down_seen:
return None
revisions.add(rev)
heads = revisions - down_revisions
return heads if heads else None
def _run_alembic_upgrade() -> None:
"""`alembic upgrade head` on startup, wrapped in the data-safety net.
Failure classes are handled differently on purpose:
- alembic unavailable / stamped at an unknown revision → **non-fatal**
(nothing was applied; warn + `_reconcile_additive_columns` keeps the
schema converged, exactly the pre-existing #552/#547 behavior).
- migrations actually pending → the DB is snapshotted first
(``omnivoice.db.backup-<version>-<n>``, newest 3 kept), then upgraded.
- a migration fails **while executing** → raise :class:`MigrationError`:
startup stops with a message naming the backup, instead of silently
running the app on a half-migrated DB.
"""
# Walk up from backend/core/db.py to find the alembic.ini at the
# project root.
here = os.path.dirname(os.path.abspath(__file__))
root = os.path.dirname(os.path.dirname(here))
ini = os.path.join(root, "alembic.ini")
if not os.path.isfile(ini):
logger.debug("alembic.ini not found at %s; skipping migrations", ini)
return
# Fast-path: when already stamped at head, skip importing alembic +
# sqlalchemy + mako (~900 ms saved on every normal app launch).
versions_dir = os.path.join(root, "backend", "migrations", "versions")
fast_heads = _fast_alembic_heads(versions_dir)
if fast_heads:
stamped = _stamped_revisions(DB_PATH)
if stamped and stamped == fast_heads:
return
try:
from alembic import command
from alembic.config import Config
cfg = Config(ini)
cfg.set_main_option("sqlalchemy.url", f"sqlite:///{DB_PATH}")
# In-app run: alembic.ini's logging section must not touch the live
# app's logging. env.py's fileConfig() — even with
# disable_existing_loggers=False — replaces the root logger's handlers
# and applies [logger_root] level=WARN, so every boot that actually
# migrated (first run, upgrades) lost the omnivoice.log file handler
# and all INFO logging for the rest of the process — including the
# graceful-shutdown trace, making a SIGTERM'd clean quit look like a
# silent crash (#1174). env.py checks this attribute; the standalone
# `alembic` CLI (which doesn't set it) keeps its logging config.
cfg.attributes["configure_logger"] = False
except Exception as exc: # noqa: BLE001 — alembic not importable / bad ini
logger.warning("alembic upgrade head skipped: %s", exc)
_reconcile_after_alembic_skip()
return
plan = _plan_alembic(cfg)
if plan == "up_to_date":
return
if plan == "unknown_revision":
logger.warning(
"alembic_version is stamped at a revision this build doesn't ship "
"(preview/newer build ran on this DB) — skipping alembic and "
"reconciling the schema additively (#552/#547)"
)
_reconcile_after_alembic_skip()
return
# Migrations may actually execute: snapshot the DB first so a failed or
# interrupted migration can never cost user data. A backup problem alone
# must not brick startup (the >500 MB skip is by design), so log and go on.
# ``db_backup``/``APP_VERSION`` are module-level imports (top of file), not
# re-imported here: a test that patches ``core.db_backup.MAX_BACKUP_DB_BYTES``
# on the object it imported at collection must see the same object this
# function uses. A lazy ``from core import db_backup`` would re-resolve
# through the (possibly re-imported) ``core`` package and silently miss the
# patch after another suite purged ``core.*`` from ``sys.modules``.
backup_path = None
try:
backup_path = db_backup.snapshot_before_migration(DB_PATH, APP_VERSION)
except Exception: # noqa: BLE001
logger.exception("Pre-migration DB backup failed — continuing without one")
try:
command.upgrade(cfg, "head")
except Exception as exc:
if "Can't locate revision" in str(exc):
# Belt for an unknown-revision case _plan_alembic missed: alembic
# bails before applying anything, so the old non-fatal path is safe.
logger.warning("alembic upgrade head skipped: %s", exc)
_reconcile_after_alembic_skip()
return
backup_note = (
f"A backup of your data from just before the migration is at: {backup_path}"
if backup_path
else "No pre-migration backup was written this run (see the log above)"
)
msg = (
f"Database migration failed while running: {exc}. "
f"VoiceStudio stopped instead of running on a partially migrated database, "
f"and nothing was auto-restored (your database at {DB_PATH} was left "
f"exactly as the failed migration left it). "
f"{backup_note}. "
"What to do: relaunch to retry; if it keeps failing, report it at "
"https://github.com/debpalash/VoiceStudio/issues (keep the backup file). "
"To roll back manually: quit the app, replace omnivoice.db with the backup "
"file, and reinstall the previous version."
)
logger.error(msg)
raise MigrationError(msg) from exc