Run SQLx migrations on startup

This commit is contained in:
2026-01-16 10:39:51 +01:00
parent fc2c8b3452
commit 26b23948ec
7 changed files with 99 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
use crate::config::CONFIG;
use sqlx::{postgres::PgPoolOptions, PgPool};
use tracing::info;
pub async fn get_postgres_pool() -> PgPool {
let database_url: String = format!(
@@ -12,10 +13,20 @@ pub async fn get_postgres_pool() -> PgPool {
CONFIG.postgres_db
);
PgPoolOptions::new()
let pool = PgPoolOptions::new()
.max_connections(10)
.acquire_timeout(std::time::Duration::from_secs(300))
.connect(&database_url)
.await
.unwrap()
.unwrap();
// Run migrations
info!("Running database migrations...");
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("Failed to run migrations");
info!("Database migrations completed successfully");
pool
}