ORM-05: Add the Config builder #5

Closed
opened 2026-10-13 12:49:38 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #1, #2

Context

Database settings (where it lives, foreign keys, journal mode, busy timeout) need one place. SQLite pragmas are per connection, so they must be applied every time a connection is opened. Foreign keys are off by default in SQLite and relationships depend on them.

Tasks

  • Config::file(path) and Config::memory().
  • Options: foreign_keys(bool) (default true), journal_mode(JournalMode) (SQLite's default unless set; Wal is opt-in), busy_timeout(Duration) (default 5 s).
  • Config::open() -> Result<Connection> opens and applies the pragmas. Check the journal mode SQLite reports; in-memory databases report memory, which is not an error.
  • Add tempfile as a dev-dependency.

Example

let config = Config::file("app.db")
    .foreign_keys(true)
    .journal_mode(JournalMode::Wal)
    .busy_timeout(Duration::from_secs(5));

let conn: rusqlite::Connection = config.open()?;
#[test]
fn foreign_keys_are_on_by_default() {
    let conn = Config::memory().open().unwrap();
    let on: i64 = conn.query_row("PRAGMA foreign_keys", [], |r| r.get(0)).unwrap();
    assert_eq!(on, 1);
}

#[test]
fn foreign_keys_can_be_disabled() {
    let conn = Config::memory().foreign_keys(false).open().unwrap();
    let on: i64 = conn.query_row("PRAGMA foreign_keys", [], |r| r.get(0)).unwrap();
    assert_eq!(on, 0);
}

#[test]
fn file_config_can_use_wal() {
    let dir = tempfile::tempdir().unwrap();
    let conn = Config::file(dir.path().join("app.db"))
        .journal_mode(JournalMode::Wal)
        .open()
        .unwrap();
    let mode: String = conn.query_row("PRAGMA journal_mode", [], |r| r.get(0)).unwrap();
    assert_eq!(mode, "wal");
}

Acceptance criteria

  • The three tests above pass.
  • Opening a nonexistent directory path returns Error::Sqlite, not a panic.
**Depends on:** #1, #2 ## Context Database settings (where it lives, foreign keys, journal mode, busy timeout) need one place. SQLite pragmas are per connection, so they must be applied every time a connection is opened. Foreign keys are off by default in SQLite and relationships depend on them. ## Tasks - [x] `Config::file(path)` and `Config::memory()`. - [x] Options: `foreign_keys(bool)` (default **true**), `journal_mode(JournalMode)` (SQLite's default unless set; `Wal` is opt-in), `busy_timeout(Duration)` (default 5 s). - [x] `Config::open() -> Result<Connection>` opens and applies the pragmas. Check the journal mode SQLite reports; in-memory databases report `memory`, which is not an error. - [x] Add `tempfile` as a dev-dependency. ## Example ```rust let config = Config::file("app.db") .foreign_keys(true) .journal_mode(JournalMode::Wal) .busy_timeout(Duration::from_secs(5)); let conn: rusqlite::Connection = config.open()?; ``` ```rust #[test] fn foreign_keys_are_on_by_default() { let conn = Config::memory().open().unwrap(); let on: i64 = conn.query_row("PRAGMA foreign_keys", [], |r| r.get(0)).unwrap(); assert_eq!(on, 1); } #[test] fn foreign_keys_can_be_disabled() { let conn = Config::memory().foreign_keys(false).open().unwrap(); let on: i64 = conn.query_row("PRAGMA foreign_keys", [], |r| r.get(0)).unwrap(); assert_eq!(on, 0); } #[test] fn file_config_can_use_wal() { let dir = tempfile::tempdir().unwrap(); let conn = Config::file(dir.path().join("app.db")) .journal_mode(JournalMode::Wal) .open() .unwrap(); let mode: String = conn.query_row("PRAGMA journal_mode", [], |r| r.get(0)).unwrap(); assert_eq!(mode, "wal"); } ``` ## Acceptance criteria - [x] The three tests above pass. - [x] Opening a nonexistent directory path returns `Error::Sqlite`, not a panic.
Sign in to join this conversation.
No description provided.