Skip to content

Commit

Permalink
feat(core): open sqlite databases with SQLITE_OPEN_FULL_MUTEX (#28276)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
The database is currently opened with the following flag:
```
OpenFlags::SQLITE_OPEN_NO_MUTEX,
```

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The database is opened with the following flag:
```
OpenFlags::SQLITE_OPEN_FULL_MUTEX
```

This allows sqlite itself to handle multiple connections to the database
and make all writes serial.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
Cammisuli authored Oct 3, 2024
1 parent 3375734 commit 89ae128
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 89ae128

Please sign in to comment.