11a811db6a
CI / Build, lint & test (push) Failing after 1m20s
Previously the binary failed with "unable to open database file" (SQLite code 14) when no openfut.db existed yet. Using SqliteConnectOptions with create_if_missing(true) tells SQLite to create the file if absent. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
30 lines
823 B
Rust
30 lines
823 B
Rust
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<Pool> {
|
|
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(())
|
|
}
|