Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): open sqlite databases with SQLITE_OPEN_FULL_MUTEX #28276

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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