From aef033055587d1983a5dace5d6edb2e318fcd583 Mon Sep 17 00:00:00 2001 From: Luke Mino-Altherr Date: Thu, 12 Mar 2026 13:21:58 -0700 Subject: [PATCH] Fix double commit in create_from_hash Move mime_type update into _register_existing_asset so it shares a single transaction with reference creation. Log a warning when the hash is not found instead of silently returning None. Amp-Thread-ID: https://ampcode.com/threads/T-019ce377-8bde-7048-bc28-a9df063409f9 Co-authored-by: Amp --- app/assets/services/ingest.py | 38 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/app/assets/services/ingest.py b/app/assets/services/ingest.py index 64a1ef68b..3d6640223 100644 --- a/app/assets/services/ingest.py +++ b/app/assets/services/ingest.py @@ -136,6 +136,7 @@ def _register_existing_asset( tags: list[str] | None = None, tag_origin: str = "manual", owner_id: str = "", + mime_type: str | None = None, ) -> RegisterAssetResult: user_metadata = user_metadata or {} @@ -144,6 +145,9 @@ def _register_existing_asset( if not asset: raise ValueError(f"No asset with hash {asset_hash}") + if mime_type and asset.mime_type != mime_type: + update_asset_hash_and_mime(session, asset_id=asset.id, mime_type=mime_type) + ref, ref_created = get_or_create_reference( session, asset_id=asset.id, @@ -411,25 +415,21 @@ def create_from_hash( ) -> UploadResult | None: canonical = hash_str.strip().lower() - with create_session() as session: - asset = get_asset_by_hash(session, asset_hash=canonical) - if not asset: - return None - - if mime_type and asset.mime_type != mime_type: - update_asset_hash_and_mime(session, asset_id=asset.id, mime_type=mime_type) - session.commit() - - result = _register_existing_asset( - asset_hash=canonical, - name=_sanitize_filename( - name, fallback=canonical.split(":", 1)[1] if ":" in canonical else canonical - ), - user_metadata=user_metadata or {}, - tags=tags or [], - tag_origin="manual", - owner_id=owner_id, - ) + try: + result = _register_existing_asset( + asset_hash=canonical, + name=_sanitize_filename( + name, fallback=canonical.split(":", 1)[1] if ":" in canonical else canonical + ), + user_metadata=user_metadata or {}, + tags=tags or [], + tag_origin="manual", + owner_id=owner_id, + mime_type=mime_type, + ) + except ValueError: + logging.warning("create_from_hash: no asset found for hash %s", canonical) + return None return UploadResult( ref=result.ref,