Moved environement related code to its own module
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
pub async fn get_db_url() -> String {
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
std::env::var(crate::keys::DBURL).expect(crate::keys::error::ERROR)
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
pub const DBURL: &str = "DATABASE_URL";
|
pub const DBURL: &str = "DATABASE_URL";
|
||||||
|
pub const ROOT_DIRECTORY: &str = "ROOT_DIRECTORY";
|
||||||
|
|
||||||
pub mod error {
|
pub mod error {
|
||||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||||
|
|||||||
+7
-5
@@ -1,10 +1,10 @@
|
|||||||
pub mod callers;
|
pub mod callers;
|
||||||
|
pub mod environment;
|
||||||
pub mod keys;
|
pub mod keys;
|
||||||
|
|
||||||
pub mod db {
|
pub mod db {
|
||||||
|
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use std::env;
|
|
||||||
|
|
||||||
use crate::keys;
|
use crate::keys;
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ pub mod db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
let database_url = get_db_url().await;
|
let database_url = crate::environment::get_db_url().await;
|
||||||
println!("Database url: {:?}", database_url);
|
println!("Database url: {:?}", database_url);
|
||||||
|
|
||||||
PgPoolOptions::new()
|
PgPoolOptions::new()
|
||||||
@@ -22,10 +22,12 @@ pub mod db {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
async fn get_db_url() -> String {
|
async fn get_db_url() -> String {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
env::var(keys::DBURL).expect(keys::error::ERROR)
|
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
pub async fn migrations(pool: &sqlx::PgPool) {
|
pub async fn migrations(pool: &sqlx::PgPool) {
|
||||||
// Run migrations using the sqlx::migrate! macro
|
// Run migrations using the sqlx::migrate! macro
|
||||||
@@ -158,7 +160,8 @@ mod tests {
|
|||||||
|
|
||||||
pub async fn get_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn get_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
let tm_db_url = std::env::var(keys::DBURL).expect("DATABASE_URL must be present");
|
// let tm_db_url = std::env::var(keys::DBURL).expect("DATABASE_URL must be present");
|
||||||
|
let tm_db_url = crate::environment::get_db_url().await;
|
||||||
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
||||||
sqlx::PgPool::connect_with(tm_options).await
|
sqlx::PgPool::connect_with(tm_options).await
|
||||||
}
|
}
|
||||||
@@ -171,8 +174,7 @@ mod tests {
|
|||||||
|
|
||||||
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
let db_url =
|
let db_url = crate::environment::get_db_url().await;
|
||||||
std::env::var(keys::DBURL).expect("DATABASE_URL must be set for tests");
|
|
||||||
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||||
sqlx::PgPool::connect_with(options).await
|
sqlx::PgPool::connect_with(options).await
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user