fix(server): handle duplicate asset checksums atomically with ON CONFLICT#25899
Closed
Oddly wants to merge 1 commit intoimmich-app:mainfrom
Closed
fix(server): handle duplicate asset checksums atomically with ON CONFLICT#25899Oddly wants to merge 1 commit intoimmich-app:mainfrom
Oddly wants to merge 1 commit intoimmich-app:mainfrom
Conversation
|
Label error. Requires exactly 1 of: changelog:.*. Found: 🗄️server. A maintainer will add the required label. |
…LICT Replace the check-then-insert race condition with PostgreSQL ON CONFLICT DO NOTHING on the UQ_assets_owner_checksum constraint. - Split AssetRepository.create() into create() (ON CONFLICT DO NOTHING, returns undefined on conflict) and createStrict() (throws, used by createCopy for asset replacement) - Update AssetRepository.createAll() with the same ON CONFLICT clause - Handle the undefined return in uploadAsset() by cleaning up files and returning DUPLICATE status without logging noisy constraint violations - Simplify metadata.service motion photo extraction to use if/else instead of try/catch with isAssetChecksumConstraint - Update tests to match the new behavior Fixes immich-app#22742
f0fbb67 to
c7b2a2c
Compare
Author
|
I've tested the issue before and after this patch, and this patch does seem to fix it. This was definitely LLM assisted, so I took care to write tests an also manually test again.
In the newer version, this is not issue any more. |
Author
|
I've asked for feedback with a test image in the issue thread. |
Member
|
Closing in favor of #26113 |
Open
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the check-then-insert race condition in asset creation with PostgreSQL
ON CONFLICT DO NOTHINGon theUQ_assets_owner_checksumconstraint. This eliminates the noisy constraint violation errors that spam server logs when the mobile app uploads duplicate assets.Fixes #22742
Changes
AssetRepository(asset.repository.ts)create()now usesON CONFLICT (constraint UQ_assets_owner_checksum) DO NOTHINGwithexecuteTakeFirst(), returningundefinedwhen the asset already exists instead of throwingcreateStrict()(new) preserves the original throwing behavior viaexecuteTakeFirstOrThrow(), used bycreateCopy()during asset replacement where a conflict is a real errorcreateAll()also usesON CONFLICT DO NOTHING, so batch inserts silently skip duplicatesAssetMediaService(asset-media.service.ts)uploadAsset()handles theundefinedreturn fromcreate()as a duplicate: cleans up uploaded files, returnsDUPLICATEstatus, and skips the usage update — all without logging a constraint violationcreateCopy()switched tocreateStrict()since backup copies during asset replacement should never conflictcreate()method now returns{ id, duplicate }for clean control flowMetadataService(metadata.service.ts)isAssetChecksumConstraintto a straightforwardif (created) / elsepatternisAssetChecksumConstraintimportTests
mockRejectedValue(error)tomockResolvedValue()replaceAssettests frommocks.asset.createtomocks.asset.createStrictcreateStrictto the asset repository mockVerification
tsc --noEmit— cleanpnpm lint— clean (0 warnings, 0 errors)