diff --git a/crates/radicle-node/src/runtime.rs b/crates/radicle-node/src/runtime.rs index 0e2b8699..0f5d61f1 100644 --- a/crates/radicle-node/src/runtime.rs +++ b/crates/radicle-node/src/runtime.rs @@ -177,7 +177,7 @@ impl Runtime { log::info!(target: "node", "Opening node database.."); let db = home .database_mut()? - .journal_mode(node::db::JournalMode::default())? + .journal_mode(node::db::sqlite_ext::JournalMode::WAL)? .init( &id, announcement.features, diff --git a/crates/radicle/src/node/db.rs b/crates/radicle/src/node/db.rs index 42316252..084e4c83 100644 --- a/crates/radicle/src/node/db.rs +++ b/crates/radicle/src/node/db.rs @@ -20,6 +20,8 @@ use crate::node::{ }; use crate::sql::transaction; +pub mod sqlite_ext; + /// How long to wait for the database lock to be released before failing a read. const DB_READ_TIMEOUT: time::Duration = time::Duration::from_secs(3); /// How long to wait for the database lock to be released before failing a write. @@ -49,19 +51,6 @@ pub enum Error { NoRows, } -/// Database journal mode. -#[derive(Debug, Default, Copy, Clone, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum JournalMode { - /// "WAL" mode. Good for concurrent reads & writes, but keeps some extra files around. - #[serde(rename = "wal")] - #[default] - WriteAheadLog, - /// Default "rollback" mode. Certain writes may block reads. - #[serde(alias = "rollback")] - Rollback, -} - /// A file-backed database storing information about the network. #[derive(Clone)] pub struct Database { @@ -107,7 +96,7 @@ impl Database { pub fn reader>(path: P) -> Result { let mut db = sql::Connection::open_thread_safe_with_flags( path, - sqlite::OpenFlags::new().with_read_only(), + sql::OpenFlags::new().with_read_only(), )?; db.set_busy_timeout(DB_READ_TIMEOUT.as_millis() as usize)?; db.execute(Self::PRAGMA)?; @@ -115,16 +104,9 @@ impl Database { Ok(Self { db: Arc::new(db) }) } - /// Set journal mode. - pub fn journal_mode(self, mode: JournalMode) -> Result { - match mode { - JournalMode::Rollback => { - self.db.execute("PRAGMA journal_mode = DELETE;")?; - } - JournalMode::WriteAheadLog => { - self.db.execute("PRAGMA journal_mode = WAL;")?; - } - } + /// Set `journal_mode` pragma. + pub fn journal_mode(self, mode: sqlite_ext::JournalMode) -> Result { + self.db.execute(format!("PRAGMA journal_mode = {mode};"))?; Ok(self) } diff --git a/crates/radicle/src/node/db/sqlite_ext.rs b/crates/radicle/src/node/db/sqlite_ext.rs new file mode 100644 index 00000000..e8b955f4 --- /dev/null +++ b/crates/radicle/src/node/db/sqlite_ext.rs @@ -0,0 +1,32 @@ +//! This module contains definitions for use with the `sqlite` crate. + +/// Value for a `journal_mode` pragma statement. +/// For a description of all variants please refer to +/// . +/// Note that when SQLite documentation talks about "the application", +/// the application linked against this crate, e.g. Radicle Node, Radicle CLI, +/// and others, is meant. +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub enum JournalMode { + #[default] + DELETE, + TRUNCATE, + PERSIST, + MEMORY, + WAL, + OFF, +} + +impl std::fmt::Display for JournalMode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::DELETE => "DELETE", + Self::TRUNCATE => "TRUNCATE", + Self::PERSIST => "PERSIST", + Self::MEMORY => "MEMORY", + Self::WAL => "WAL", + Self::OFF => "OFF", + }) + } +} diff --git a/crates/radicle/src/profile.rs b/crates/radicle/src/profile.rs index 79488aaa..0d3420f6 100644 --- a/crates/radicle/src/profile.rs +++ b/crates/radicle/src/profile.rs @@ -253,7 +253,7 @@ impl Profile { home.policies_mut()?; home.notifications_mut()?; home.database_mut()? - .journal_mode(node::db::JournalMode::default())? + .journal_mode(node::db::sqlite_ext::JournalMode::WAL)? .init( &public_key, config.node.features(),