Skip to content

Commit

Permalink
feat(core): open sqlite databases with SQLITE_OPEN_FULL_MUTEX
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Oct 3, 2024
1 parent e3a304b commit e4beafb
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/nx/src/native/db/mod.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -13,15 +14,15 @@ pub fn connect_to_nx_db(
db_name: Option<String>,
) -> anyhow::Result<External<Connection>> {
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'",
[],
Expand Down Expand Up @@ -64,7 +65,14 @@ pub fn connect_to_nx_db(

fn create_connection(db_path: &PathBuf) -> anyhow::Result<Connection> {
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")?;
Expand Down

0 comments on commit e4beafb

Please sign in to comment.