sqlite_graphrag/commands/
merge_entities.rs1use crate::errors::AppError;
11use crate::i18n::errors_msg;
12use crate::output::{self, OutputFormat};
13use crate::paths::AppPaths;
14use crate::storage::connection::open_rw;
15use crate::storage::entities;
16use rusqlite::params;
17use serde::Serialize;
18
19#[derive(clap::Args)]
20#[command(after_long_help = "EXAMPLES:\n \
21 # Merge two source entities into a target\n \
22 sqlite-graphrag merge-entities --names auth,authentication --into auth-service\n\n \
23 # Merge three sources into one target across a namespace\n \
24 sqlite-graphrag merge-entities --names svc-a,svc-b,old-svc --into canonical-service --namespace my-project\n\n\
25NOTE:\n \
26 --names is a comma-separated list of source entity names.\n \
27 --into is the target entity name and must already exist.\n \
28 Source entities are deleted after the merge; the target is preserved.\n \
29 Duplicate relationships (same endpoints + relation) are removed automatically.\n \
30 Run `sqlite-graphrag cleanup-orphans` afterwards if sources had no other links.")]
31pub struct MergeEntitiesArgs {
32 #[arg(long, value_delimiter = ',', value_name = "NAMES")]
34 pub names: Vec<String>,
35 #[arg(long, value_name = "TARGET")]
37 pub into: String,
38 #[arg(long)]
39 pub namespace: Option<String>,
40 #[arg(long, value_enum, default_value = "json")]
41 pub format: OutputFormat,
42 #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
43 pub json: bool,
44 #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
45 pub db: Option<String>,
46}
47
48#[derive(Serialize)]
49struct MergeEntitiesResponse {
50 action: String,
51 sources: Vec<String>,
52 target: String,
53 namespace: String,
54 relationships_moved: usize,
55 entities_removed: usize,
56 elapsed_ms: u64,
58}
59
60pub fn run(args: MergeEntitiesArgs) -> Result<(), AppError> {
61 let inicio = std::time::Instant::now();
62
63 if args.names.is_empty() {
64 return Err(AppError::Validation(
65 "--names must contain at least one source entity name".to_string(),
66 ));
67 }
68
69 let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
70 let paths = AppPaths::resolve(args.db.as_deref())?;
71
72 crate::storage::connection::ensure_db_ready(&paths)?;
73
74 let mut conn = open_rw(&paths.db)?;
75
76 let target_id = entities::find_entity_id(&conn, &namespace, &args.into)?
78 .ok_or_else(|| AppError::NotFound(errors_msg::entity_not_found(&args.into, &namespace)))?;
79
80 let mut source_ids: Vec<i64> = Vec::with_capacity(args.names.len());
82 for name in &args.names {
83 if name == &args.into {
84 return Err(AppError::Validation(format!(
85 "source entity '{}' equals target '{}' — self-referential merge is not allowed",
86 name, args.into
87 )));
88 }
89 let id = entities::find_entity_id(&conn, &namespace, name)?
90 .ok_or_else(|| AppError::NotFound(errors_msg::entity_not_found(name, &namespace)))?;
91 if !source_ids.contains(&id) {
92 source_ids.push(id);
93 }
94 }
95
96 if source_ids.is_empty() {
97 return Err(AppError::Validation(
98 "no valid source entities to merge (all names equal the target or were duplicates)"
99 .to_string(),
100 ));
101 }
102
103 let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
104
105 let mut relationships_moved: usize = 0;
106
107 for &src_id in &source_ids {
108 let moved_src = tx.execute(
110 "UPDATE OR IGNORE relationships SET source_id = ?1 WHERE source_id = ?2",
111 params![target_id, src_id],
112 )?;
113 tx.execute(
114 "DELETE FROM relationships WHERE source_id = ?1",
115 params![src_id],
116 )?;
117 let moved_tgt = tx.execute(
119 "UPDATE OR IGNORE relationships SET target_id = ?1 WHERE target_id = ?2",
120 params![target_id, src_id],
121 )?;
122 tx.execute(
123 "DELETE FROM relationships WHERE target_id = ?1",
124 params![src_id],
125 )?;
126 relationships_moved += moved_src + moved_tgt;
127 }
128
129 tx.execute("DELETE FROM relationships WHERE source_id = target_id", [])?;
131
132 tx.execute(
135 "DELETE FROM relationships
136 WHERE id NOT IN (
137 SELECT MIN(id)
138 FROM relationships
139 GROUP BY source_id, target_id, relation
140 )",
141 [],
142 )?;
143
144 for &src_id in &source_ids {
149 tx.execute(
150 "UPDATE OR IGNORE memory_entities SET entity_id = ?1 WHERE entity_id = ?2",
151 params![target_id, src_id],
152 )?;
153 tx.execute(
154 "DELETE FROM memory_entities WHERE entity_id = ?1",
155 params![src_id],
156 )?;
157 }
158
159 tx.execute(
161 "DELETE FROM memory_entities
162 WHERE rowid NOT IN (
163 SELECT MIN(rowid)
164 FROM memory_entities
165 GROUP BY memory_id, entity_id
166 )",
167 [],
168 )?;
169
170 let mut entities_removed: usize = 0;
173 for &src_id in &source_ids {
174 let removed = tx.execute("DELETE FROM entities WHERE id = ?1", params![src_id])?;
175 entities_removed += removed;
176 }
177
178 let adjacent_ids: Vec<i64> = {
180 let mut stmt = tx.prepare(
181 "SELECT DISTINCT CASE WHEN source_id = ?1 THEN target_id ELSE source_id END
182 FROM relationships WHERE source_id = ?1 OR target_id = ?1",
183 )?;
184 let ids: Vec<i64> = stmt
185 .query_map(params![target_id], |r| r.get(0))?
186 .collect::<Result<Vec<_>, _>>()?;
187 ids
188 };
189 entities::recalculate_degree(&tx, target_id)?;
190 for &adj_id in &adjacent_ids {
191 entities::recalculate_degree(&tx, adj_id)?;
192 }
193
194 tx.commit()?;
195
196 conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
197
198 let processed_sources: Vec<String> = args
200 .names
201 .iter()
202 .filter(|n| n.as_str() != args.into.as_str())
203 .cloned()
204 .collect();
205
206 let response = MergeEntitiesResponse {
207 action: "merged".to_string(),
208 sources: processed_sources,
209 target: args.into.clone(),
210 namespace: namespace.clone(),
211 relationships_moved,
212 entities_removed,
213 elapsed_ms: inicio.elapsed().as_millis() as u64,
214 };
215
216 match args.format {
217 OutputFormat::Json => output::emit_json(&response)?,
218 OutputFormat::Text | OutputFormat::Markdown => {
219 output::emit_text(&format!(
220 "merged: {} sources into '{}' (relationships_moved={}, entities_removed={}) [{}]",
221 response.sources.len(),
222 response.target,
223 response.relationships_moved,
224 response.entities_removed,
225 response.namespace
226 ));
227 }
228 }
229
230 Ok(())
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236
237 #[test]
238 fn merge_entities_response_serializes_all_fields() {
239 let resp = MergeEntitiesResponse {
240 action: "merged".to_string(),
241 sources: vec!["auth".to_string(), "authentication".to_string()],
242 target: "auth-service".to_string(),
243 namespace: "global".to_string(),
244 relationships_moved: 7,
245 entities_removed: 2,
246 elapsed_ms: 15,
247 };
248 let json = serde_json::to_value(&resp).expect("serialization failed");
249 assert_eq!(json["action"], "merged");
250 assert_eq!(json["target"], "auth-service");
251 assert_eq!(json["namespace"], "global");
252 assert_eq!(json["relationships_moved"], 7);
253 assert_eq!(json["entities_removed"], 2);
254 let sources = json["sources"].as_array().expect("must be array");
255 assert_eq!(sources.len(), 2);
256 assert!(json["elapsed_ms"].is_number());
257 }
258
259 #[test]
260 fn merge_entities_response_action_is_merged() {
261 let resp = MergeEntitiesResponse {
262 action: "merged".to_string(),
263 sources: vec!["src".to_string()],
264 target: "tgt".to_string(),
265 namespace: "ns".to_string(),
266 relationships_moved: 0,
267 entities_removed: 1,
268 elapsed_ms: 0,
269 };
270 assert_eq!(resp.action, "merged");
271 }
272
273 #[test]
274 fn merge_entities_response_empty_sources_serializes() {
275 let resp = MergeEntitiesResponse {
276 action: "merged".to_string(),
277 sources: vec![],
278 target: "target".to_string(),
279 namespace: "global".to_string(),
280 relationships_moved: 0,
281 entities_removed: 0,
282 elapsed_ms: 1,
283 };
284 let json = serde_json::to_value(&resp).expect("serialization failed");
285 let sources = json["sources"].as_array().expect("must be array");
286 assert_eq!(sources.len(), 0);
287 }
288
289 #[test]
290 fn merge_entities_response_with_zero_relationships_moved() {
291 let resp = MergeEntitiesResponse {
292 action: "merged".to_string(),
293 sources: vec!["src-a".to_string()],
294 target: "tgt".to_string(),
295 namespace: "global".to_string(),
296 relationships_moved: 0,
297 entities_removed: 1,
298 elapsed_ms: 5,
299 };
300 let json = serde_json::to_value(&resp).expect("serialization failed");
301 assert_eq!(json["relationships_moved"], 0);
302 assert_eq!(json["entities_removed"], 1);
303 }
304
305 #[test]
306 fn merge_entities_response_multiple_sources() {
307 let resp = MergeEntitiesResponse {
308 action: "merged".to_string(),
309 sources: vec!["a".into(), "b".into(), "c".into()],
310 target: "canonical".to_string(),
311 namespace: "proj".to_string(),
312 relationships_moved: 12,
313 entities_removed: 3,
314 elapsed_ms: 42,
315 };
316 let json = serde_json::to_value(&resp).expect("serialization failed");
317 assert_eq!(json["entities_removed"], 3);
318 let sources = json["sources"].as_array().unwrap();
319 assert_eq!(sources.len(), 3);
320 }
321}