All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 9m41s
20 lines
529 B
Rust
20 lines
529 B
Rust
use sqlx::{Pool, Postgres, migrate::MigrateError, postgres::PgPoolOptions};
|
|
|
|
use crate::errors::AppError;
|
|
|
|
pub async fn init(db_url: &str) -> Result<Pool<Postgres>, AppError> {
|
|
let db = PgPoolOptions::new()
|
|
.connect(db_url)
|
|
.await
|
|
.map_err(AppError::DbConnect)?;
|
|
|
|
sqlx::migrate!("./migrations")
|
|
.run(&db)
|
|
.await
|
|
.map_err(|e: MigrateError| AppError::InvalidConfig(format!("Migration failed: {}", e)))?;
|
|
|
|
tracing::info!("Migration completed successfully");
|
|
|
|
Ok(db)
|
|
}
|