Adding code to use test database when in debug mode #10
18
src/lib.rs
18
src/lib.rs
@@ -8,6 +8,13 @@ mod keys {
|
||||
pub mod error {
|
||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||
}
|
||||
|
||||
pub mod test {
|
||||
pub const DBURL: &str = "TEST_DATABASE_URL";
|
||||
pub mod error {
|
||||
pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod connection_settings {
|
||||
@@ -23,11 +30,20 @@ pub mod db_pool {
|
||||
|
||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
dotenv::dotenv().ok();
|
||||
let database_url = env::var(keys::DBURL).expect(keys::error::ERROR);
|
||||
let database_url = get_db_url().await;
|
||||
println!("Database url: {:?}", database_url);
|
||||
|
||||
PgPoolOptions::new()
|
||||
.max_connections(connection_settings::MAXCONN)
|
||||
.connect(&database_url)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_db_url() -> String {
|
||||
if cfg!(debug_assertions) {
|
||||
env::var(keys::test::DBURL).expect(keys::test::error::ERROR)
|
||||
} else {
|
||||
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user