From 15060e3a4f727d730c78a93513fe6d429d642c9c Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 4 Dec 2024 17:27:35 -0500 Subject: [PATCH] fix(core): recreate db when unable to connect (#29207) ## Current Behavior When Nx is unable to connect to the DB, nothing works and the user needs to run `nx reset` to remove the db file. ## Expected Behavior When Nx is unable to connect to the DB, the db is destroyed and recreated automatically within the same command. ## Related Issue(s) Fixes # --- packages/nx/src/native/db/initialize.rs | 72 ++++++++++++++----------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/packages/nx/src/native/db/initialize.rs b/packages/nx/src/native/db/initialize.rs index 5ad5264b33..ada1ebb349 100644 --- a/packages/nx/src/native/db/initialize.rs +++ b/packages/nx/src/native/db/initialize.rs @@ -37,44 +37,54 @@ pub(super) fn create_lock_file(db_path: &Path) -> anyhow::Result { } pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Result { - let mut c = open_database_connection(db_path)?; + match open_database_connection(db_path) { + Ok(mut c) => { + trace!( + "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'", + [], + |row| { + let r: String = row.get(0)?; + Ok(r) + }, + ); + let c = match db_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(s) if s.to_string().contains("metadata") => { + configure_database(&c)?; + create_metadata_table(&mut c, &nx_version)?; + c + } + reason => { + trace!("Incompatible database because: {:?}", reason); + trace!("Disconnecting from existing incompatible database"); + c.close()?; + trace!("Removing existing incompatible database"); + remove_file(db_path)?; - trace!( - "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'", - [], - |row| { - let r: String = row.get(0)?; - Ok(r) - }, - ); - let c = match db_version { - Ok(Some(version)) if version == nx_version => { - trace!("Database is compatible with Nx {}", nx_version); - c + trace!("Initializing a new database"); + initialize_db(nx_version, db_path)? + } + }; + + Ok(c) } - // If there is no metadata, it means that this database is new - Err(s) if s.to_string().contains("metadata") => { - configure_database(&c)?; - create_metadata_table(&mut c, &nx_version)?; - c - } - reason => { - trace!("Incompatible database because: {:?}", reason); - trace!("Disconnecting from existing incompatible database"); - c.close()?; + Err(reason) => { + trace!("Unable to connect to existing database because: {:?}", reason); trace!("Removing existing incompatible database"); remove_file(db_path)?; trace!("Initializing a new database"); - initialize_db(nx_version, db_path)? + initialize_db(nx_version, db_path) } - }; - - Ok(c) + } } fn create_metadata_table(c: &mut NxDbConnection, nx_version: &str) -> anyhow::Result<()> {