use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
use sqlx::PgPool;
use crate::error::{Result, StoreError};
pub static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations");
pub async fn connect(database_url: &str) -> Result<PgPool> {
let opts: PgConnectOptions = database_url
.parse()
.map_err(|e: sqlx::Error| StoreError::Database(e.to_string()))?;
let pool = PgPoolOptions::new()
.max_connections(16)
.acquire_timeout(std::time::Duration::from_secs(5))
.connect_with(opts)
.await
.map_err(StoreError::from)?;
Ok(pool)
}
pub async fn run_migrations(pool: &PgPool) -> Result<()> {
MIGRATOR
.run(pool)
.await
.map_err(|e| StoreError::Migration(e.to_string()))
}