Reviewed-on: #48 Co-authored-by: Felipe Contreras Salinas <felipe@bstr.cl> Co-committed-by: Felipe Contreras Salinas <felipe@bstr.cl>
26 lines
777 B
Rust
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(())
|
|
}
|