fix(core): recreate db when unable to connect (#29207)

<!-- 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 -->

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
<!-- This is the behavior we should expect with the changes in this PR
-->

When Nx is unable to connect to the DB, the db is destroyed and
recreated automatically within the same command.

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

Fixes #
This commit is contained in:
Jason Jean 2024-12-04 17:27:35 -05:00 committed by GitHub
parent 4773e35d01
commit 15060e3a4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -37,8 +37,8 @@ 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 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
@ -75,6 +75,16 @@ pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Resul
};
Ok(c)
}
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)
}
}
}
fn create_metadata_table(c: &mut NxDbConnection, nx_version: &str) -> anyhow::Result<()> {