-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Update diesel_setup.rs to use Result type for setup_database function
- Loading branch information
Showing
1 changed file
with
10 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |