use std::fs;
use gen_core::errors::ConnectionError;
use gen_models::{
db::{ConfigConnection, GraphConnection},
migrations::{run_config_migrations, run_migrations},
};
use rusqlite::Connection;
pub fn get_connection<'a>(
db_path: impl Into<Option<&'a str>>,
) -> Result<GraphConnection, ConnectionError> {
let path: Option<&str> = db_path.into();
let mut conn;
if let Some(v) = path {
if fs::metadata(v).is_ok() {
fs::remove_file(v).expect("Unable to remove database entry.");
}
conn = Connection::open(v).map_err(ConnectionError::OpenFailed)?;
} else {
conn = Connection::open_in_memory().map_err(ConnectionError::OpenFailed)?;
}
rusqlite::vtab::array::load_module(&conn)?;
run_migrations(&mut conn);
Ok(GraphConnection(conn))
}
pub fn get_config_connection<'a>(
db_path: impl Into<Option<&'a str>>,
) -> Result<ConfigConnection, ConnectionError> {
let path: Option<&str> = db_path.into();
let mut conn;
if let Some(v) = path {
if fs::metadata(v).is_ok() {
fs::remove_file(v).expect("Unable to remove database entry.");
}
conn = Connection::open(v).map_err(ConnectionError::OpenFailed)?;
} else {
conn = Connection::open_in_memory().map_err(ConnectionError::OpenFailed)?;
}
rusqlite::vtab::array::load_module(&conn)?;
run_config_migrations(&mut conn);
Ok(ConfigConnection(conn))
}