From c79d006a06241c5b09733a14822ded25f269371f Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 6 Aug 2026 22:01:34 -0600 Subject: [PATCH] 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) --- modules/videos/models.py | 10 +++++----- modules/videos/test/test_video.py | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/videos/models.py b/modules/videos/models.py index e280c65d..6d75256a 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -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: diff --git a/modules/videos/test/test_video.py b/modules/videos/test/test_video.py index 99241352..9d3b00d7 100644 --- a/modules/videos/test/test_video.py +++ b/modules/videos/test/test_video.py @@ -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