huellas/src/db.rs
Felipe Contreras Salinas 78cb376791
chore: use native HTML5 dialog element and refactor backend into hexagonal architecture. (#48)
Reviewed-on: #48
Co-authored-by: Felipe Contreras Salinas <felipe@bstr.cl>
Co-committed-by: Felipe Contreras Salinas <felipe@bstr.cl>
2025-06-11 22:46:58 -04:00

26 lines
777 B
Rust

//! 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(())
}