Repository: pulp/pulpcore

Issue:
Content summary shows incorrect numbers when previous version is deleted
Author: daviddavis (daviddavis)


Redmine Issue: 8798, https://pulp.plan.io/issues/8798

---

Not sure if this is a bug or intentional but I noticed while working on #8793.

If you create two repo versions (version 1 and 2) and add a content item to each, and then delete version 1, the "added" count will show a count of 1 even though compared to version 0, version 2 has 2 items added:

```
  "content_summary": {                                                                                                                                                                        
    "added": {                                                                                                                                                                                
      "file.file": {                                                                                                                                                                          
        "count": 1,                                                                                                                                                                           
        "href": "/pulp/api/v3/content/file/files/?repository_version_added=/pulp/api/v3/repositories/file/file/f53f39d7-341d-4c4c-8835-1fcddb359791/versions/2/"                              
      }                                                                                                                                                                                       
    },                                                                                                                                                                                        
    "removed": {},                                                                                                                                                                            
    "present": {                                                                                                                                                                              
      "file.file": {                                                                                                                                                                          
        "count": 2,                                                                                                                                                                           
        "href": "/pulp/api/v3/content/file/files/?repository_version=/pulp/api/v3/repositories/file/file/f53f39d7-341d-4c4c-8835-1fcddb359791/versions/2/"                                    
      }                                                                                                                                                                                       
    }                                                                                                                                                                                         
```

Also, when you query the content through the href provided by the content summary, you get back 2 units which doesn't seem to match the count provided:

```
$ http :/pulp/api/v3/content/file/files/?repository_version_added=/pulp/api/v3/repositories/file/file/f53f39d7-341d-4c4c-8835-1fcddb359791/versions/2/                                          
{                                                                                                                                                                                             
    "count": 2,                                                                                                                                                                               
    "next": null,                                                                                                                                                                             
    "previous": null,                                                                                                                                                                         
    "results": [...]
}
```

## Steps to reproduce

```
pulp file repository create --name test1 --remote file
pulp file repository sync --name test1
c1=$(pulp file content list | jq -r ".[0] | with_entries(select([.key] | inside([\"sha256\", \"relative_path\"])))")
c2=$(pulp file content list | jq -r ".[1] | with_entries(select([.key] | inside([\"sha256\", \"relative_path\"])))")

pulp file repository create --name test2
pulp file repository content modify --repository test2 --add-content "[$c1]"
pulp file repository content modify --repository test2 --add-content "[$c2]"

pulp file repository version destroy --repository test2 --version 1
pulp file repository version show --repository test2 --version 2

1. then query the content added
http :/pulp/api/v3/content/file/files/?repository_version_added=/pulp/api/v3/repositories/file/file/f53f39d7-341d-4c4c-8835-1fcddb359791/versions/2/
```





Discussion:

Resolving patch:
diff --git a/pulpcore/app/models/repository.py b/pulpcore/app/models/repository.py
--- a/pulpcore/app/models/repository.py
+++ b/pulpcore/app/models/repository.py
@@ -878,6 +878,9 @@ def _squash(self, repo_relations, next_version):
         repo_relations.filter(version_added=self).update(version_added=next_version)
         repo_relations.filter(version_removed=self).update(version_removed=next_version)
 
+        # Update next version's counts as they have been modified
+        next_version._compute_counts()
+
     def delete(self, **kwargs):
         """
         Deletes a RepositoryVersion


Test patch:
diff --git a/pulpcore/tests/functional/api/using_plugin/test_repo_versions.py b/pulpcore/tests/functional/api/using_plugin/test_repo_versions.py
--- a/pulpcore/tests/functional/api/using_plugin/test_repo_versions.py
+++ b/pulpcore/tests/functional/api/using_plugin/test_repo_versions.py
@@ -357,12 +357,16 @@ def test_delete_last_version(self):
 
     def test_delete_middle_version(self):
         """Delete a middle version."""
-        index = randint(1, len(self.repo_version_hrefs) - 2)
+        index = randint(1, len(self.repo_version_hrefs) - 3)
         delete_version(self.repo, self.repo_version_hrefs[index])
 
         with self.assertRaises(HTTPError):
             get_content(self.repo, self.repo_version_hrefs[index])
 
+        # Check added count is updated properly
+        added = get_added_content_summary(self.repo, self.repo_version_hrefs[index + 1])
+        self.assertEqual(added["file.file"], 2)
+
         for repo_version_href in self.repo_version_hrefs[index + 1 :]:
             artifact_paths = get_artifact_paths(self.repo, repo_version_href)
             self.assertIn(self.content[index]["artifact"], artifact_paths)