Fixing bug where duplicate entries could be created when using FileGroup.merge.
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

This commit is contained in:
Roland 2026-02-15 17:30:11 -07:00
parent 8fc98741d0
commit 3a9d3da3e9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 17 additions and 1 deletions

View file

@ -518,14 +518,18 @@ class FileGroup(ModelHelper, Base):
session = Session.object_session(self)
collected_files = self.files.copy()
# Track filenames (strings) for proper deduplication
existing_filenames = {f['path'] for f in self.files}
for file_group in file_groups:
if file_group.primary_path == self.primary_path:
# Don't merge myself.
continue
for file in file_group.files:
if file['path'] not in self.my_paths():
if file['path'] not in existing_filenames:
collected_files.append(file)
existing_filenames.add(file['path']) # Track newly added
# Move any applied Tags.
for tag_file in file_group.tag_files:
if tag_file.tag is not None and tag_file.tag.name not in self.tag_names:

View file

@ -747,6 +747,14 @@ async def test_file_group_merge(async_client, test_session, test_directory, make
assert vid_group.mimetype == 'video/mp4'
assert srt_group.mimetype == 'text/srt'
# Add srt to vid_group so both groups have it (simulates race condition scenario)
vid_group.append_files(srt)
test_session.commit()
# Verify both groups have the srt file
assert 'vid.srt' in [f['path'] for f in vid_group.files]
assert 'vid.srt' in [f['path'] for f in srt_group.files]
# Both FileGroups are merged.
vid = FileGroup.from_paths(test_session, vid, srt)
test_session.commit()
@ -763,6 +771,10 @@ async def test_file_group_merge(async_client, test_session, test_directory, make
assert vid.size > len(video_bytes)
assert vid.mimetype == 'video/mp4'
# Verify no duplicate files were created during merge
filenames = [f['path'] for f in vid.files]
assert len(filenames) == len(set(filenames)), f"Duplicate files found: {filenames}"
@pytest.mark.asyncio
async def test_move_directory(async_client, test_session, test_directory, make_files_structure,