Replace expect usage with Result
This commit is contained in:
parent
9394a1ae52
commit
b3228ea6b5
11 changed files with 135 additions and 95 deletions
emgauwa-lib/src/db
|
@ -19,38 +19,31 @@ pub use relays::DbRelay;
|
|||
pub use schedules::{DbPeriods, DbSchedule};
|
||||
pub use tag::DbTag;
|
||||
|
||||
use crate::errors::{DatabaseError, EmgauwaError};
|
||||
|
||||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
|
||||
|
||||
pub async fn run_migrations(pool: &Pool<Sqlite>) {
|
||||
pub async fn run_migrations(pool: &Pool<Sqlite>) -> Result<(), EmgauwaError> {
|
||||
log::info!("Running migrations");
|
||||
MIGRATOR.run(pool).await.expect("Failed to run migrations.");
|
||||
MIGRATOR.run(pool).await.map_err(DatabaseError::from)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn init(db: &str) -> Pool<Sqlite> {
|
||||
let options = SqliteConnectOptions::from_str(db)
|
||||
.expect("Error parsing database path")
|
||||
.create_if_missing(true);
|
||||
pub async fn init(db: &str) -> Result<Pool<Sqlite>, EmgauwaError> {
|
||||
let options = SqliteConnectOptions::from_str(db)?.create_if_missing(true);
|
||||
|
||||
let pool: Pool<Sqlite> = SqlitePoolOptions::new()
|
||||
.acquire_timeout(std::time::Duration::from_secs(1))
|
||||
.max_connections(5)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.expect("Error connecting to database");
|
||||
.await?;
|
||||
|
||||
run_migrations(&pool).await;
|
||||
run_migrations(&pool).await?;
|
||||
|
||||
let mut pool_conn = pool
|
||||
.acquire()
|
||||
.await
|
||||
.expect("Failed to acquire pool connection");
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
DbSchedule::get_on(&mut pool_conn)
|
||||
.await
|
||||
.expect("Failed to init 'on' schedule");
|
||||
DbSchedule::get_off(&mut pool_conn)
|
||||
.await
|
||||
.expect("Failed to init 'off' schedule");
|
||||
DbSchedule::get_on(&mut pool_conn).await?;
|
||||
DbSchedule::get_off(&mut pool_conn).await?;
|
||||
|
||||
pool
|
||||
Ok(pool)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue