Skip to content

Commit c700684

Browse files
committed
Don't delete db metadata with --delete unless with --delete-metadata
* bdx/cli.py (index): Add --delete-metadata option and pass it's value to `delete_index'. Also pass the value of `dry_run'. * bdx/index.py (delete_index): Add new `metadata_too' and `dry_run' arguments.
1 parent c0f003e commit c700684

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

bdx/cli.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ def cli():
326326
" have been modified/removed."
327327
),
328328
)
329+
@click.option(
330+
"--delete-metadata",
331+
is_flag=True,
332+
help=(
333+
"With --delete, also delete all database metadata. "
334+
"Effectively this removes all database files."
335+
),
336+
)
329337
@click.option(
330338
"-n",
331339
"--dry-run",
@@ -340,11 +348,15 @@ def index(
340348
reindex,
341349
exclude,
342350
delete,
351+
delete_metadata,
343352
dry_run,
344353
):
345354
"""Index the specified directory."""
346-
if delete and not dry_run:
347-
delete_index(index_path)
355+
if delete_metadata and not delete:
356+
msg = "--delete-metadata does not make sense without --delete"
357+
raise click.BadArgumentUsage(msg)
358+
if delete:
359+
delete_index(index_path, metadata_too=delete_metadata, dry_run=dry_run)
348360

349361
exclusions = [Exclusion(ex) for ex in exclude]
350362

bdx/index.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,17 +1186,30 @@ def unindex_file(path, is_deleted):
11861186
return stats
11871187

11881188

1189-
def delete_index(index_path: Path):
1190-
"""Delete index at given path."""
1189+
def delete_index(index_path: Path, metadata_too=False, dry_run=False):
1190+
"""Delete index at given path.
1191+
1192+
If ``metadata_too``, then delete ALL database files.
1193+
"""
11911194
if not index_path.exists():
11921195
log("Index does not exist - not deleting")
11931196
return
11941197

1198+
def log_if_dry_run(path, kind):
1199+
if dry_run:
1200+
print("would delete {} {}".format(kind, path))
1201+
else:
1202+
debug("deleting {}", kind, path)
1203+
11951204
files_to_remove: list[Path] = []
11961205
dirs_to_remove: list[Path] = []
11971206

11981207
with SymbolIndex.open(index_path, readonly=False) as index:
1199-
for shard_path in [index.path, *index.shards()]:
1208+
shards = list(index.shards())
1209+
if metadata_too:
1210+
shards.insert(0, index.path)
1211+
1212+
for shard_path in shards:
12001213
required_files = [
12011214
shard_path / "flintlock",
12021215
shard_path / "iamglass",
@@ -1223,15 +1236,19 @@ def delete_index(index_path: Path):
12231236
dirs_to_remove.append(shard_path)
12241237

12251238
for file in files_to_remove:
1226-
debug("Deleting db file: {}", file)
1227-
file.unlink()
1239+
log_if_dry_run(file, "db file")
1240+
if not dry_run:
1241+
file.unlink()
12281242

12291243
for dir in dirs_to_remove:
1230-
debug("Deleting db directory: {}", dir)
1231-
dir.rmdir()
1244+
log_if_dry_run(dir, "db directory")
1245+
if not dry_run:
1246+
dir.rmdir()
12321247

1233-
debug("Deleting index directory: {}", index_path)
1234-
index_path.rmdir()
1248+
if metadata_too:
1249+
log_if_dry_run(index_path, "index directory")
1250+
if not dry_run:
1251+
index_path.rmdir()
12351252

12361253

12371254
@dataclass(frozen=True)

0 commit comments

Comments
 (0)