diff --git a/tools/database/README.md b/tools/database/README.md index 7642f841445..9c4e3ec6dd6 100644 --- a/tools/database/README.md +++ b/tools/database/README.md @@ -79,6 +79,18 @@ available in `/home/ubuntu/.near/data/snapshot` This command can be helpful before attempting activities that can potentially corrupt the database. +### Run DB Migrations + +Opens the DB and runs migrations to bring it to the actual version expected by `neard` +Example usage: +```bash +cargo run --bin neard database run-migrations +``` + +For example, if the binary expects DB version `38`, but the DB is currently +version `36`, the command will open the DB, run migrations that bring the DB +from version `36` to version `38`, and then exits. + ## State read perf A tool for performance testing hot storage RocksDB State column reads. Use help to get more details: `neard database state-perf --help` diff --git a/tools/database/src/commands.rs b/tools/database/src/commands.rs index 540b50c1266..eab6fb58e4d 100644 --- a/tools/database/src/commands.rs +++ b/tools/database/src/commands.rs @@ -1,6 +1,7 @@ use crate::adjust_database::ChangeDbKindCommand; use crate::analyse_data_size_distribution::AnalyseDataSizeDistributionCommand; use crate::make_snapshot::MakeSnapshotCommand; +use crate::run_migrations::RunMigrationsCommand; use crate::state_perf::StatePerfCommand; use clap::Parser; use std::path::PathBuf; @@ -23,6 +24,9 @@ enum SubCommand { /// Make snapshot of the database MakeSnapshot(MakeSnapshotCommand), + /// Run migrations, + RunMigrations(RunMigrationsCommand), + /// Run performance test for State column reads. /// Uses RocksDB data specified via --home argument. StatePerf(StatePerfCommand), @@ -41,6 +45,7 @@ impl DatabaseCommand { .unwrap_or_else(|e| panic!("Error loading config: {:#}", e)); cmd.run(home, near_config.config.archive, &near_config.config.store) } + SubCommand::RunMigrations(cmd) => cmd.run(home), SubCommand::StatePerf(cmd) => cmd.run(home), } } diff --git a/tools/database/src/lib.rs b/tools/database/src/lib.rs index d7d45baf849..f4bb1914908 100644 --- a/tools/database/src/lib.rs +++ b/tools/database/src/lib.rs @@ -2,5 +2,6 @@ mod adjust_database; mod analyse_data_size_distribution; pub mod commands; mod make_snapshot; +mod run_migrations; mod state_perf; mod utils; diff --git a/tools/database/src/run_migrations.rs b/tools/database/src/run_migrations.rs new file mode 100644 index 00000000000..a301d39260b --- /dev/null +++ b/tools/database/src/run_migrations.rs @@ -0,0 +1,16 @@ +use std::path::Path; + +#[derive(clap::Args)] +pub(crate) struct RunMigrationsCommand {} + +impl RunMigrationsCommand { + pub(crate) fn run(&self, home_dir: &Path) -> anyhow::Result<()> { + let mut near_config = nearcore::config::load_config( + &home_dir, + near_chain_configs::GenesisValidationMode::UnsafeFast, + ) + .unwrap_or_else(|e| panic!("Error loading config: {:#}", e)); + nearcore::open_storage(home_dir, &mut near_config)?; + Ok(()) + } +}