Skip to main content

sqlite_graphrag/storage/
versions.rs

1//! Version history storage for memory records.
2//!
3//! Manages the `memory_versions` table: inserts a new version snapshot on
4//! every update so the `restore` command can roll back to any prior body.
5
6use crate::errors::AppError;
7use rusqlite::{params, Connection};
8
9/// Insert version.
10// One parameter per column of the `memory_versions` row this writes: the arity
11// IS the schema, and a struct here would be that row spelled a second time.
12#[allow(clippy::too_many_arguments)]
13pub fn insert_version(
14    conn: &Connection,
15    memory_id: i64,
16    version: i64,
17    name: &str,
18    memory_type: &str,
19    description: &str,
20    body: &str,
21    metadata: &str,
22    changed_by: Option<&str>,
23    change_reason: &str,
24) -> Result<(), AppError> {
25    conn.execute(
26        "INSERT INTO memory_versions
27         (memory_id, version, name, type, description, body, metadata, changed_by, change_reason)
28         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
29        params![
30            memory_id,
31            version,
32            name,
33            memory_type,
34            description,
35            body,
36            metadata,
37            changed_by,
38            change_reason
39        ],
40    )?;
41    Ok(())
42}
43
44/// Next version.
45pub fn next_version(conn: &Connection, memory_id: i64) -> Result<i64, AppError> {
46    let v: i64 = conn.query_row(
47        "SELECT COALESCE(MAX(version), 0) + 1 FROM memory_versions WHERE memory_id = ?1",
48        params![memory_id],
49        |r| r.get(0),
50    )?;
51    Ok(v)
52}