return a struct on database open

feat/vaults
Tomáš Mládek 2020-08-30 17:13:18 +02:00
parent bed9d953d2
commit ec0f4f5ec6
2 changed files with 14 additions and 8 deletions

View File

@ -6,8 +6,7 @@ use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager};
use diesel::sqlite::SqliteConnection;
use log::debug;
use std::error::Error;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time::Duration;
pub type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
@ -144,10 +143,16 @@ impl std::io::Write for LoggerSink {
}
}
pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<DbPool, Box<dyn Error>> {
pub struct OpenResult {
pub pool: DbPool,
pub new: bool,
}
pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<OpenResult> {
embed_migrations!("./migrations/upend/");
let database_path = dirpath.as_ref().join("upend.sqlite3");
let database_path: PathBuf = dirpath.as_ref().join("upend.sqlite3");
let new = database_path.exists();
let manager = ConnectionManager::<SqliteConnection>::new(database_path.to_str().unwrap());
let pool = r2d2::Pool::builder()
@ -164,7 +169,8 @@ pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<DbPool, Box<dyn Error>>
..Default::default()
},
)?;
Ok(pool)
Ok(OpenResult { pool, new })
}
// extern crate xdg;

View File

@ -50,12 +50,12 @@ fn main() -> std::io::Result<()> {
let path = PathBuf::from(dirname);
let _ = fs::remove_file(format!("{}/upend.sqlite3", dirname)); // TODO REMOVE!!!
let db_pool = database::open_upend(&dirname).expect("failed to open database!");
let open_result = database::open_upend(&dirname).expect("failed to open database!");
let sys = actix::System::new("upend");
// let connection = db_pool.get().expect("Could not get SQL connection.");
let db_addr = SyncArbiter::start(3, move || database::DbExecutor(db_pool.clone()));
let pool = open_result.pool;
let db_addr = SyncArbiter::start(3, move || database::DbExecutor(pool.clone()));
let hash_addr = SyncArbiter::start(4, move || dataops::HasherWorker);
let bind: SocketAddr = matches