mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-28 16:48:13 -05:00
feat(bin): db clear (#3934)
This commit is contained in:
41
bin/reth/src/db/clear.rs
Normal file
41
bin/reth/src/db/clear.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use clap::Parser;
|
||||
|
||||
use reth_db::{
|
||||
database::Database,
|
||||
table::Table,
|
||||
transaction::{DbTx, DbTxMut},
|
||||
TableViewer, Tables,
|
||||
};
|
||||
|
||||
/// The arguments for the `reth db clear` command
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct Command {
|
||||
/// Table name
|
||||
#[arg()]
|
||||
pub table: Tables,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
/// Execute `db clear` command
|
||||
pub fn execute<DB: Database>(self, db: &DB) -> eyre::Result<()> {
|
||||
self.table.view(&ClearViewer { db })?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct ClearViewer<'a, DB: Database> {
|
||||
db: &'a DB,
|
||||
}
|
||||
|
||||
impl<DB: Database> TableViewer<()> for ClearViewer<'_, DB> {
|
||||
type Error = eyre::Report;
|
||||
|
||||
fn view<T: Table>(&self) -> Result<(), Self::Error> {
|
||||
let tx = self.db.tx_mut()?;
|
||||
tx.clear::<T>()?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use reth_db::{
|
||||
use reth_primitives::ChainSpec;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod clear;
|
||||
mod get;
|
||||
mod list;
|
||||
/// DB List TUI
|
||||
@@ -71,6 +72,8 @@ pub enum Subcommands {
|
||||
Get(get::Command),
|
||||
/// Deletes all database entries
|
||||
Drop,
|
||||
/// Deletes all table entries
|
||||
Clear(clear::Command),
|
||||
/// Lists current and local database versions
|
||||
Version,
|
||||
/// Returns the full database path
|
||||
@@ -172,6 +175,10 @@ impl Command {
|
||||
let mut tool = DbTool::new(&db, self.chain.clone())?;
|
||||
tool.drop(db_path)?;
|
||||
}
|
||||
Subcommands::Clear(command) => {
|
||||
let db = open_db(&db_path, self.db.log_level)?;
|
||||
command.execute(&db)?;
|
||||
}
|
||||
Subcommands::Version => {
|
||||
let local_db_version = match get_db_version(&db_path) {
|
||||
Ok(version) => Some(version),
|
||||
|
||||
Reference in New Issue
Block a user