Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 67db2ae

Browse files
author
Hendrik van Antwerpen
committed
Add flag to delete database file and report clean results
1 parent 7158142 commit 67db2ae

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

stack-graphs/src/storage.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,28 @@ impl SQLiteWriter {
101101

102102
/// Clean file data from the database. If a path is given, data for all descendants of
103103
/// that path is cleaned. Otherwise, data for all files is cleaned.
104-
pub fn clean<P: AsRef<Path>>(&mut self, path: Option<P>) -> Result<()> {
105-
if let Some(path) = path {
104+
pub fn clean<P: AsRef<Path>>(&mut self, path: Option<P>) -> Result<usize> {
105+
let count = if let Some(path) = path {
106106
let file = format!("{}%", path.as_ref().to_string_lossy());
107107
self.conn.execute("BEGIN;", [])?;
108108
self.conn
109109
.execute("DELETE FROM file_paths WHERE file LIKE ?", [&file])?;
110110
self.conn
111111
.execute("DELETE FROM root_paths WHERE file LIKE ?", [&file])?;
112-
self.conn
112+
let count = self
113+
.conn
113114
.execute("DELETE FROM graphs WHERE file LIKE ?", [&file])?;
114115
self.conn.execute("COMMIT;", [])?;
116+
count
115117
} else {
116-
self.conn.execute_batch(
117-
r#"
118-
BEGIN;
119-
DELETE FROM file_paths;
120-
DELETE FROM root_paths;
121-
DELETE FROM graphs;
122-
COMMIT;
123-
"#,
124-
)?;
125-
}
126-
Ok(())
118+
self.conn.execute("BEGIN;", [])?;
119+
self.conn.execute("DELETE FROM file_paths", [])?;
120+
self.conn.execute("DELETE FROM root_paths", [])?;
121+
let count = self.conn.execute("DELETE FROM graphs", [])?;
122+
self.conn.execute("COMMIT;", [])?;
123+
count
124+
};
125+
Ok(count)
127126
}
128127

129128
fn init(conn: &Connection) -> Result<()> {

tree-sitter-stack-graphs/src/cli/clean.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use clap::ArgGroup;
99
use clap::Args;
1010
use clap::ValueHint;
1111
use stack_graphs::storage::SQLiteWriter;
12+
use std::path::Path;
1213
use std::path::PathBuf;
1314

1415
use super::util::path_exists;
@@ -19,7 +20,7 @@ use super::util::provided_or_default_database_path;
1920
#[clap(group(
2021
ArgGroup::new("paths")
2122
.required(true)
22-
.args(&["source-paths", "all"]),
23+
.args(&["source-paths", "all", "delete"]),
2324
))]
2425
pub struct CleanArgs {
2526
/// Source file or directory paths.
@@ -40,21 +41,53 @@ pub struct CleanArgs {
4041
)]
4142
pub database: Option<PathBuf>,
4243

44+
/// Remove all data from the database.
4345
#[clap(long, short = 'a')]
4446
pub all: bool,
47+
48+
/// Delete the database file.
49+
#[clap(long)]
50+
pub delete: bool,
51+
52+
#[clap(long, short = 'v')]
53+
pub verbose: bool,
4554
}
4655

4756
impl CleanArgs {
4857
pub fn run(&self) -> anyhow::Result<()> {
4958
let db_path = provided_or_default_database_path(&self.database)?;
59+
if self.delete {
60+
self.delete(&db_path)
61+
} else {
62+
self.clean(&db_path)
63+
}
64+
}
65+
66+
fn delete(&self, db_path: &Path) -> anyhow::Result<()> {
67+
if !db_path.exists() {
68+
return Ok(());
69+
}
70+
std::fs::remove_file(db_path)?;
71+
if self.verbose {
72+
println!("deleted database {}", db_path.display());
73+
}
74+
Ok(())
75+
}
76+
77+
fn clean(&self, db_path: &Path) -> anyhow::Result<()> {
5078
let mut db = SQLiteWriter::open(&db_path)?;
51-
if self.all {
52-
db.clean(None::<&PathBuf>)?;
79+
let count = if self.all {
80+
db.clean(None::<&PathBuf>)?
5381
} else {
82+
let mut count = 0usize;
5483
for path in &self.source_paths {
5584
let path = path.canonicalize()?;
56-
db.clean(Some(path))?;
85+
count += db.clean(Some(path))?;
5786
}
87+
count
88+
};
89+
if self.verbose {
90+
println!("removed data for {} files", count);
5891
}
5992
Ok(())
6093
}

0 commit comments

Comments
 (0)