Really made imports drop the db
parent
b15b1f23fd
commit
19101caccc
|
@ -102,7 +102,7 @@ checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "j_db"
|
name = "j_db"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"json",
|
"json",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "j_db"
|
name = "j_db"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
authors = ["Joey Hines <joey@ahines.net>"]
|
authors = ["Joey Hines <joey@ahines.net>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A good enough (TM) embedded Rust DB* and ORM*"
|
description = "A good enough (TM) embedded Rust DB* and ORM*"
|
||||||
|
|
|
@ -217,7 +217,11 @@ impl Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn import_db(&self, json: JsonValue) -> Result<()> {
|
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.clear()?;
|
||||||
|
self.db.flush()?;
|
||||||
for model in json["global"].members() {
|
for model in json["global"].members() {
|
||||||
let id_bytes = model["id"].as_u64().unwrap().to_be_bytes();
|
let id_bytes = model["id"].as_u64().unwrap().to_be_bytes();
|
||||||
self.db.insert(id_bytes, model.to_string().as_bytes())?;
|
self.db.insert(id_bytes, model.to_string().as_bytes())?;
|
||||||
|
@ -245,6 +249,7 @@ mod tests {
|
||||||
use crate::model::JdbModel;
|
use crate::model::JdbModel;
|
||||||
use crate::test::{cleanup, User, DB, LOCK};
|
use crate::test::{cleanup, User, DB, LOCK};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use json::JsonValue;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_insert() {
|
fn test_insert() {
|
||||||
|
@ -371,4 +376,26 @@ mod tests {
|
||||||
|
|
||||||
cleanup();
|
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