Skip to content

Commit 15060e3

Browse files
authored
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 #
1 parent 4773e35 commit 15060e3

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

packages/nx/src/native/db/initialize.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,54 @@ pub(super) fn create_lock_file(db_path: &Path) -> anyhow::Result<LockFile> {
3737
}
3838

3939
pub(super) fn initialize_db(nx_version: String, db_path: &Path) -> anyhow::Result<NxDbConnection> {
40-
let mut c = open_database_connection(db_path)?;
41-
42-
trace!(
43-
"Checking if current existing database is compatible with Nx {}",
44-
nx_version
45-
);
46-
let db_version = c.query_row(
47-
"SELECT value FROM metadata WHERE key='NX_VERSION'",
48-
[],
49-
|row| {
50-
let r: String = row.get(0)?;
51-
Ok(r)
52-
},
53-
);
54-
let c = match db_version {
55-
Ok(Some(version)) if version == nx_version => {
56-
trace!("Database is compatible with Nx {}", nx_version);
57-
c
40+
match open_database_connection(db_path) {
41+
Ok(mut c) => {
42+
trace!(
43+
"Checking if current existing database is compatible with Nx {}",
44+
nx_version
45+
);
46+
let db_version = c.query_row(
47+
"SELECT value FROM metadata WHERE key='NX_VERSION'",
48+
[],
49+
|row| {
50+
let r: String = row.get(0)?;
51+
Ok(r)
52+
},
53+
);
54+
let c = match db_version {
55+
Ok(Some(version)) if version == nx_version => {
56+
trace!("Database is compatible with Nx {}", nx_version);
57+
c
58+
}
59+
// If there is no metadata, it means that this database is new
60+
Err(s) if s.to_string().contains("metadata") => {
61+
configure_database(&c)?;
62+
create_metadata_table(&mut c, &nx_version)?;
63+
c
64+
}
65+
reason => {
66+
trace!("Incompatible database because: {:?}", reason);
67+
trace!("Disconnecting from existing incompatible database");
68+
c.close()?;
69+
trace!("Removing existing incompatible database");
70+
remove_file(db_path)?;
71+
72+
trace!("Initializing a new database");
73+
initialize_db(nx_version, db_path)?
74+
}
75+
};
76+
77+
Ok(c)
5878
}
59-
// If there is no metadata, it means that this database is new
60-
Err(s) if s.to_string().contains("metadata") => {
61-
configure_database(&c)?;
62-
create_metadata_table(&mut c, &nx_version)?;
63-
c
64-
}
65-
reason => {
66-
trace!("Incompatible database because: {:?}", reason);
67-
trace!("Disconnecting from existing incompatible database");
68-
c.close()?;
79+
Err(reason) => {
80+
trace!("Unable to connect to existing database because: {:?}", reason);
6981
trace!("Removing existing incompatible database");
7082
remove_file(db_path)?;
7183

7284
trace!("Initializing a new database");
73-
initialize_db(nx_version, db_path)?
85+
initialize_db(nx_version, db_path)
7486
}
75-
};
76-
77-
Ok(c)
87+
}
7888
}
7989

8090
fn create_metadata_table(c: &mut NxDbConnection, nx_version: &str) -> anyhow::Result<()> {

0 commit comments

Comments
 (0)