pub struct PendingEntry {
pub query: String,
pub params: Option<serde_json::Value>,
}
pub enum JournalCommand {
Write(PendingEntry),
Flush(std::sync::mpsc::SyncSender<()>),
Shutdown,
}
pub type JournalSender = std::sync::mpsc::Sender<JournalCommand>;
const MUTATION_KEYWORDS: &[&str] = &[
"CREATE", "MERGE", "DELETE", "DROP", "ALTER", "COPY", "SET",
"INSTALL", "UNINSTALL", "IMPORT", "ATTACH", "DETACH",
];
pub fn is_mutation(query: &str) -> bool {
let upper = query.to_ascii_uppercase();
let bytes = upper.as_bytes();
for keyword in MUTATION_KEYWORDS {
let kw_bytes = keyword.as_bytes();
let kw_len = kw_bytes.len();
let mut i = 0;
while i + kw_len <= bytes.len() {
if &bytes[i..i + kw_len] == kw_bytes {
let before_ok = i == 0 || !bytes[i - 1].is_ascii_alphanumeric();
let after_ok =
i + kw_len >= bytes.len() || !bytes[i + kw_len].is_ascii_alphanumeric();
if before_ok && after_ok {
return true;
}
}
i += 1;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mutation_keywords() {
assert!(is_mutation("CREATE (:Person {name: 'Alice'})"));
assert!(is_mutation("MERGE (n:Person)"));
assert!(is_mutation("DELETE n"));
assert!(is_mutation("DROP TABLE foo"));
assert!(is_mutation("ALTER TABLE foo"));
assert!(is_mutation("SET n.name = 'Bob'"));
assert!(is_mutation("COPY FROM 'file.csv'"));
}
#[test]
fn test_non_mutations() {
assert!(!is_mutation("MATCH (n) RETURN n"));
assert!(!is_mutation("RETURN 1 AS n"));
assert!(!is_mutation("EXPLAIN MATCH (n) RETURN n"));
}
#[test]
fn test_word_boundary() {
assert!(!is_mutation("MATCH (n:CREATED) RETURN n"));
assert!(is_mutation("CREATE NODE TABLE Foo(id INT64, PRIMARY KEY(id))"));
}
#[test]
fn test_is_mutation_install() {
assert!(is_mutation("INSTALL extension_name"));
assert!(is_mutation("install myext"));
assert!(is_mutation("Install SomeExtension"));
}
#[test]
fn test_is_mutation_uninstall() {
assert!(is_mutation("UNINSTALL extension_name"));
assert!(is_mutation("uninstall myext"));
}
#[test]
fn test_is_mutation_import() {
assert!(is_mutation("IMPORT DATABASE '/path/to/db'"));
assert!(is_mutation("import database '/tmp/backup'"));
}
#[test]
fn test_is_mutation_attach() {
assert!(is_mutation("ATTACH DATABASE '/path/to/other.db'"));
assert!(is_mutation("attach database '/tmp/other'"));
}
#[test]
fn test_is_mutation_detach() {
assert!(is_mutation("DETACH DATABASE other_db"));
assert!(is_mutation("detach database mydb"));
}
#[test]
fn test_uninstall_not_caught_by_install_boundary() {
assert!(is_mutation("UNINSTALL ext"));
}
#[test]
fn test_not_mutation_new_keywords_substring() {
assert!(!is_mutation("RETURN 'INSTALLED'"));
assert!(!is_mutation("RETURN 'IMPORTING'"));
assert!(!is_mutation("RETURN 'ATTACHED'"));
assert!(!is_mutation("RETURN 'DETACHED'"));
assert!(!is_mutation("RETURN 'DETACHING'"));
}
#[test]
fn test_not_mutation_new_keywords_prefixed() {
assert!(!is_mutation("RETURN 'REIMPORT'"));
assert!(!is_mutation("RETURN 'REATTACH'"));
}
#[test]
fn test_replica_safety_all_mutation_types() {
assert!(is_mutation("CREATE (:Person {name: 'Alice'})"));
assert!(is_mutation("CREATE NODE TABLE Person(name STRING, PRIMARY KEY(name))"));
assert!(is_mutation("CREATE REL TABLE KNOWS(FROM Person TO Person)"));
assert!(is_mutation("MATCH (a:Person), (b:Person) CREATE (a)-[:KNOWS]->(b)"));
assert!(is_mutation("MERGE (n:Person {name: 'Bob'})"));
assert!(is_mutation("MATCH (n:Person) SET n.age = 30"));
assert!(is_mutation("MATCH (n:Person) DELETE n"));
assert!(is_mutation("MATCH (n:Person) DETACH DELETE n"));
assert!(is_mutation("DROP TABLE Person"));
assert!(is_mutation("DROP TABLE IF EXISTS Person"));
assert!(is_mutation("ALTER TABLE Person ADD age INT64"));
assert!(is_mutation("ALTER TABLE Person DROP col"));
assert!(is_mutation("ALTER TABLE Person RENAME TO People"));
assert!(is_mutation("CREATE SEQUENCE my_seq"));
assert!(is_mutation("DROP SEQUENCE my_seq"));
assert!(is_mutation("CREATE TYPE my_type AS STRUCT(x INT64)"));
assert!(is_mutation("CREATE MACRO my_func(x) AS x + 1"));
assert!(is_mutation("DROP MACRO my_func"));
assert!(is_mutation("CREATE GRAPH my_graph"));
assert!(is_mutation("DROP GRAPH my_graph"));
assert!(is_mutation("COPY Person FROM 'people.csv'"));
assert!(is_mutation("COPY Person FROM 'people.parquet'"));
assert!(is_mutation("INSTALL httpfs"));
assert!(is_mutation("UNINSTALL httpfs"));
assert!(is_mutation("IMPORT DATABASE '/path/to/export'"));
assert!(is_mutation("ATTACH DATABASE '/other/db' AS other"));
assert!(is_mutation("DETACH DATABASE other"));
assert!(!is_mutation("MATCH (n:Person) RETURN n"));
assert!(!is_mutation("MATCH (n:Person) RETURN n.name ORDER BY n.name"));
assert!(!is_mutation("MATCH (n:Person) RETURN count(n)"));
assert!(!is_mutation("RETURN 1"));
assert!(!is_mutation("RETURN 'hello world'"));
assert!(!is_mutation("UNWIND [1, 2, 3] AS x RETURN x"));
assert!(!is_mutation("CALL current_setting('threads')"));
assert!(!is_mutation("EXPLAIN MATCH (n) RETURN n"));
assert!(!is_mutation("EXPORT DATABASE '/path/to/export'"));
assert!(!is_mutation("LOAD EXTENSION httpfs"));
assert!(!is_mutation("USE DATABASE other"));
assert!(!is_mutation("USE GRAPH my_graph"));
}
}