Skip to main content

sqlite_graphrag/commands/
rename_entity.rs

1//! Handler for the `rename-entity` CLI subcommand.
2//!
3//! Renames an entity preserving all relationships and memory bindings.
4//! Only the `name` column in `entities` and the corresponding `vec_entities`
5//! row need updating because relationships use integer FK `entity_id`.
6
7use crate::entity_type::EntityType;
8use crate::errors::AppError;
9use crate::i18n::errors_msg;
10use crate::output::{self, OutputFormat};
11use crate::paths::AppPaths;
12use crate::storage::connection::open_rw;
13use crate::storage::entities;
14use rusqlite::params;
15use serde::Serialize;
16
17#[derive(clap::Args)]
18#[command(after_long_help = "EXAMPLES:\n  \
19    # Rename an entity\n  \
20    sqlite-graphrag rename-entity --name old-name --new-name new-name\n\n  \
21    # Rename with namespace\n  \
22    sqlite-graphrag rename-entity --name auth --new-name authentication --namespace my-project\n\n  \
23    # Rename by ID (unambiguous when homonyms exist across namespaces)\n  \
24    sqlite-graphrag rename-entity --id 42 --new-name authentication")]
25pub struct RenameEntityArgs {
26    /// Current entity name to rename.
27    #[arg(
28        long,
29        value_name = "NAME",
30        required_unless_present = "id",
31        conflicts_with = "id"
32    )]
33    pub name: Option<String>,
34    /// v1.1.1 (P5): entity ID to rename. IDs are globally unique, so --id
35    /// disambiguates homonyms across namespaces. Conflicts with --name; the
36    /// entity must belong to the resolved namespace.
37    #[arg(long, value_name = "ID")]
38    pub id: Option<i64>,
39    /// New name for the entity.
40    #[arg(long, value_name = "NEW_NAME")]
41    pub new_name: String,
42    #[arg(long)]
43    pub namespace: Option<String>,
44    #[arg(long, value_enum, default_value = "json")]
45    pub format: OutputFormat,
46    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
47    pub json: bool,
48    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
49    pub db: Option<String>,
50}
51
52#[derive(Serialize)]
53struct RenameEntityResponse {
54    action: String,
55    old_name: String,
56    new_name: String,
57    entity_id: i64,
58    namespace: String,
59    elapsed_ms: u64,
60}
61
62/// v1.1.1 (P5): resolves an entity ID to `(id, type, stored name)`, enforcing
63/// that the entity exists AND belongs to the namespace — IDs are global, so a
64/// bare existence check could silently cross namespaces.
65fn lookup_entity_by_id(
66    conn: &rusqlite::Connection,
67    namespace: &str,
68    id: i64,
69) -> Result<(i64, EntityType, String), AppError> {
70    let mut stmt = conn
71        .prepare_cached("SELECT id, type, name FROM entities WHERE id = ?1 AND namespace = ?2")?;
72    match stmt.query_row(params![id, namespace], |r| {
73        Ok((
74            r.get::<_, i64>(0)?,
75            r.get::<_, EntityType>(1)?,
76            r.get::<_, String>(2)?,
77        ))
78    }) {
79        Ok(row) => Ok(row),
80        Err(rusqlite::Error::QueryReturnedNoRows) => Err(AppError::NotFound(format!(
81            "entity id={id} not found in namespace '{namespace}'"
82        ))),
83        Err(e) => Err(AppError::Database(e)),
84    }
85}
86
87pub fn run(
88    args: RenameEntityArgs,
89    llm_backend: crate::cli::LlmBackendChoice,
90    embedding_backend: crate::cli::EmbeddingBackendChoice,
91) -> Result<(), AppError> {
92    let start = std::time::Instant::now();
93    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
94    let paths = AppPaths::resolve(args.db.as_deref())?;
95
96    crate::storage::connection::ensure_db_ready(&paths)?;
97
98    let mut conn = open_rw(&paths.db)?;
99
100    // Verify the source entity exists and fetch id, type and stored name —
101    // by ID (v1.1.1 P5, unambiguous across homonyms) or by normalized name.
102    // Existence is validated here, BEFORE any mutation.
103    let (entity_id, entity_type, old_name) = match args.id {
104        Some(id) => lookup_entity_by_id(&conn, &namespace, id)?,
105        None => {
106            let Some(ref raw_name) = args.name else {
107                return Err(AppError::Validation(
108                    "--name or --id is required".to_string(),
109                ));
110            };
111            // Normalize the lookup name to match the normalized stored names.
112            let lookup_name = crate::parsers::normalize_entity_name(raw_name);
113            let mut stmt = conn.prepare_cached(
114                "SELECT id, type FROM entities WHERE namespace = ?1 AND name = ?2",
115            )?;
116            match stmt.query_row(params![namespace, lookup_name], |r| {
117                Ok((r.get::<_, i64>(0)?, r.get::<_, EntityType>(1)?))
118            }) {
119                Ok((id, ty)) => (id, ty, lookup_name),
120                Err(rusqlite::Error::QueryReturnedNoRows) => {
121                    return Err(AppError::NotFound(errors_msg::entity_not_found(
122                        raw_name, &namespace,
123                    )))
124                }
125                Err(e) => return Err(AppError::Database(e)),
126            }
127        }
128    };
129
130    // Validate the raw new name first (catches short ALL_CAPS NER noise),
131    // then normalize it for storage to preserve the normalized-name invariant.
132    entities::validate_entity_name(&args.new_name)?;
133    let new_name = crate::parsers::normalize_entity_name(&args.new_name);
134
135    if old_name == new_name {
136        return Err(AppError::Validation(
137            "source and target entity names are identical".to_string(),
138        ));
139    }
140
141    // Ensure new name is not already taken in this namespace.
142    if entities::find_entity_id(&conn, &namespace, &new_name)?.is_some() {
143        return Err(AppError::Validation(format!(
144            "entity with name '{new_name}' already exists in namespace '{namespace}'"
145        )));
146    }
147
148    let skip_embed = crate::embedder::should_skip_embedding_on_failure();
149    let embedding: Option<Vec<f32>> = match crate::embedder::embed_passage_with_embedding_choice(
150        &paths.models,
151        &new_name,
152        embedding_backend,
153        llm_backend,
154    ) {
155        Ok((emb, _backend)) => Some(emb),
156        Err(AppError::Validation(msg)) => return Err(AppError::Validation(msg)),
157        Err(e) if skip_embed => {
158            tracing::warn!(error = %e, "rename-entity: embedding failed; --skip-embedding-on-failure active, persisting without embedding");
159            None
160        }
161        Err(e) => return Err(e),
162    };
163
164    let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
165    tx.execute(
166        "UPDATE entities SET name = ?1, updated_at = unixepoch() WHERE id = ?2",
167        params![new_name, entity_id],
168    )?;
169    // v1.0.76: BLOB-backed entity_embeddings table (PK = entity_id).
170    // G43: reuse the canonical writer instead of a duplicated INSERT that
171    // hardcoded dim=384 and a removed local model name; `upsert_entity_vec`
172    // records the real vector length and the CLI version as `model`.
173    if let Some(ref emb) = embedding {
174        entities::upsert_entity_vec(&tx, entity_id, &namespace, entity_type, emb, &new_name)?;
175    }
176    tx.commit()?;
177
178    conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
179
180    let response = RenameEntityResponse {
181        action: "renamed".to_string(),
182        old_name,
183        new_name,
184        entity_id,
185        namespace: namespace.clone(),
186        elapsed_ms: start.elapsed().as_millis() as u64,
187    };
188
189    match args.format {
190        OutputFormat::Json => output::emit_json(&response)?,
191        OutputFormat::Text | OutputFormat::Markdown => {
192            output::emit_text(&format!(
193                "renamed entity: '{}' → '{}' [{}]",
194                response.old_name, response.new_name, response.namespace
195            ));
196        }
197    }
198
199    Ok(())
200}
201
202#[cfg(test)]
203mod tests {
204    use super::*;
205
206    // v1.1.1 (P5): ID lookup is namespace-scoped and returns the stored name,
207    // so homonyms across namespaces resolve deterministically.
208    #[test]
209    fn lookup_entity_by_id_disambiguates_homonyms_across_namespaces() {
210        let conn = rusqlite::Connection::open_in_memory().unwrap();
211        conn.execute_batch(
212            "CREATE TABLE entities (
213                id INTEGER PRIMARY KEY,
214                namespace TEXT NOT NULL,
215                name TEXT NOT NULL,
216                type TEXT NOT NULL,
217                UNIQUE(namespace, name)
218            );",
219        )
220        .unwrap();
221        conn.execute(
222            "INSERT INTO entities (id, namespace, name, type)
223             VALUES (1, 'ns-a', 'auth', 'concept'), (2, 'ns-b', 'auth', 'tool')",
224            [],
225        )
226        .unwrap();
227
228        let (id, ty, name) = lookup_entity_by_id(&conn, "ns-b", 2).unwrap();
229        assert_eq!(id, 2);
230        assert_eq!(name, "auth");
231        assert_eq!(ty, EntityType::Tool);
232
233        let err = lookup_entity_by_id(&conn, "ns-b", 1).unwrap_err();
234        assert_eq!(err.exit_code(), 4, "cross-namespace ID must be NotFound");
235        assert!(err.to_string().contains("id=1"), "obtido: {err}");
236    }
237
238    // v1.1.1 (P5): --name and --id are mutually exclusive at the clap level,
239    // and at least one selector is required.
240    #[derive(clap::Parser)]
241    struct TestCli {
242        #[command(flatten)]
243        args: RenameEntityArgs,
244    }
245
246    #[test]
247    fn clap_rejects_name_combined_with_id() {
248        use clap::Parser;
249        let err =
250            match TestCli::try_parse_from(["t", "--name", "auth", "--id", "42", "--new-name", "x"])
251            {
252                Ok(_) => panic!("expected argument conflict"),
253                Err(e) => e,
254            };
255        assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
256    }
257
258    #[test]
259    fn clap_requires_name_or_id() {
260        use clap::Parser;
261        assert!(TestCli::try_parse_from(["t", "--new-name", "x"]).is_err());
262        let ok = match TestCli::try_parse_from(["t", "--id", "7", "--new-name", "x"]) {
263            Ok(cli) => cli,
264            Err(e) => panic!("expected successful parse: {e}"),
265        };
266        assert_eq!(ok.args.id, Some(7));
267        assert!(ok.args.name.is_none());
268    }
269
270    #[test]
271    fn rename_entity_response_serializes_all_fields() {
272        let resp = RenameEntityResponse {
273            action: "renamed".to_string(),
274            old_name: "auth".to_string(),
275            new_name: "authentication".to_string(),
276            entity_id: 42,
277            namespace: "global".to_string(),
278            elapsed_ms: 7,
279        };
280        let json = serde_json::to_value(&resp).expect("serialization failed");
281        assert_eq!(json["action"], "renamed");
282        assert_eq!(json["old_name"], "auth");
283        assert_eq!(json["new_name"], "authentication");
284        assert_eq!(json["entity_id"], 42);
285        assert_eq!(json["namespace"], "global");
286        assert!(json["elapsed_ms"].is_number());
287    }
288
289    #[test]
290    fn rename_entity_response_action_is_renamed() {
291        let resp = RenameEntityResponse {
292            action: "renamed".to_string(),
293            old_name: "x".to_string(),
294            new_name: "y".to_string(),
295            entity_id: 1,
296            namespace: "ns".to_string(),
297            elapsed_ms: 1,
298        };
299        assert_eq!(resp.action, "renamed");
300    }
301
302    #[test]
303    fn rename_entity_response_entity_id_preserved() {
304        let resp = RenameEntityResponse {
305            action: "renamed".to_string(),
306            old_name: "old".to_string(),
307            new_name: "new".to_string(),
308            entity_id: 999,
309            namespace: "test-ns".to_string(),
310            elapsed_ms: 5,
311        };
312        let json = serde_json::to_value(&resp).expect("serialization failed");
313        assert_eq!(json["entity_id"], 999);
314    }
315
316    #[test]
317    fn rejects_rename_entity_to_same_name() {
318        use crate::errors::AppError;
319        let err = AppError::Validation("source and target entity names are identical".to_string());
320        assert_eq!(err.exit_code(), 1);
321        assert!(err.to_string().contains("identical"));
322    }
323
324    #[test]
325    fn rename_entity_response_namespace_reflected() {
326        let resp = RenameEntityResponse {
327            action: "renamed".to_string(),
328            old_name: "a".to_string(),
329            new_name: "b".to_string(),
330            entity_id: 10,
331            namespace: "my-project".to_string(),
332            elapsed_ms: 2,
333        };
334        let json = serde_json::to_value(&resp).expect("serialization failed");
335        assert_eq!(json["namespace"], "my-project");
336    }
337}