Skip to main content

sqlite_graphrag/commands/
export.rs

1//! Handler for the `export` CLI subcommand.
2
3use crate::cli::MemoryType;
4use crate::errors::AppError;
5use crate::output;
6use crate::paths::AppPaths;
7use crate::storage::connection::open_ro;
8use serde::Serialize;
9
10#[derive(clap::Args)]
11#[command(after_long_help = "EXAMPLES:\n  \
12    # Export all memories as NDJSON\n  \
13    sqlite-graphrag export\n\n  \
14    # Export only decision memories from a namespace\n  \
15    sqlite-graphrag export --type decision --namespace my-project\n\n  \
16    # Export including soft-deleted memories\n  \
17    sqlite-graphrag export --include-deleted\n\n  \
18    # Pipe to file for backup\n  \
19    sqlite-graphrag export > backup.ndjson")]
20pub struct ExportArgs {
21    /// Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global).
22    #[arg(
23        long,
24        help = "Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global)"
25    )]
26    pub namespace: Option<String>,
27    /// Filter by memory type.
28    #[arg(long, value_enum)]
29    pub r#type: Option<MemoryType>,
30    /// Include soft-deleted memories in the export.
31    #[arg(long, default_value_t = false)]
32    pub include_deleted: bool,
33    /// Maximum number of memories to export (default: 100000).
34    #[arg(long, default_value_t = 100_000)]
35    pub limit: usize,
36    /// Offset for pagination.
37    #[arg(long, default_value_t = 0)]
38    pub offset: usize,
39    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
40    pub json: bool,
41    /// Path to graphrag.sqlite (overrides SQLITE_GRAPHRAG_DB_PATH and default CWD).
42    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
43    pub db: Option<String>,
44}
45
46#[derive(Serialize)]
47struct ExportMemoryLine {
48    name: String,
49    r#type: String,
50    description: String,
51    body: String,
52    namespace: String,
53    created_at_iso: String,
54    updated_at_iso: String,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    deleted_at_iso: Option<String>,
57}
58
59#[derive(Serialize)]
60struct ExportSummary {
61    summary: bool,
62    exported: usize,
63    namespace: String,
64    elapsed_ms: u64,
65}
66
67/// Exports memories as NDJSON (one JSON line per memory, followed by a summary line).
68pub fn run(args: ExportArgs) -> Result<(), AppError> {
69    let start = std::time::Instant::now();
70    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
71    let paths = AppPaths::resolve(args.db.as_deref())?;
72    crate::storage::connection::ensure_db_ready(&paths)?;
73    let conn = open_ro(&paths.db)?;
74
75    let deleted_filter = if args.include_deleted {
76        ""
77    } else {
78        "AND m.deleted_at IS NULL"
79    };
80
81    let limit_i64 = args.limit as i64;
82    let offset_i64 = args.offset as i64;
83    let type_str: Option<String> = args.r#type.map(|t| t.as_str().to_string());
84
85    let rows = fetch_rows(
86        &conn,
87        &namespace,
88        &type_str,
89        deleted_filter,
90        limit_i64,
91        offset_i64,
92    )?;
93
94    let exported = rows.len();
95    for line in &rows {
96        output::emit_json_compact(line)?;
97    }
98
99    output::emit_json_compact(&ExportSummary {
100        summary: true,
101        exported,
102        namespace: namespace.clone(),
103        elapsed_ms: start.elapsed().as_millis() as u64,
104    })?;
105
106    Ok(())
107}
108
109fn fetch_rows(
110    conn: &rusqlite::Connection,
111    namespace: &str,
112    type_str: &Option<String>,
113    deleted_filter: &str,
114    limit: i64,
115    offset: i64,
116) -> Result<Vec<ExportMemoryLine>, AppError> {
117    let rows = if let Some(t) = type_str {
118        let sql = format!(
119            "SELECT m.name, m.type, m.description, m.body, m.namespace, \
120                    m.created_at, m.updated_at, m.deleted_at \
121             FROM memories m \
122             WHERE m.namespace = ?1 {deleted_filter} AND m.type = ?2 \
123             ORDER BY m.name \
124             LIMIT ?3 OFFSET ?4"
125        );
126        let mut stmt = conn.prepare(&sql)?;
127        let result = stmt
128            .query_map(rusqlite::params![namespace, t, limit, offset], map_row)?
129            .collect::<Result<Vec<_>, _>>()?;
130        result
131    } else {
132        let sql = format!(
133            "SELECT m.name, m.type, m.description, m.body, m.namespace, \
134                    m.created_at, m.updated_at, m.deleted_at \
135             FROM memories m \
136             WHERE m.namespace = ?1 {deleted_filter} \
137             ORDER BY m.name \
138             LIMIT ?2 OFFSET ?3"
139        );
140        let mut stmt = conn.prepare(&sql)?;
141        let result = stmt
142            .query_map(rusqlite::params![namespace, limit, offset], map_row)?
143            .collect::<Result<Vec<_>, _>>()?;
144        result
145    };
146    Ok(rows)
147}
148
149fn map_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<ExportMemoryLine> {
150    Ok(ExportMemoryLine {
151        name: row.get(0)?,
152        r#type: row.get(1)?,
153        description: row.get(2)?,
154        body: row.get(3)?,
155        namespace: row.get(4)?,
156        created_at_iso: crate::tz::epoch_to_iso(row.get::<_, i64>(5)?),
157        updated_at_iso: crate::tz::epoch_to_iso(row.get::<_, i64>(6)?),
158        deleted_at_iso: row.get::<_, Option<i64>>(7)?.map(crate::tz::epoch_to_iso),
159    })
160}