feat #14740: track uploads per backend server

Schema v4 adds calls.crm_base_url; existing uploads are tagged with the
test backend. A call counts as uploaded (list/detail fields, the
"Hochgeladen am" line, the upload-once guard) only if it was uploaded to
this build's backend. Uploading to the other server replaces the single
upload record; nothing is deleted.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-09 10:52:16 +01:00
co-authored by Claude Opus 5.5
parent d0ece263a2
commit 3a16c76257
5 changed files with 265 additions and 42 deletions
+121 -22
View File
@@ -120,9 +120,19 @@ ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
";
/// Version 4: which backend an upload belongs to, so a build whose own
/// backend differs (see `crate::backend`) does not show someone else's
/// upload as done (`call_view::uploaded_here`). Every upload before this
/// version only ever went to the test backend (production never had the
/// API yet): backfilled here, so existing rows keep meaning what they did.
const SCHEMA_V4_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;
";
/// 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];
const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL, SCHEMA_V3_SQL, SCHEMA_V4_SQL];
/// SQLite-backed store for calls and everything derived from them.
pub struct Store {
@@ -361,18 +371,21 @@ impl Store {
Ok(())
}
/// Records that the transcript was uploaded to the CRM at `at`, which
/// gave it `crm_transcript_id`; replaces an earlier upload's record.
/// Records that the transcript was uploaded to `crm_base_url` at `at`,
/// which gave it `crm_transcript_id`; replaces an earlier upload's
/// record (even one of another backend — one upload slot per call, see
/// `call_view::uploaded_here`).
pub fn set_uploaded(
&self,
id: CallId,
crm_transcript_id: &str,
crm_base_url: &str,
at: DateTime<Local>,
) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE calls SET crm_transcript_id = ?1, uploaded_at = ?2 WHERE id = ?3",
params![crm_transcript_id, dt_to_str(at), id],
"UPDATE calls SET crm_transcript_id = ?1, crm_base_url = ?2, uploaded_at = ?3 WHERE id = ?4",
params![crm_transcript_id, crm_base_url, dt_to_str(at), id],
)?;
Ok(())
}
@@ -536,9 +549,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 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 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 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 FROM calls WHERE id = ?1";
fn row_to_call(row: &Row) -> Result<Call> {
let id: CallId = row.get(0)?;
@@ -555,6 +568,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
let title: Option<String> = row.get(11)?;
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)?;
Ok(Call {
id,
@@ -570,6 +584,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
audio_deleted,
title,
crm_transcript_id,
crm_base_url,
uploaded_at: opt_dt_from_str(uploaded_at)?,
})
}
@@ -692,6 +707,14 @@ CREATE TABLE speaker_merges (
into_key TEXT NOT NULL,
PRIMARY KEY (call_id, from_key)
);
";
/// The schema of user_version 3, exactly as release 0.2.0 created it
/// (copied from `SCHEMA_V3_SQL` at `HEAD`): version 2 plus these
/// statements.
const RELEASED_V3_STEP_SQL: &str = "
ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
";
fn user_version(path: &Path) -> i64 {
@@ -715,17 +738,19 @@ CREATE TABLE speaker_merges (
}
#[test]
fn a_new_database_starts_at_version_3() {
fn a_new_database_starts_at_version_4() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let store = Store::open(&path).unwrap();
let id = new_call(&store);
store.set_title(id, Some("Angebot")).unwrap();
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
store.set_uploaded(id, "4711", Local::now()).unwrap();
store
.set_uploaded(id, "4711", "https://backend.example", Local::now())
.unwrap();
drop(store);
assert_eq!(user_version(&path), 3);
assert_eq!(user_version(&path), 4);
}
#[test]
@@ -782,7 +807,7 @@ CREATE TABLE speaker_merges (
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), 3);
assert_eq!(user_version(&path), 4);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.title.as_deref(), Some("Lieferung März"));
@@ -832,41 +857,115 @@ CREATE TABLE speaker_merges (
// The new columns work, and opening again changes nothing.
let uploaded = Local::now();
store.set_uploaded(1, "crm-1", uploaded).unwrap();
store
.set_uploaded(1, "crm-1", "https://backend.example", uploaded)
.unwrap();
drop(store);
assert_eq!(user_version(&path), 3);
assert_eq!(user_version(&path), 4);
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"));
assert_eq!(
call.crm_base_url.as_deref(),
Some("https://backend.example")
);
assert_eq!(call.uploaded_at, Some(uploaded));
}
#[test]
fn an_upload_is_recorded_and_replaced_by_the_next() {
fn a_version_3_database_is_upgraded_backfilling_the_test_backend_for_uploads() {
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("PRAGMA user_version = 3;").unwrap();
// An already-uploaded call (every pre-v4 upload went to the
// test backend; production never had the API).
conn.execute(
"INSERT INTO calls \
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 0, NULL, NULL, 'crm-1', ?1)",
rusqlite::params![started],
)
.unwrap();
// A call that was never uploaded.
conn.execute(
"INSERT INTO calls \
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \
VALUES ('MicroSIP', ?1, NULL, 'awaiting_decision', 0, NULL, '/rec/2-mic.wav', '/rec/2-out.wav', NULL, 0, NULL, NULL, NULL, NULL)",
rusqlite::params![started],
)
.unwrap();
}
let store = Store::open(&path).unwrap();
let uploaded = store.get_call(1).unwrap().expect("call kept");
assert_eq!(uploaded.crm_transcript_id.as_deref(), Some("crm-1"));
assert_eq!(
uploaded.crm_base_url.as_deref(),
Some("https://test.euvinopro.eu")
);
let not_uploaded = store.get_call(2).unwrap().expect("call kept");
assert_eq!(not_uploaded.crm_transcript_id, None);
assert_eq!(not_uploaded.crm_base_url, None);
assert_eq!(user_version(&path), 4);
}
#[test]
fn an_upload_is_recorded_and_replaced_by_the_next_even_for_another_backend() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
let other = new_call(&store);
let first = Local::now();
let second = first + chrono::Duration::minutes(5);
store.set_uploaded(id, "4711", first).unwrap();
store
.set_uploaded(id, "4711", "https://test.euvinopro.eu", first)
.unwrap();
let call = store.get_call(id).unwrap().unwrap();
assert_eq!(
(call.crm_transcript_id.as_deref(), call.uploaded_at),
(Some("4711"), Some(first))
(
call.crm_transcript_id.as_deref(),
call.crm_base_url.as_deref(),
call.uploaded_at
),
(Some("4711"), Some("https://test.euvinopro.eu"), Some(first))
);
store.set_uploaded(id, "4712", second).unwrap();
// One upload slot per call: a later upload, even to a different
// backend, replaces it rather than keeping both.
store
.set_uploaded(id, "4712", "https://backend.euvinopro.eu", second)
.unwrap();
let listed = store.list_calls().unwrap();
let call = listed.iter().find(|c| c.id == id).unwrap();
assert_eq!(
(call.crm_transcript_id.as_deref(), call.uploaded_at),
(Some("4712"), Some(second))
(
call.crm_transcript_id.as_deref(),
call.crm_base_url.as_deref(),
call.uploaded_at
),
(
Some("4712"),
Some("https://backend.euvinopro.eu"),
Some(second)
)
);
let untouched = store.get_call(other).unwrap().unwrap();
assert_eq!(
(untouched.crm_transcript_id, untouched.uploaded_at),
(None, None)
(
untouched.crm_transcript_id,
untouched.crm_base_url,
untouched.uploaded_at
),
(None, None, None)
);
}