From 37ba4e7bf4d9c3321073c3f0b8d0d2344cfba55b Mon Sep 17 00:00:00 2001
From: Jonathan Cammisuli <jon@cammisuli.ca>
Date: Tue, 29 Oct 2024 10:56:01 -0400
Subject: [PATCH] fix(core): retry query and open in full_mutex on CI

---
 packages/nx/src/native/db/initialize.rs | 46 ++++++++++++++-----------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/packages/nx/src/native/db/initialize.rs b/packages/nx/src/native/db/initialize.rs
index b8592db713253..5396e962fe83b 100644
--- a/packages/nx/src/native/db/initialize.rs
+++ b/packages/nx/src/native/db/initialize.rs
@@ -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| {
@@ -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 @ _ => {
@@ -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(())
 }
 
@@ -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 {
@@ -189,7 +193,7 @@ 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'",
@@ -197,7 +201,7 @@ mod tests {
             |row| row.get(0),
         )?;
 
-        assert_eq!(version.unwrap(), "0.0.0-pr-28667-e5533b1");
+        assert_eq!(version.unwrap(), "2.0.0");
         Ok(())
     }
 }