fix: handle file path conflicts during move operations

Resolves #169 

- Add simple IntegrityError handling in handle_move function
- Use temporary path strategy to resolve file swap conflicts  
- Add comprehensive test coverage for move conflict scenarios
- Maintain backward compatibility for all other error types

This fixes the "UNIQUE constraint failed: entity.file_path, entity.project_id" 
error that occurred during sync operations when files were swapped or moved 
to locations already occupied by other entities.

Co-authored-by: Paul Hernandez <phernandez@users.noreply.github.com>
This commit is contained in:
claude[bot]
2025-07-08 02:16:52 +00:00
committed by GitHub
parent 473f70c949
commit 56f5dcd4bc
2 changed files with 180 additions and 1 deletions
+25 -1
View File
@@ -477,7 +477,31 @@ class SyncService:
f"new_checksum={new_checksum}"
)
updated = await self.entity_repository.update(entity.id, updates)
try:
updated = await self.entity_repository.update(entity.id, updates)
except IntegrityError as e:
# Handle file path conflicts (e.g., file swaps)
if "UNIQUE constraint failed: entity.file_path" in str(e):
logger.info(
f"File path conflict detected during move: "
f"entity_id={entity.id}, old_path={old_path}, new_path={new_path}. "
f"Using temporary path to resolve conflict."
)
# Use temporary path to resolve conflicts
import uuid
temp_path = f"{new_path}.tmp_{uuid.uuid4().hex[:8]}"
temp_updates = updates.copy()
temp_updates["file_path"] = temp_path
# Update to temporary path first
await self.entity_repository.update(entity.id, temp_updates)
# Then update to final path
updated = await self.entity_repository.update(entity.id, updates)
else:
# Re-raise if it's a different integrity error
raise
if updated is None: # pragma: no cover
logger.error(