captains-log/backend/src/database/connection.rs
Drew Galbraith d32f6be813 Allow creating and updating tasks on the backend. (#2)
Create hurl tests along with the necessary api code to resolve them.

Reviewed-on: #2
Co-authored-by: Drew Galbraith <drew@tiramisu.one>
Co-committed-by: Drew Galbraith <drew@tiramisu.one>
2025-09-20 18:49:11 +00:00

28 lines
752 B
Rust

use anyhow::Result;
use sqlx::{SqlitePool, sqlite::SqliteConnectOptions};
use std::str::FromStr;
pub struct DatabaseConfig {
pub database_url: String,
}
pub async fn create_pool(config: &DatabaseConfig) -> Result<SqlitePool> {
let options = SqliteConnectOptions::from_str(&config.database_url)?.create_if_missing(true);
let pool = SqlitePool::connect_with(options).await?;
sqlx::migrate!("./migrations").run(&pool).await?;
Ok(pool)
}
#[cfg(test)]
pub async fn create_test_pool() -> Result<SqlitePool> {
let options = SqliteConnectOptions::from_str("sqlite::memory:")?.create_if_missing(true);
let pool = SqlitePool::connect_with(options).await?;
sqlx::migrate!("./migrations").run(&pool).await?;
Ok(pool)
}