Skip to content

Commit

Permalink
fix(core): recreate db when unable to connect (#29207)
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 -->

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 #
  • Loading branch information
FrozenPandaz authored Dec 4, 2024
1 parent 4773e35 commit 15060e3
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions packages/nx/src/native/db/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,54 @@ 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)?;

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
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!("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<()> {
Expand Down

0 comments on commit 15060e3

Please sign in to comment.