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")]
23pub struct RenameEntityArgs {
24    /// Current entity name to rename.
25    #[arg(long, value_name = "NAME")]
26    pub name: String,
27    /// New name for the entity.
28    #[arg(long, value_name = "NEW_NAME")]
29    pub new_name: String,
30    #[arg(long)]
31    pub namespace: Option<String>,
32    #[arg(long, value_enum, default_value = "json")]
33    pub format: OutputFormat,
34    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
35    pub json: bool,
36    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
37    pub db: Option<String>,
38}
39
40#[derive(Serialize)]
41struct RenameEntityResponse {
42    action: String,
43    old_name: String,
44    new_name: String,
45    entity_id: i64,
46    namespace: String,
47    elapsed_ms: u64,
48}
49
50pub fn run(
51    args: RenameEntityArgs,
52    llm_backend: crate::cli::LlmBackendChoice,
53) -> Result<(), AppError> {
54    let start = std::time::Instant::now();
55    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
56    let paths = AppPaths::resolve(args.db.as_deref())?;
57
58    crate::storage::connection::ensure_db_ready(&paths)?;
59
60    let mut conn = open_rw(&paths.db)?;
61
62    // Verify source entity exists and fetch its id and type.
63    // Normalize the lookup name to match the normalized stored names.
64    let lookup_name = crate::parsers::normalize_entity_name(&args.name);
65    let row: Option<(i64, EntityType)> = {
66        let mut stmt = conn
67            .prepare_cached("SELECT id, type FROM entities WHERE namespace = ?1 AND name = ?2")?;
68        match stmt.query_row(params![namespace, lookup_name], |r| {
69            Ok((r.get::<_, i64>(0)?, r.get::<_, EntityType>(1)?))
70        }) {
71            Ok(row) => Some(row),
72            Err(rusqlite::Error::QueryReturnedNoRows) => None,
73            Err(e) => return Err(AppError::Database(e)),
74        }
75    };
76    let (entity_id, entity_type) = row
77        .ok_or_else(|| AppError::NotFound(errors_msg::entity_not_found(&args.name, &namespace)))?;
78
79    // Validate the raw new name first (catches short ALL_CAPS NER noise),
80    // then normalize it for storage to preserve the normalized-name invariant.
81    entities::validate_entity_name(&args.new_name)?;
82    let new_name = crate::parsers::normalize_entity_name(&args.new_name);
83
84    if lookup_name == new_name {
85        return Err(AppError::Validation(
86            "source and target entity names are identical".to_string(),
87        ));
88    }
89
90    // Ensure new name is not already taken in this namespace.
91    if entities::find_entity_id(&conn, &namespace, &new_name)?.is_some() {
92        return Err(AppError::Validation(format!(
93            "entity with name '{new_name}' already exists in namespace '{namespace}'"
94        )));
95    }
96
97    let skip_embed = crate::embedder::should_skip_embedding_on_failure();
98    let embedding: Option<Vec<f32>> = match crate::embedder::embed_passage_with_choice(
99        &paths.models,
100        &new_name,
101        Some(llm_backend),
102    ) {
103        Ok((emb, _backend)) => Some(emb),
104        Err(AppError::Validation(msg)) => return Err(AppError::Validation(msg)),
105        Err(e) if skip_embed => {
106            tracing::warn!(error = %e, "rename-entity: embedding failed; --skip-embedding-on-failure active, persisting without embedding");
107            None
108        }
109        Err(e) => return Err(e),
110    };
111
112    let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
113    tx.execute(
114        "UPDATE entities SET name = ?1, updated_at = unixepoch() WHERE id = ?2",
115        params![new_name, entity_id],
116    )?;
117    // v1.0.76: BLOB-backed entity_embeddings table (PK = entity_id).
118    // G43: reuse the canonical writer instead of a duplicated INSERT that
119    // hardcoded dim=384 and a removed local model name; `upsert_entity_vec`
120    // records the real vector length and the CLI version as `model`.
121    if let Some(ref emb) = embedding {
122        entities::upsert_entity_vec(&tx, entity_id, &namespace, entity_type, emb, &new_name)?;
123    }
124    tx.commit()?;
125
126    conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
127
128    let response = RenameEntityResponse {
129        action: "renamed".to_string(),
130        old_name: args.name,
131        new_name,
132        entity_id,
133        namespace: namespace.clone(),
134        elapsed_ms: start.elapsed().as_millis() as u64,
135    };
136
137    match args.format {
138        OutputFormat::Json => output::emit_json(&response)?,
139        OutputFormat::Text | OutputFormat::Markdown => {
140            output::emit_text(&format!(
141                "renamed entity: '{}' → '{}' [{}]",
142                response.old_name, response.new_name, response.namespace
143            ));
144        }
145    }
146
147    Ok(())
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    #[test]
155    fn rename_entity_response_serializes_all_fields() {
156        let resp = RenameEntityResponse {
157            action: "renamed".to_string(),
158            old_name: "auth".to_string(),
159            new_name: "authentication".to_string(),
160            entity_id: 42,
161            namespace: "global".to_string(),
162            elapsed_ms: 7,
163        };
164        let json = serde_json::to_value(&resp).expect("serialization failed");
165        assert_eq!(json["action"], "renamed");
166        assert_eq!(json["old_name"], "auth");
167        assert_eq!(json["new_name"], "authentication");
168        assert_eq!(json["entity_id"], 42);
169        assert_eq!(json["namespace"], "global");
170        assert!(json["elapsed_ms"].is_number());
171    }
172
173    #[test]
174    fn rename_entity_response_action_is_renamed() {
175        let resp = RenameEntityResponse {
176            action: "renamed".to_string(),
177            old_name: "x".to_string(),
178            new_name: "y".to_string(),
179            entity_id: 1,
180            namespace: "ns".to_string(),
181            elapsed_ms: 1,
182        };
183        assert_eq!(resp.action, "renamed");
184    }
185
186    #[test]
187    fn rename_entity_response_entity_id_preserved() {
188        let resp = RenameEntityResponse {
189            action: "renamed".to_string(),
190            old_name: "old".to_string(),
191            new_name: "new".to_string(),
192            entity_id: 999,
193            namespace: "test-ns".to_string(),
194            elapsed_ms: 5,
195        };
196        let json = serde_json::to_value(&resp).expect("serialization failed");
197        assert_eq!(json["entity_id"], 999);
198    }
199
200    #[test]
201    fn rejects_rename_entity_to_same_name() {
202        use crate::errors::AppError;
203        let err = AppError::Validation("source and target entity names are identical".to_string());
204        assert_eq!(err.exit_code(), 1);
205        assert!(err.to_string().contains("identical"));
206    }
207
208    #[test]
209    fn rename_entity_response_namespace_reflected() {
210        let resp = RenameEntityResponse {
211            action: "renamed".to_string(),
212            old_name: "a".to_string(),
213            new_name: "b".to_string(),
214            entity_id: 10,
215            namespace: "my-project".to_string(),
216            elapsed_ms: 2,
217        };
218        let json = serde_json::to_value(&resp).expect("serialization failed");
219        assert_eq!(json["namespace"], "my-project");
220    }
221}