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