review: flush before the rename, not just after one delete

Move the flush to immediately before FileGroup.move so it covers every
deletion queued in this function, not only the one found by
get_by_path.  The earlier missing-file deletions were already reaching
the DB -- the get_by_path query autoflushes them -- but that is an
incidental property of the query sitting between them and the rename,
not something the rename should depend on.

Also assert the kept Video's old path is gone, so the test reads a move
rather than a copy.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-08-06 22:01:34 -06:00
parent 3a57e28cc6
commit c79d006a06
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 8 additions and 5 deletions

View file

@ -533,17 +533,17 @@ class Video(ModelHelper, Base):
existing_video = Video.get_by_path(session, video_path)
if existing_video and largest_video != existing_video:
delete_video(existing_video)
# `FileGroup.delete` unlinks the files immediately, but its DELETE is only queued on the session. The
# rename below claims `video_path` as its `primary_path`, and SQLAlchemy emits UPDATEs before DELETEs in a
# flush -- so without this flush the UPDATE hits the not-yet-deleted row and violates the unique index on
# `file_group.primary_path`, rolling back the entire download.
session.flush()
for idx, video in enumerate(duplicate_videos):
if video == existing_video:
duplicate_videos.pop(idx)
# Rename the video, add any missing tag names.
if largest_video.video_path != video_path:
# Every deletion above unlinked its files immediately, but only queued its DELETE on the session. The
# rename claims `video_path` as its `primary_path`, and SQLAlchemy emits UPDATEs before DELETEs within a
# flush -- so without this flush the UPDATE can hit a not-yet-deleted row and violate the unique index on
# `file_group.primary_path`, rolling back the entire download.
session.flush()
largest_video.file_group.move(video_path)
for tag_name in tag_names:
if tag_name not in largest_video.file_group.tag_names:

View file

@ -265,6 +265,7 @@ async def test_delete_duplicate_video_smaller_copy_at_destination(async_client,
small.file_group.url = large.file_group.url = 'https://example.com/video'
large.file_group.size = small.file_group.size + 1
large_id = large.id
large_old_path = large.video_path
test_session.commit()
assert small.video_path == video_path
@ -278,6 +279,8 @@ async def test_delete_duplicate_video_smaller_copy_at_destination(async_client,
assert video.id == large_id, 'The larger Video was not the one kept'
assert video.video_path == video_path
assert video.video_path.is_file()
assert not large_old_path.exists(), 'The kept Video was copied to the destination rather than moved'
assert set(channel.directory.iterdir()) == set(video.file_group.my_paths())
@pytest.mark.asyncio