fix(bib): import_books skips attach_file entirely for a duplicate attachment

attach_file is idempotent by filename, but a re-run still called it
for an attachment already on file — a wasted copy every time. Skip the
call outright once the dup check finds it, instead of calling it and
only using the dup flag to decide whether to count it as new.
This commit is contained in:
kert
2026-09-09 21:17:04 -04:00
parent a44c9982fc
commit 30db67b7e1
2 changed files with 22 additions and 1 deletions

View File

@@ -241,8 +241,8 @@ def import_books(
) )
.fetchone() .fetchone()
) )
store.attach_file(key, att_path)
if dup is None: if dup is None:
store.attach_file(key, att_path)
n_new += 1 n_new += 1
stats["attached"] += n_new stats["attached"] += n_new
stats["missing"] += n_missing stats["missing"] += n_missing

View File

@@ -208,6 +208,27 @@ class TestImportBooks:
) )
assert len(rows) == 1 # no duplicate attachment assert len(rows) == 1 # no duplicate attachment
def test_rerun_never_calls_attach_file_for_a_duplicate(self, store, storage_dir):
"""Minor: attach_file is idempotent-by-filename, but a re-run
used to call it anyway (a wasted copy) — it must now be skipped
entirely once the dup check finds the attachment already there."""
book = self._cpt_book(storage_dir)
tags = ["source:ama"]
import_books(store, [book], tags=tags)
calls = []
orig_attach_file = store.attach_file
store.attach_file = lambda *a, **k: (
calls.append((a, k)) or orig_attach_file(*a, **k)
)
try:
second = import_books(store, [book], tags=tags)
finally:
store.attach_file = orig_attach_file
assert calls == []
assert second["attached"] == 0
def test_dry_run_makes_no_writes(self, store, storage_dir): def test_dry_run_makes_no_writes(self, store, storage_dir):
book = self._cpt_book(storage_dir) book = self._cpt_book(storage_dir)
stats = import_books(store, [book], tags=["source:ama"], dry_run=True) stats = import_books(store, [book], tags=["source:ama"], dry_run=True)