feat(bin): db version CLI (#3243)

This commit is contained in:
Alexey Shekhirin
2023-06-19 18:53:26 +01:00
committed by GitHub
parent 7ab8a7f3ec
commit 79aa9cb2c2
2 changed files with 36 additions and 8 deletions

View File

@ -8,7 +8,11 @@ use clap::{Parser, Subcommand};
use comfy_table::{Cell, Row, Table as ComfyTable}; use comfy_table::{Cell, Row, Table as ComfyTable};
use eyre::WrapErr; use eyre::WrapErr;
use human_bytes::human_bytes; use human_bytes::human_bytes;
use reth_db::{database::Database, tables}; use reth_db::{
database::Database,
tables,
version::{get_db_version, DatabaseVersionError, DB_VERSION},
};
use reth_primitives::ChainSpec; use reth_primitives::ChainSpec;
use reth_staged_sync::utils::init::init_db; use reth_staged_sync::utils::init::init_db;
use std::sync::Arc; use std::sync::Arc;
@ -66,6 +70,8 @@ pub enum Subcommands {
Get(get::Command), Get(get::Command),
/// Deletes all database entries /// Deletes all database entries
Drop, Drop,
/// Lists current and local database versions
Version,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -222,6 +228,21 @@ impl Command {
Subcommands::Drop => { Subcommands::Drop => {
tool.drop(db_path)?; tool.drop(db_path)?;
} }
Subcommands::Version => {
let local_db_version = match get_db_version(&db_path) {
Ok(version) => Some(version),
Err(DatabaseVersionError::MissingFile) => None,
Err(err) => return Err(err.into()),
};
println!("Current database version: {DB_VERSION}");
if let Some(version) = local_db_version {
println!("Local database version: {version}");
} else {
println!("Local database is uninitialized");
}
}
} }
Ok(()) Ok(())

View File

@ -34,16 +34,23 @@ pub enum DatabaseVersionError {
/// Returns [Ok] if file is found and has one line which equals to [DB_VERSION]. /// Returns [Ok] if file is found and has one line which equals to [DB_VERSION].
/// Otherwise, returns different [DatabaseVersionError] error variants. /// Otherwise, returns different [DatabaseVersionError] error variants.
pub fn check_db_version_file<P: AsRef<Path>>(db_path: P) -> Result<(), DatabaseVersionError> { pub fn check_db_version_file<P: AsRef<Path>>(db_path: P) -> Result<(), DatabaseVersionError> {
let version = get_db_version(db_path)?;
if version != DB_VERSION {
return Err(DatabaseVersionError::VersionMismatch { version })
}
Ok(())
}
/// Returns the database version from file with [DB_VERSION_FILE_NAME] name.
///
/// Returns [Ok] if file is found and contains a valid version.
/// Otherwise, returns different [DatabaseVersionError] error variants.
pub fn get_db_version<P: AsRef<Path>>(db_path: P) -> Result<u64, DatabaseVersionError> {
let version_file_path = db_version_file_path(db_path); let version_file_path = db_version_file_path(db_path);
match fs::read_to_string(&version_file_path) { match fs::read_to_string(&version_file_path) {
Ok(raw_version) => { Ok(raw_version) => {
let version = Ok(raw_version.parse::<u64>().map_err(|_| DatabaseVersionError::MalformedFile)?)
raw_version.parse::<u64>().map_err(|_| DatabaseVersionError::MalformedFile)?;
if version != DB_VERSION {
return Err(DatabaseVersionError::VersionMismatch { version })
}
Ok(())
} }
Err(err) if err.kind() == io::ErrorKind::NotFound => Err(DatabaseVersionError::MissingFile), Err(err) if err.kind() == io::ErrorKind::NotFound => Err(DatabaseVersionError::MissingFile),
Err(err) => Err(DatabaseVersionError::IORead { err, path: version_file_path }), Err(err) => Err(DatabaseVersionError::IORead { err, path: version_file_path }),