Initial commit: VRBattles API
This commit is contained in:
33
src/db/mod.rs
Normal file
33
src/db/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
/// Type alias for the database connection pool
|
||||
pub type DbPool = PgPool;
|
||||
|
||||
/// Initialize the database connection pool
|
||||
pub async fn init_pool(config: &Config) -> Result<DbPool, sqlx::Error> {
|
||||
tracing::info!("Connecting to database...");
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.min_connections(2)
|
||||
.acquire_timeout(std::time::Duration::from_secs(30))
|
||||
.connect(&config.database_url)
|
||||
.await?;
|
||||
|
||||
tracing::info!("Database connection established");
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
/// Run database migrations
|
||||
pub async fn run_migrations(pool: &DbPool) -> Result<(), sqlx::migrate::MigrateError> {
|
||||
tracing::info!("Running database migrations...");
|
||||
|
||||
sqlx::migrate!("./migrations").run(pool).await?;
|
||||
|
||||
tracing::info!("Database migrations complete");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user