Really made imports drop the db
parent
b15b1f23fd
commit
19101caccc
|
@ -102,7 +102,7 @@ checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
|||
|
||||
[[package]]
|
||||
name = "j_db"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"json",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "j_db"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
authors = ["Joey Hines <joey@ahines.net>"]
|
||||
edition = "2021"
|
||||
description = "A good enough (TM) embedded Rust DB* and ORM*"
|
||||
|
|
|
@ -217,7 +217,11 @@ impl Database {
|
|||
}
|
||||
|
||||
pub fn import_db(&self, json: JsonValue) -> Result<()> {
|
||||
for tree in self.db.tree_names() {
|
||||
self.db.open_tree(tree)?.clear()?;
|
||||
}
|
||||
self.db.clear()?;
|
||||
self.db.flush()?;
|
||||
for model in json["global"].members() {
|
||||
let id_bytes = model["id"].as_u64().unwrap().to_be_bytes();
|
||||
self.db.insert(id_bytes, model.to_string().as_bytes())?;
|
||||
|
@ -245,6 +249,7 @@ mod tests {
|
|||
use crate::model::JdbModel;
|
||||
use crate::test::{cleanup, User, DB, LOCK};
|
||||
use std::time::Instant;
|
||||
use json::JsonValue;
|
||||
|
||||
#[test]
|
||||
fn test_insert() {
|
||||
|
@ -371,4 +376,26 @@ mod tests {
|
|||
|
||||
cleanup();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_import_clear() {
|
||||
let _lock = LOCK.lock().unwrap();
|
||||
cleanup();
|
||||
|
||||
let mut users = vec![];
|
||||
for i in 0..10 {
|
||||
let user = User::new(&format!("User{}", i), 0);
|
||||
|
||||
let u = DB.insert::<User>(user).unwrap();
|
||||
|
||||
users.push(u);
|
||||
}
|
||||
|
||||
DB.import_db(JsonValue::Null).unwrap();
|
||||
|
||||
let count = DB.filter(|_, _user: &User| true).unwrap().count();
|
||||
assert_eq!(count, 0);
|
||||
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue