Skip to main content

max / makenotwork

1020 B · 29 lines History Blame Raw
1 use anyhow::Result;
2 use sqlx::SqlitePool;
3 use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
4 use std::path::Path;
5 use std::str::FromStr;
6 use std::time::Duration;
7
8 pub async fn connect(path: &Path) -> Result<SqlitePool> {
9 let url = format!("sqlite://{}?mode=rwc", path.display());
10 let opts = SqliteConnectOptions::from_str(&url)?
11 .create_if_missing(true)
12 .foreign_keys(true)
13 // WAL lets readers run concurrently with a writer; busy_timeout makes a
14 // second writer wait for the lock instead of erroring out as SQLITE_BUSY
15 // (which surfaced as a 500 to the operator mid-deploy — ultra-fuzz Run 2).
16 .journal_mode(SqliteJournalMode::Wal)
17 .busy_timeout(Duration::from_secs(5));
18 let pool = SqlitePoolOptions::new()
19 .max_connections(4)
20 .connect_with(opts)
21 .await?;
22 Ok(pool)
23 }
24
25 pub async fn migrate(pool: &SqlitePool) -> Result<()> {
26 sqlx::migrate!("./migrations").run(pool).await?;
27 Ok(())
28 }
29