Skip to content

Commit edc1b31

Browse files
authored
Skip execution of migrations if all were executed previously (#1730)
1 parent 3ce2289 commit edc1b31

File tree

1 file changed

+28
-20
lines changed
  • crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/migrations

1 file changed

+28
-20
lines changed

crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/migrations/mod.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::Path;
44
use std::sync::Arc;
55

66
use anyhow::anyhow;
7-
use tracing::{debug, error};
7+
use tracing::{debug, error, info};
88

99
use super::migrations::utils::{drop_column_families, list_column_families};
1010
use super::LedgerDB;
@@ -61,7 +61,7 @@ impl<'a> LedgerDBMigrator<'a> {
6161
return Ok(());
6262
}
6363

64-
debug!("Starting LedgerDB migrations...");
64+
info!("Checking for pending LedgerDB migrations...");
6565

6666
let dbs_path = &self.ledger_path;
6767

@@ -94,10 +94,23 @@ impl<'a> LedgerDBMigrator<'a> {
9494
// Return an empty vector for executed migrations in case of an error since the iteration fails
9595
// because of the absence of the table.
9696
let executed_migrations = ledger_db.get_executed_migrations().unwrap_or(vec![]);
97+
let unexecuted_migrations: Vec<_> = self
98+
.migrations
99+
.iter()
100+
.filter(|migration| !executed_migrations.contains(&migration.identifier()))
101+
.collect();
102+
103+
// Do not invoke backup the database and prepare for migration
104+
// if there are no migrations that were not previously executed.
105+
if unexecuted_migrations.is_empty() {
106+
info!("No pending ledger migrations found, skipping.");
107+
return Ok(());
108+
}
97109

98110
// Drop the lock file
99111
drop(ledger_db);
100112

113+
info!("Pending migrations exist. Preparing backups...");
101114
// Copy files over, if temp_db_path falls out of scope, the directory is removed.
102115
let temp_db_path = tempfile::tempdir()?;
103116
copy_db_dir_recursive(dbs_path, temp_db_path.path())?;
@@ -110,25 +123,19 @@ impl<'a> LedgerDBMigrator<'a> {
110123

111124
let mut tables_to_drop = vec![];
112125

113-
for migration in self.migrations {
114-
if !executed_migrations.contains(&migration.identifier()) {
115-
debug!("Running migration: {}", migration.identifier().0);
116-
if let Err(e) = migration.execute(new_ledger_db.clone(), &mut tables_to_drop) {
117-
error!(
118-
"Error executing migration {}\n: {:?}",
119-
migration.identifier().0,
120-
e
121-
);
122-
123-
// Error happened on the temporary DB, therefore,
124-
// fail the node.
125-
return Err(e);
126-
}
127-
} else {
128-
debug!(
129-
"Skip previously executed migration: {}",
130-
migration.identifier().0
126+
info!("Executing pending migrations.");
127+
for migration in unexecuted_migrations {
128+
debug!("Running migration: {}", migration.identifier().0);
129+
if let Err(e) = migration.execute(new_ledger_db.clone(), &mut tables_to_drop) {
130+
error!(
131+
"Error executing migration {}\n: {:?}",
132+
migration.identifier().0,
133+
e
131134
);
135+
136+
// Error happened on the temporary DB, therefore,
137+
// fail the node.
138+
return Err(e);
132139
}
133140
}
134141

@@ -155,6 +162,7 @@ impl<'a> LedgerDBMigrator<'a> {
155162
tables_to_drop,
156163
)?;
157164

165+
info!("Migrations executed, restoring to original path");
158166
// Construct a backup path adjacent to original path
159167
let ledger_path = dbs_path.join(LEDGER_DB_PATH_SUFFIX);
160168
let temp_ledger_path = temp_db_path.path().join(LEDGER_DB_PATH_SUFFIX);

0 commit comments

Comments
 (0)