huellas/src/db.rs

27 lines
777 B
Rust
Raw Normal View History

//! Database handling.
use anyhow::{Context, Result};
use sqlx::SqlitePool;
/// Creates a Database Pool
///
/// # Errors
/// This function may return an error if the `DATABASE_URL` environment is not defined or if the
/// database that URL points to is not reachable for some reason.
pub async fn pool() -> Result<SqlitePool> {
let db_url = std::env::var("DATABASE_URL").context("DATABASE_URL not defined")?;
let pool = SqlitePool::connect(&db_url)
.await
.context("Couldn't connect to database")?;
Ok(pool)
}
/// Run migrations on the database `pool` is connected to.
pub async fn run_migrations(pool: &SqlitePool) -> Result<()> {
sqlx::migrate!()
.run(pool)
.await
.context("Couldn't run migrations")?;
Ok(())
}