Skip to content

Commit 6af0e12

Browse files
authored
feat(bin): drop static files in db drop (#6876)
1 parent fdc372f commit 6af0e12

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

bin/reth/src/commands/db/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl Command {
9696
// add network name to data dir
9797
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
9898
let db_path = data_dir.db_path();
99+
let static_files_path = data_dir.static_files_path();
99100

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

110111
let tool = DbTool::new(provider_factory, self.chain.clone())?;
111112
command.execute(data_dir, &tool)?;
@@ -116,7 +117,7 @@ impl Command {
116117
DatabaseArguments::default().log_level(self.db.log_level),
117118
)?;
118119
let provider_factory =
119-
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
120+
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;
120121

121122
let tool = DbTool::new(provider_factory, self.chain.clone())?;
122123
command.execute(&tool)?;
@@ -127,7 +128,7 @@ impl Command {
127128
DatabaseArguments::default().log_level(self.db.log_level),
128129
)?;
129130
let provider_factory =
130-
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
131+
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;
131132

132133
let tool = DbTool::new(provider_factory, self.chain.clone())?;
133134
command.execute(&tool)?;
@@ -138,15 +139,15 @@ impl Command {
138139
DatabaseArguments::default().log_level(self.db.log_level),
139140
)?;
140141
let provider_factory =
141-
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
142+
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;
142143

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

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

167168
let mut tool = DbTool::new(provider_factory, self.chain.clone())?;
168-
tool.drop(db_path)?;
169+
tool.drop(db_path, static_files_path)?;
169170
}
170171
Subcommands::Clear(command) => {
171172
let db =
172173
open_db(&db_path, DatabaseArguments::default().log_level(self.db.log_level))?;
173174
let provider_factory =
174-
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
175+
ProviderFactory::new(db, self.chain.clone(), static_files_path)?;
175176

176177
command.execute(provider_factory)?;
177178
}

bin/reth/src/utils.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,21 @@ impl<DB: Database> DbTool<DB> {
130130
.map_err(|e| eyre::eyre!(e))
131131
}
132132

133-
/// Drops the database at the given path.
134-
pub fn drop(&mut self, path: impl AsRef<Path>) -> Result<()> {
135-
let path = path.as_ref();
136-
info!(target: "reth::cli", "Dropping database at {:?}", path);
137-
fs::remove_dir_all(path)?;
133+
/// Drops the database and the static files at the given path.
134+
pub fn drop(
135+
&mut self,
136+
db_path: impl AsRef<Path>,
137+
static_files_path: impl AsRef<Path>,
138+
) -> Result<()> {
139+
let db_path = db_path.as_ref();
140+
info!(target: "reth::cli", "Dropping database at {:?}", db_path);
141+
fs::remove_dir_all(db_path)?;
142+
143+
let static_files_path = static_files_path.as_ref();
144+
info!(target: "reth::cli", "Dropping static files at {:?}", static_files_path);
145+
fs::remove_dir_all(static_files_path)?;
146+
fs::create_dir_all(static_files_path)?;
147+
138148
Ok(())
139149
}
140150

0 commit comments

Comments
 (0)