Skip to content

Commit

Permalink
Checking for dupe content in finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
David Davis committed Mar 20, 2020
1 parent 23ab7ce commit db31633
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES/plugin_api/6362.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added two new repo validation methods (validate_repo_version and validate_duplicate_content).
1 change: 1 addition & 0 deletions CHANGES/plugin_api/6362.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RepositoryVersion add_content no longer checks for duplicate content.
27 changes: 0 additions & 27 deletions pulpcore/app/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,33 +553,6 @@ def add_content(self, content):
if self.complete:
raise ResourceImmutableError(self)

error_messages = []
for type_obj in self.repository.CONTENT_TYPES:
if type_obj.repo_key_fields == ():
continue

pulp_type = type_obj.get_pulp_type()
repo_key_fields = type_obj.repo_key_fields
new_content_total = type_obj.objects.filter(
pk__in=content.filter(pulp_type=pulp_type)
).count()
unique_new_content_total = type_obj.objects.filter(
pk__in=content.filter(pulp_type=pulp_type)
).distinct(*repo_key_fields).count()

if unique_new_content_total < new_content_total:
error_messages.append(_(
"More than one {pulp_type} content with the duplicate values for {fields}."
).format(
pulp_type=pulp_type,
fields=", ".join(repo_key_fields),
)
)
if error_messages:
raise ValueError(
_("Cannot create repository version. {msg}").format(msg=", ".join(error_messages))
)

repo_content = []
to_add = set(content.exclude(pk__in=self.content).values_list('pk', flat=True))

Expand Down
57 changes: 56 additions & 1 deletion pulpcore/plugin/repo_version_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

def remove_duplicates(repository_version):
"""
Inspect content additions in the `RepositoryVersion` and replace repository duplicates.
Inspect content additions in the `RepositoryVersion` and remove existing repository duplicates.
This function will inspect the content being added to a repo version and remove any existing
content which would collide with the content being added to the repository version. It does not
inspect the content being added for duplicates.
Some content can have two instances A and B which are unique, but cannot both exist together in
one repository. For example, pulp_file's content has `relative_path` for that file within the
Expand Down Expand Up @@ -67,6 +71,44 @@ def remove_duplicates(repository_version):
repository_version.remove_content(duplicates_qs)


def validate_duplicate_content(version):
"""
Validate that a repository version doesn't contain duplicate content.
Uses repo_key_fields to determine if content is duplicated.
Raises:
ValueError: If repo version has duplicate content.
"""
error_messages = []

for type_obj in version.repository.CONTENT_TYPES:
if type_obj.repo_key_fields == ():
continue

pulp_type = type_obj.get_pulp_type()
repo_key_fields = type_obj.repo_key_fields
new_content_total = type_obj.objects.filter(
pk__in=version.content.filter(pulp_type=pulp_type)
).count()
unique_new_content_total = type_obj.objects.filter(
pk__in=version.content.filter(pulp_type=pulp_type)
).distinct(*repo_key_fields).count()

if unique_new_content_total < new_content_total:
error_messages.append(_(
"More than one {pulp_type} content with the duplicate values for {fields}."
).format(
pulp_type=pulp_type,
fields=", ".join(repo_key_fields),
)
)
if error_messages:
raise ValueError(
_("Cannot create repository version. {msg}").format(msg=", ".join(error_messages))
)


def validate_version_paths(version):
"""
Validate artifact relative paths for dupes or overlap (e.g. a/b and a/b/c).
Expand All @@ -82,3 +124,16 @@ def validate_version_paths(version):
validate_file_paths(paths)
except ValueError as e:
raise ValueError(_("Cannot create repository version. {err}.").format(err=e))


def validate_repo_version(version):
"""
Validate a repo version.
Checks for duplicate content, duplicate relative paths, etc.
Raises:
ValueError: If repo version is not valid.
"""
validate_duplicate_content(version)
validate_version_paths(version)

0 comments on commit db31633

Please sign in to comment.