Skip to content

Commit

Permalink
feat(bin): drop static files in db drop (#6876)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Feb 29, 2024
1 parent fdc372f commit 6af0e12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
17 changes: 9 additions & 8 deletions bin/reth/src/commands/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Command {
// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let db_path = data_dir.db_path();
let static_files_path = data_dir.static_files_path();

match self.command {
// TODO: We'll need to add this on the DB trait.
Expand All @@ -105,7 +106,7 @@ impl Command {
DatabaseArguments::default().log_level(self.db.log_level),
)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(data_dir, &tool)?;
Expand All @@ -116,7 +117,7 @@ impl Command {
DatabaseArguments::default().log_level(self.db.log_level),
)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
Expand All @@ -127,7 +128,7 @@ impl Command {
DatabaseArguments::default().log_level(self.db.log_level),
)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
Expand All @@ -138,15 +139,15 @@ impl Command {
DatabaseArguments::default().log_level(self.db.log_level),
)?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

let tool = DbTool::new(provider_factory, self.chain.clone())?;
command.execute(&tool)?;
}
Subcommands::Drop { force } => {
if !force {
// Ask for confirmation
print!("Are you sure you want to drop the database at {db_path:?}? This cannot be undone. (y/N): ");
print!("Are you sure you want to drop the database at {data_dir}? This cannot be undone. (y/N): ");
// Flush the buffer to ensure the message is printed immediately
io::stdout().flush().unwrap();

Expand All @@ -162,16 +163,16 @@ impl Command {
let db =
open_db(&db_path, DatabaseArguments::default().log_level(self.db.log_level))?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path.clone())?;

let mut tool = DbTool::new(provider_factory, self.chain.clone())?;
tool.drop(db_path)?;
tool.drop(db_path, static_files_path)?;
}
Subcommands::Clear(command) => {
let db =
open_db(&db_path, DatabaseArguments::default().log_level(self.db.log_level))?;
let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;

command.execute(provider_factory)?;
}
Expand Down
20 changes: 15 additions & 5 deletions bin/reth/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,21 @@ impl<DB: Database> DbTool<DB> {
.map_err(|e| eyre::eyre!(e))
}

/// Drops the database at the given path.
pub fn drop(&mut self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
info!(target: "reth::cli", "Dropping database at {:?}", path);
fs::remove_dir_all(path)?;
/// Drops the database and the static files at the given path.
pub fn drop(
&mut self,
db_path: impl AsRef<Path>,
static_files_path: impl AsRef<Path>,
) -> Result<()> {
let db_path = db_path.as_ref();
info!(target: "reth::cli", "Dropping database at {:?}", db_path);
fs::remove_dir_all(db_path)?;

let static_files_path = static_files_path.as_ref();
info!(target: "reth::cli", "Dropping static files at {:?}", static_files_path);
fs::remove_dir_all(static_files_path)?;
fs::create_dir_all(static_files_path)?;

Ok(())
}

Expand Down

0 comments on commit 6af0e12

Please sign in to comment.