Skip to content

Commit

Permalink
fix(core): retry query and open in full_mutex on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Oct 29, 2024
1 parent 309c668 commit 37ba4e7
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions packages/nx/src/native/db/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ pub(super) fn create_lock_file(db_path: &Path) -> anyhow::Result<LockFile> {
}

pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Result<NxDbConnection> {
let c = open_database_connection(db_path)?;
let mut c = open_database_connection(db_path)?;

trace!(
"Checking if current existing database is compatible with Nx {}",
nx_version
);
let db_version = c.conn.query_row(
let db_version = c.query_row(
"SELECT value FROM metadata WHERE key='NX_VERSION'",
[],
|row| {
Expand All @@ -52,14 +52,14 @@ pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Resul
},
);
let c = match db_version {
Ok(version) if version == nx_version => {
Ok(Some(version)) if version == nx_version => {
trace!("Database is compatible with Nx {}", nx_version);
c
}
// If there is no metadata, it means that this database is new
Err(rusqlite::Error::SqliteFailure(_, Some(s))) if s.contains("metadata") => {
Err(s) if s.to_string().contains("metadata") => {
configure_database(&c)?;
create_metadata_table(&c, &nx_version)?;
create_metadata_table(&mut c, &nx_version)?;
c
}
check @ _ => {
Expand All @@ -77,20 +77,24 @@ pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Resul
Ok(c)
}

fn create_metadata_table(c: &NxDbConnection, nx_version: &str) -> anyhow::Result<()> {
fn create_metadata_table(c: &mut NxDbConnection, nx_version: &str) -> anyhow::Result<()> {
debug!("Creating table for metadata");
c.execute(
"CREATE TABLE metadata (
key TEXT NOT NULL PRIMARY KEY,
value TEXT NOT NULL
)",
[],
)?;
trace!("Recording Nx Version: {}", nx_version);
c.execute(
"INSERT INTO metadata (key, value) VALUES ('NX_VERSION', ?)",
[nx_version],
)?;
c.transaction(|conn| {
conn.execute(
"CREATE TABLE metadata (
key TEXT NOT NULL PRIMARY KEY,
value TEXT NOT NULL
)",
[],
)?;
trace!("Recording Nx Version: {}", nx_version);
conn.execute(
"INSERT INTO metadata (key, value) VALUES ('NX_VERSION', ?)",
[nx_version],
)?;
Ok(())
})?;

Ok(())
}

Expand All @@ -102,7 +106,7 @@ fn open_database_connection(db_path: &Path) -> anyhow::Result<NxDbConnection> {
OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_URI
| OpenFlags::SQLITE_OPEN_NO_MUTEX,
| OpenFlags::SQLITE_OPEN_FULL_MUTEX,
"unix-dotfile",
)
} else {
Expand Down Expand Up @@ -189,15 +193,15 @@ mod tests {
let _ = initialize_db("1.0.0".to_string(), &db_path)?;

// Try to initialize with different version
let conn = initialize_db("0.0.0-pr-28667-e5533b1".to_string(), &db_path)?;
let conn = initialize_db("2.0.0".to_string(), &db_path)?;

let version: Option<String> = conn.query_row(
"SELECT value FROM metadata WHERE key='NX_VERSION'",
[],
|row| row.get(0),
)?;

assert_eq!(version.unwrap(), "0.0.0-pr-28667-e5533b1");
assert_eq!(version.unwrap(), "2.0.0");
Ok(())
}
}

0 comments on commit 37ba4e7

Please sign in to comment.