@@ -4,7 +4,7 @@ use std::path::Path;
4
4
use std:: sync:: Arc ;
5
5
6
6
use anyhow:: anyhow;
7
- use tracing:: { debug, error} ;
7
+ use tracing:: { debug, error, info } ;
8
8
9
9
use super :: migrations:: utils:: { drop_column_families, list_column_families} ;
10
10
use super :: LedgerDB ;
@@ -61,7 +61,7 @@ impl<'a> LedgerDBMigrator<'a> {
61
61
return Ok ( ( ) ) ;
62
62
}
63
63
64
- debug ! ( "Starting LedgerDB migrations..." ) ;
64
+ info ! ( "Checking for pending LedgerDB migrations..." ) ;
65
65
66
66
let dbs_path = & self . ledger_path ;
67
67
@@ -94,10 +94,23 @@ impl<'a> LedgerDBMigrator<'a> {
94
94
// Return an empty vector for executed migrations in case of an error since the iteration fails
95
95
// because of the absence of the table.
96
96
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
+ }
97
109
98
110
// Drop the lock file
99
111
drop ( ledger_db) ;
100
112
113
+ info ! ( "Pending migrations exist. Preparing backups..." ) ;
101
114
// Copy files over, if temp_db_path falls out of scope, the directory is removed.
102
115
let temp_db_path = tempfile:: tempdir ( ) ?;
103
116
copy_db_dir_recursive ( dbs_path, temp_db_path. path ( ) ) ?;
@@ -110,25 +123,19 @@ impl<'a> LedgerDBMigrator<'a> {
110
123
111
124
let mut tables_to_drop = vec ! [ ] ;
112
125
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
131
134
) ;
135
+
136
+ // Error happened on the temporary DB, therefore,
137
+ // fail the node.
138
+ return Err ( e) ;
132
139
}
133
140
}
134
141
@@ -155,6 +162,7 @@ impl<'a> LedgerDBMigrator<'a> {
155
162
tables_to_drop,
156
163
) ?;
157
164
165
+ info ! ( "Migrations executed, restoring to original path" ) ;
158
166
// Construct a backup path adjacent to original path
159
167
let ledger_path = dbs_path. join ( LEDGER_DB_PATH_SUFFIX ) ;
160
168
let temp_ledger_path = temp_db_path. path ( ) . join ( LEDGER_DB_PATH_SUFFIX ) ;
0 commit comments