From e4beafbf49c135c177222936eeb348e0bd3cf94e Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Thu, 3 Oct 2024 11:59:57 -0400 Subject: [PATCH] feat(core): open sqlite databases with `SQLITE_OPEN_FULL_MUTEX` --- packages/nx/src/native/db/mod.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/nx/src/native/db/mod.rs b/packages/nx/src/native/db/mod.rs index 0c82c01c41b6e..90ca66f32fa34 100644 --- a/packages/nx/src/native/db/mod.rs +++ b/packages/nx/src/native/db/mod.rs @@ -1,10 +1,11 @@ +use rusqlite::OpenFlags; use std::fs::{create_dir_all, remove_file}; use std::path::PathBuf; +use crate::native::machine_id::get_machine_id; use napi::bindgen_prelude::External; use rusqlite::Connection; use tracing::debug; -use crate::native::machine_id::get_machine_id; #[napi] pub fn connect_to_nx_db( @@ -13,15 +14,15 @@ pub fn connect_to_nx_db( db_name: Option, ) -> anyhow::Result> { let cache_dir_buf = PathBuf::from(cache_dir); - let db_path = cache_dir_buf.join(format!( - "{}.db", - db_name.unwrap_or_else(get_machine_id) - )); + let db_path = cache_dir_buf.join(format!("{}.db", db_name.unwrap_or_else(get_machine_id))); create_dir_all(cache_dir_buf)?; let c = create_connection(&db_path)?; - debug!("Checking if current existing database is compatible with Nx {}", nx_version); + debug!( + "Checking if current existing database is compatible with Nx {}", + nx_version + ); let db_version = c.query_row( "SELECT value FROM metadata WHERE key='NX_VERSION'", [], @@ -64,7 +65,14 @@ pub fn connect_to_nx_db( fn create_connection(db_path: &PathBuf) -> anyhow::Result { debug!("Creating connection to {:?}", db_path); - let c = Connection::open(db_path).map_err(anyhow::Error::from)?; + let c = Connection::open_with_flags( + db_path, + OpenFlags::SQLITE_OPEN_READ_WRITE + | OpenFlags::SQLITE_OPEN_CREATE + | OpenFlags::SQLITE_OPEN_URI + | OpenFlags::SQLITE_OPEN_FULL_MUTEX, + ) + .map_err(anyhow::Error::from)?; // This allows writes at the same time as reads c.pragma_update(None, "journal_mode", "WAL")?;