Skip to content

Commit

Permalink
⚡️ Update diesel_setup.rs to use Result type for setup_database function
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikEight committed May 28, 2024
1 parent 35f5733 commit 4fdb84a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src-modules/eam_commons/src/diesel_setup.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use diesel::sqlite::SqliteConnection;
use diesel::r2d2::{self, ConnectionManager};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use diesel::r2d2::{Pool, PooledConnection};
use std::error::Error;

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

pub type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
pub type DbConn = PooledConnection<ConnectionManager<SqliteConnection>>;

pub fn setup_database(database_url: &str) -> DbPool {
pub fn setup_database(database_url: &str) -> Result<DbPool, Box<dyn Error + Send + Sync>> {
std::env::set_var("DATABASE_URL", database_url);
let manager = ConnectionManager::<SqliteConnection>::new(database_url);
let pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
.build(manager)?;

let mut conn = pool.get().expect("Failed to get connection from pool.");
let mut conn = pool.get()?;

conn.run_pending_migrations(MIGRATIONS).expect("Failed to run migrations");
conn.run_pending_migrations(MIGRATIONS)?;

Ok(pool)
}

pool
}

0 comments on commit 4fdb84a

Please sign in to comment.