diff --git a/data/model/tag.py b/data/model/tag.py index dbff7ff73..d4c89c153 100644 --- a/data/model/tag.py +++ b/data/model/tag.py @@ -612,7 +612,13 @@ def get_possibly_expired_tag(namespace, repo_name, tag_name): def associate_generated_tag_manifest(namespace, repo_name, tag_name, manifest, storage_id_map): tag = get_active_tag(namespace, repo_name, tag_name) - return _create_manifest(tag, manifest, storage_id_map) + try: + manifest = TagManifest.get(digest=manifest.digest) + manifest.tag = tag + manifest.save() + return manifest, False + except TagManifest.DoesNotExist: + return _create_manifest(tag, manifest, storage_id_map), True def _create_manifest(tag, manifest, storage_id_map): @@ -639,9 +645,20 @@ def _create_manifest(tag, manifest, storage_id_map): def populate_manifest(repository, manifest, legacy_image, storage_ids): """ Populates the rows for the manifest, including its blobs and legacy image. """ media_type = Manifest.media_type.get_id(manifest.media_type) + + # Check for an existing manifest. If present, return it. + try: + return Manifest.get(repository=repository, digest=manifest.digest) + except Manifest.DoesNotExist: + pass + with db_transaction(): - manifest_row = Manifest.create(digest=manifest.digest, repository=repository, - manifest_bytes=manifest.bytes, media_type=media_type) + try: + manifest_row = Manifest.create(digest=manifest.digest, repository=repository, + manifest_bytes=manifest.bytes, media_type=media_type) + except IntegrityError: + return Manifest.get(repository=repository, digest=manifest.digest) + ManifestLegacyImage.create(manifest=manifest_row, repository=repository, image=legacy_image) blobs_to_insert = [dict(manifest=manifest_row, repository=repository,