Skip to content

Commit 33e36f1

Browse files
authored
When deleting nodes, make sure there's no relationships. (#325)
1 parent f945ba3 commit 33e36f1

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/ops/storages/neo4j.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ impl ExportContext {
396396
RowMappingSpec::Node(node_spec) => {
397397
let delete_cypher = formatdoc! {"
398398
OPTIONAL MATCH (old_node:{label} {key_fields_literal})
399+
WITH old_node
400+
WHERE NOT (old_node)--()
399401
DELETE old_node
400402
FINISH
401403
",
@@ -894,11 +896,17 @@ impl components::Operator for SetupComponentOperator {
894896
metric,
895897
vector_size,
896898
} => {
897-
format!(
898-
r#"CREATE VECTOR INDEX {name} IF NOT EXISTS FOR {matcher} ON {qualifier}.{field_name} OPTIONS
899-
{{ indexConfig: {{`vector.dimensions`: {vector_size}, `vector.similarity_function`: '{metric}'}}}}"#,
899+
formatdoc! {"
900+
CREATE VECTOR INDEX {name} IF NOT EXISTS
901+
FOR {matcher} ON {qualifier}.{field_name}
902+
OPTIONS {{
903+
indexConfig: {{
904+
`vector.dimensions`: {vector_size},
905+
`vector.similarity_function`: '{metric}'
906+
}}
907+
}}",
900908
name = key.name,
901-
)
909+
}
902910
}
903911
});
904912
Ok(graph.run(query).await?)
@@ -1013,30 +1021,33 @@ impl ResourceSetupStatusCheck for SetupStatusCheck {
10131021
async fn apply_change(&self) -> Result<()> {
10141022
let graph = self.graph_pool.get_graph(&self.conn_spec).await?;
10151023
if let Some(data_clear) = &self.data_clear {
1016-
let delete_rel_query = neo4rs::query(&format!(
1017-
r#"
1024+
let delete_rel_query = neo4rs::query(&formatdoc! {"
10181025
CALL {{
1019-
MATCH {matcher}
1020-
WITH {var_name}
1021-
DELETE {var_name}
1026+
MATCH {matcher}
1027+
WITH {var_name}
1028+
{optional_orphan_condition}
1029+
DELETE {var_name}
10221030
}} IN TRANSACTIONS
1023-
"#,
1031+
",
10241032
matcher = data_clear.core_elem_type.matcher(CORE_ELEMENT_MATCHER_VAR),
10251033
var_name = CORE_ELEMENT_MATCHER_VAR,
1026-
));
1034+
optional_orphan_condition = match data_clear.core_elem_type {
1035+
ElementType::Node(_) => format!("WHERE NOT ({CORE_ELEMENT_MATCHER_VAR})--()"),
1036+
_ => "".to_string(),
1037+
},
1038+
});
10271039
graph.run(delete_rel_query).await?;
10281040

10291041
for node_label in &data_clear.dependent_node_labels {
1030-
let delete_node_query = neo4rs::query(&format!(
1031-
r#"
1042+
let delete_node_query = neo4rs::query(&formatdoc! {"
10321043
CALL {{
1033-
MATCH (n:{node_label})
1034-
WHERE NOT (n)--()
1035-
DELETE n
1044+
MATCH (n:{node_label})
1045+
WHERE NOT (n)--()
1046+
DELETE n
10361047
}} IN TRANSACTIONS
1037-
"#,
1048+
",
10381049
node_label = node_label
1039-
));
1050+
});
10401051
graph.run(delete_node_query).await?;
10411052
}
10421053
}

0 commit comments

Comments
 (0)