| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use anyhow::Result; |
| 9 |
use sqlx::SqlitePool; |
| 10 |
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous}; |
| 11 |
use std::path::Path; |
| 12 |
use std::str::FromStr; |
| 13 |
use std::time::Duration; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub async fn connect(path: &Path) -> Result<SqlitePool> { |
| 23 |
let url = format!("sqlite://{}?mode=rwc", path.display()); |
| 24 |
let opts = SqliteConnectOptions::from_str(&url)? |
| 25 |
.create_if_missing(true) |
| 26 |
.foreign_keys(true) |
| 27 |
.journal_mode(SqliteJournalMode::Wal) |
| 28 |
.synchronous(SqliteSynchronous::Normal) |
| 29 |
.busy_timeout(Duration::from_secs(5)); |
| 30 |
let pool = SqlitePoolOptions::new() |
| 31 |
.max_connections(4) |
| 32 |
.connect_with(opts) |
| 33 |
.await?; |
| 34 |
Ok(pool) |
| 35 |
} |
| 36 |
|
| 37 |
#[cfg(test)] |
| 38 |
mod tests { |
| 39 |
use super::*; |
| 40 |
|
| 41 |
#[tokio::test] |
| 42 |
async fn connect_creates_and_enables_foreign_keys() { |
| 43 |
let dir = tempfile::tempdir().unwrap(); |
| 44 |
let pool = connect(&dir.path().join("t.db")).await.unwrap(); |
| 45 |
let fk: i64 = sqlx::query_scalar("PRAGMA foreign_keys") |
| 46 |
.fetch_one(&pool) |
| 47 |
.await |
| 48 |
.unwrap(); |
| 49 |
assert_eq!(fk, 1); |
| 50 |
} |
| 51 |
} |
| 52 |
|