Skip to content

Commit fe0884f

Browse files
committed
fix: Remove link from sql index on remove command
1 parent 4cb671a commit fe0884f

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎archivebox/index/sql.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def parse_sql_main_index(out_dir: str=OUTPUT_DIR) -> Iterator[Link]:
2020
for page in Snapshot.objects.all()
2121
)
2222

23+
@enforce_types
24+
def remove_from_sql_main_index(links: List[Link], out_dir: str=OUTPUT_DIR) -> None:
25+
setup_django(out_dir, check_db=True)
26+
from core.models import Snapshot
27+
from django.db import transaction
28+
29+
with transaction.atomic():
30+
for link in links:
31+
Snapshot.objects.filter(url=link.url).delete()
32+
2333
@enforce_types
2434
def write_sql_main_index(links: List[Link], out_dir: str=OUTPUT_DIR) -> None:
2535
setup_django(out_dir, check_db=True)

‎archivebox/main.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
parse_sql_main_index,
5050
get_admins,
5151
apply_migrations,
52+
remove_from_sql_main_index,
5253
)
5354
from .index.html import parse_html_main_index
5455
from .extractors import archive_links
@@ -600,20 +601,25 @@ def remove(filter_str: Optional[str]=None,
600601
timer = TimedProgress(360, prefix=' ')
601602
try:
602603
to_keep = []
604+
to_delete = []
603605
all_links = load_main_index(out_dir=out_dir)
604606
for link in all_links:
605607
should_remove = (
606608
(after is not None and float(link.timestamp) < after)
607609
or (before is not None and float(link.timestamp) > before)
608610
or link_matches_filter(link, filter_patterns, filter_type)
609611
)
610-
if not should_remove:
612+
if should_remove:
613+
to_delete.append(link)
614+
615+
if delete:
616+
shutil.rmtree(link.link_dir, ignore_errors=True)
617+
else:
611618
to_keep.append(link)
612-
elif should_remove and delete:
613-
shutil.rmtree(link.link_dir, ignore_errors=True)
614619
finally:
615620
timer.end()
616621

622+
remove_from_sql_main_index(links=to_delete, out_dir=out_dir)
617623
write_main_index(links=to_keep, out_dir=out_dir, finished=True)
618624
log_removal_finished(len(all_links), len(to_keep))
619625

‎tests/test_remove.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .fixtures import *
2+
3+
def test_remove_leaves_index_in_consistent_state(tmp_path, process):
4+
os.chdir(tmp_path)
5+
subprocess.run(['archivebox', 'add', 'http://127.0.0.1:8080/static/example.com.html'], capture_output=True)
6+
remove_process = subprocess.run(['archivebox', 'remove', '127.0.0.1:8080/static/example.com.html', '--yes'], capture_output=True)
7+
list_process = subprocess.run(['archivebox', 'list'], capture_output=True)
8+
assert "Warning: SQL index does not match JSON index!" not in list_process.stderr.decode("utf-8")

0 commit comments

Comments
 (0)