feat: add error handling for database initialization
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use crate::*;
|
use crate::*;
|
||||||
use rusqlite::*;
|
use rusqlite::*;
|
||||||
|
|
||||||
pub fn run(path: &path::Path) {
|
pub fn run(path: &path::Path) -> result::Result<(), FailedTo> {
|
||||||
let init_statement = r#"
|
let init_statement = r#"
|
||||||
CREATE TABLE IF NOT EXISTS entity (
|
CREATE TABLE IF NOT EXISTS entity (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
@@ -22,11 +22,11 @@ pub fn run(path: &path::Path) {
|
|||||||
CREATE INDEX IF NOT EXISTS entity_class ON entity (class);
|
CREATE INDEX IF NOT EXISTS entity_class ON entity (class);
|
||||||
CREATE INDEX IF NOT EXISTS attribute_id ON attribute (id);
|
CREATE INDEX IF NOT EXISTS attribute_id ON attribute (id);
|
||||||
"#;
|
"#;
|
||||||
let connection = Connection::open(path).unwrap();
|
let connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||||
let _result = connection.execute_batch(init_statement);
|
connection
|
||||||
if _result.is_err() {
|
.execute_batch(init_statement)
|
||||||
panic!();
|
.map_err(|_| FailedTo::ExecuteSQLiteBatch)?;
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -36,6 +36,7 @@ mod unit_tests {
|
|||||||
fn test_call() {
|
fn test_call() {
|
||||||
let tempfile = tempfile::NamedTempFile::new().unwrap();
|
let tempfile = tempfile::NamedTempFile::new().unwrap();
|
||||||
let path = tempfile.path();
|
let path = tempfile.path();
|
||||||
run(path);
|
let result = run(path);
|
||||||
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ pub use crate::str::attribute::O as Attribute;
|
|||||||
pub use crate::str::catalog::O as Catalog;
|
pub use crate::str::catalog::O as Catalog;
|
||||||
pub use crate::str::entity::O as Entity;
|
pub use crate::str::entity::O as Entity;
|
||||||
pub use crate::str::entity_state::E as EntityState;
|
pub use crate::str::entity_state::E as EntityState;
|
||||||
|
pub use crate::str::failed_to::E as FailedTo;
|
||||||
pub use crate::str::value::E as Value;
|
pub use crate::str::value::E as Value;
|
||||||
pub use crate::trt::eav::T as EAV;
|
pub use crate::trt::eav::T as EAV;
|
||||||
|
|
||||||
|
|||||||
@@ -24,9 +24,10 @@ impl O {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Initializes the database.
|
/// Initializes the database.
|
||||||
pub fn init(&self) {
|
pub fn init(&self) -> result::Result<(), FailedTo> {
|
||||||
let path = path::Path::new(&self.path);
|
let path = path::Path::new(&self.path);
|
||||||
sqlite::init::db(path);
|
sqlite::init::db(path).map_err(|_| FailedTo::InitDatabase)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts a single object that implements the `EAV` trait into the catalog.
|
/// Inserts a single object that implements the `EAV` trait into the catalog.
|
||||||
|
|||||||
6
01.workspace/heave/src/str/failed_to.rs
Normal file
6
01.workspace/heave/src/str/failed_to.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||||
|
pub enum E {
|
||||||
|
ExecuteSQLiteBatch,
|
||||||
|
InitDatabase,
|
||||||
|
OpenSQLiteConnection,
|
||||||
|
}
|
||||||
@@ -2,4 +2,5 @@ pub mod attribute;
|
|||||||
pub mod catalog;
|
pub mod catalog;
|
||||||
pub mod entity;
|
pub mod entity;
|
||||||
pub mod entity_state;
|
pub mod entity_state;
|
||||||
|
pub mod failed_to;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|||||||
Reference in New Issue
Block a user