feat #14740: store the language per call

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-18 11:32:41 +01:00
co-authored by Claude Opus 5.5
parent 6183feaf82
commit 20eb53af4b
5 changed files with 107 additions and 8 deletions
+101 -8
View File
@@ -130,9 +130,25 @@ ALTER TABLE calls ADD COLUMN crm_base_url TEXT;
UPDATE calls SET crm_base_url = 'https://test.euvinopro.eu' WHERE crm_transcript_id IS NOT NULL;
";
/// Version 5: the language a call is transcribed in, chosen per call when
/// its transcription is started. Before this version every transcription
/// used the then default German: backfilled for the calls one was started
/// for, so their corrections (`call_view`) stay as they were; a call still
/// waiting gets its language when the user decides.
const SCHEMA_V5_SQL: &str = "
ALTER TABLE calls ADD COLUMN language TEXT;
UPDATE calls SET language = 'de' WHERE status NOT IN ('recording', 'awaiting_decision');
";
/// Schema steps by `PRAGMA user_version`: entry `i` takes a database from
/// version `i` to `i + 1`. A new database runs all of them.
const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL, SCHEMA_V3_SQL, SCHEMA_V4_SQL];
const MIGRATIONS: &[&str] = &[
SCHEMA_V1_SQL,
SCHEMA_V2_SQL,
SCHEMA_V3_SQL,
SCHEMA_V4_SQL,
SCHEMA_V5_SQL,
];
/// SQLite-backed store for calls and everything derived from them.
pub struct Store {
@@ -390,6 +406,16 @@ impl Store {
Ok(())
}
/// Sets the language (ISO 639-1 code) the call is transcribed in.
pub fn set_language(&self, id: CallId, language: &str) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE calls SET language = ?1 WHERE id = ?2",
params![language, id],
)?;
Ok(())
}
pub fn mark_transcribed(&self, id: CallId, at: DateTime<Local>) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
@@ -549,9 +575,9 @@ fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Ve
Ok(ids)
}
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url FROM calls";
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url, language FROM calls";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url FROM calls WHERE id = ?1";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url, language FROM calls WHERE id = ?1";
fn row_to_call(row: &Row) -> Result<Call> {
let id: CallId = row.get(0)?;
@@ -569,6 +595,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
let crm_transcript_id: Option<String> = row.get(12)?;
let uploaded_at: Option<String> = row.get(13)?;
let crm_base_url: Option<String> = row.get(14)?;
let language: Option<String> = row.get(15)?;
Ok(Call {
id,
@@ -586,6 +613,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
crm_transcript_id,
crm_base_url,
uploaded_at: opt_dt_from_str(uploaded_at)?,
language,
})
}
@@ -715,6 +743,14 @@ CREATE TABLE speaker_merges (
const RELEASED_V3_STEP_SQL: &str = "
ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
";
/// The schema of user_version 4, exactly as release 0.2.2 created it
/// (copied from `SCHEMA_V4_SQL` at `HEAD`): version 3 plus these
/// statements.
const RELEASED_V4_STEP_SQL: &str = "
ALTER TABLE calls ADD COLUMN crm_base_url TEXT;
UPDATE calls SET crm_base_url = 'https://test.euvinopro.eu' WHERE crm_transcript_id IS NOT NULL;
";
fn user_version(path: &Path) -> i64 {
@@ -738,7 +774,7 @@ ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
}
#[test]
fn a_new_database_starts_at_version_4() {
fn a_new_database_starts_at_version_5() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let store = Store::open(&path).unwrap();
@@ -750,7 +786,7 @@ ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
.unwrap();
drop(store);
assert_eq!(user_version(&path), 4);
assert_eq!(user_version(&path), 5);
}
#[test]
@@ -807,7 +843,7 @@ ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
store.set_title(1, Some("Lieferung März")).unwrap();
store.set_speaker_merge(1, "remote:2", "remote:1").unwrap();
drop(store);
assert_eq!(user_version(&path), 4);
assert_eq!(user_version(&path), 5);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.title.as_deref(), Some("Lieferung März"));
@@ -861,7 +897,7 @@ ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
.set_uploaded(1, "crm-1", "https://backend.example", uploaded)
.unwrap();
drop(store);
assert_eq!(user_version(&path), 4);
assert_eq!(user_version(&path), 5);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.crm_transcript_id.as_deref(), Some("crm-1"));
@@ -915,7 +951,64 @@ ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
assert_eq!(not_uploaded.crm_transcript_id, None);
assert_eq!(not_uploaded.crm_base_url, None);
assert_eq!(user_version(&path), 4);
assert_eq!(user_version(&path), 5);
}
#[test]
fn a_version_4_database_is_upgraded_backfilling_german_for_started_transcriptions() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let started = "2026-11-05T10:14:00+01:00";
{
let conn = rusqlite::Connection::open(&path).unwrap();
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
conn.execute_batch(RELEASED_V2_STEP_SQL).unwrap();
conn.execute_batch(RELEASED_V3_STEP_SQL).unwrap();
conn.execute_batch(RELEASED_V4_STEP_SQL).unwrap();
conn.execute_batch("PRAGMA user_version = 4;").unwrap();
for (app, status) in [
("Microsoft Teams", "done"),
("Zoom", "failed"),
("MicroSIP", "awaiting_decision"),
] {
conn.execute(
"INSERT INTO calls \
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted) \
VALUES (?1, ?2, ?2, ?3, 0, NULL, '/rec/mic.wav', '/rec/out.wav', NULL, 0)",
rusqlite::params![app, started, status],
)
.unwrap();
}
}
let store = Store::open(&path).unwrap();
// Transcriptions started before this version all used the then
// default German; a call still waiting has no language yet.
let languages: Vec<_> = (1..=3)
.map(|id| store.get_call(id).unwrap().unwrap().language)
.collect();
assert_eq!(
languages,
[Some("de".to_string()), Some("de".to_string()), None]
);
assert_eq!(user_version(&path), 5);
}
#[test]
fn the_language_is_stored_per_call() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
let other = new_call(&store);
assert_eq!(store.get_call(id).unwrap().unwrap().language, None);
store.set_language(id, "fr").unwrap();
assert_eq!(
store.get_call(id).unwrap().unwrap().language.as_deref(),
Some("fr")
);
assert_eq!(store.get_call(other).unwrap().unwrap().language, None);
}
#[test]