From affef5eaafb3b096aef4cb34b9e5563c6b513c9e Mon Sep 17 00:00:00 2001 From: Glauber Costa Date: Tue, 25 Apr 2023 20:05:56 -0400 Subject: [PATCH] parse multiline when loading dump SQLite dumps are often multi-line, so accumulate on a buffer and only send when the line ends with ; Fixes #354 --- sqld/src/database/dump/loader.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/sqld/src/database/dump/loader.rs b/sqld/src/database/dump/loader.rs index 4d52bf78..aac1d00f 100644 --- a/sqld/src/database/dump/loader.rs +++ b/sqld/src/database/dump/loader.rs @@ -68,23 +68,37 @@ const WASM_TABLE_CREATE: &str = fn perform_load_dump(conn: &rusqlite::Connection, path: PathBuf) -> anyhow::Result<()> { let mut f = BufReader::new(File::open(path)?); + let mut curr = String::new(); let mut line = String::new(); let mut skipped_wasm_table = false; - while let Ok(n) = f.read_line(&mut line) { + while let Ok(n) = f.read_line(&mut curr) { if n == 0 { break; } + let frag = curr.trim(); + + if frag.is_empty() || frag.starts_with("--") { + curr.clear(); + continue; + } + + line.push_str(frag); + curr.clear(); // This is a hack to ignore the libsql_wasm_func_table table because it is already created // by the system. - if !skipped_wasm_table && line.trim() == WASM_TABLE_CREATE { + if !skipped_wasm_table && line == WASM_TABLE_CREATE { skipped_wasm_table = true; line.clear(); continue; } - conn.execute(&line, ())?; - line.clear(); + if line.ends_with(';') { + conn.execute(&line, ())?; + line.clear(); + } else { + line.push(' '); + } } Ok(())