use anyhow::Result; use sqlx::{ sqlite::{SqliteConnectOptions, SqlitePoolOptions}, SqlitePool, }; use std::str::FromStr; use tracing::info; pub type Pool = SqlitePool; pub async fn init_pool(database_url: &str) -> Result { info!("Connecting to database: {}", database_url); let opts = SqliteConnectOptions::from_str(database_url)?.create_if_missing(true); let pool = SqlitePoolOptions::new() .max_connections(5) .connect_with(opts) .await?; sqlx::query("PRAGMA journal_mode=WAL") .execute(&pool) .await?; sqlx::query("PRAGMA foreign_keys=ON").execute(&pool).await?; Ok(pool) } pub async fn run_migrations(pool: &Pool) -> Result<()> { info!("Running database migrations"); sqlx::migrate!("./migrations").run(pool).await?; Ok(()) }