fix(pypi): key rows on the file name and show the release version

A PyPI row keys on the file name so each file of a release keeps its own cache and storage key. A new release_version column holds the real version and the API and dashboard show it. Startup deletes old sidecar rows with their storage and fills the release version on old file rows, keeping their storage keys. Startup also drops an empty download_events_old table. This replaces the local-version suffix.
This commit is contained in:
2026-11-23 02:36:53 +00:00
parent fb25501ae4
commit 1c9b04fba4
17 changed files with 333 additions and 47 deletions
+9 -21
View File
@@ -241,7 +241,9 @@ func (h *Handler) handlePackageFile(ctx context.Context, w http.ResponseWriter,
filename = filename[i+1:]
}
isSidecar := strings.HasSuffix(filename, metadataSuffix)
cacheVersion := artifactCacheVersion(filename, version)
// The cache key must be unique per file: a release has many wheels. The
// real version travels separately as the package's release version.
cacheVersion := filename
// Extract credentials from request
credentials := h.credExtractor.Extract(r)
@@ -314,13 +316,13 @@ func (h *Handler) handlePackageFile(ctx context.Context, w http.ResponseWriter,
return nil, err
}
if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
return &cache.FetchResult{Data: body, UpstreamURL: originalURL, RequiresAuth: true, AuthProvider: h.credValidator.Provider()}, nil
return &cache.FetchResult{Data: body, UpstreamURL: originalURL, RequiresAuth: true, AuthProvider: h.credValidator.Provider(), ReleaseVersion: version}, nil
}
if statusCode != http.StatusOK {
_ = body.Close()
return nil, fmt.Errorf("upstream returned status %d", statusCode)
}
return &cache.FetchResult{Data: body, UpstreamURL: originalURL}, nil
return &cache.FetchResult{Data: body, UpstreamURL: originalURL, ReleaseVersion: version}, nil
})
if err != nil {
@@ -427,24 +429,10 @@ func isPackageFile(path string) bool {
strings.HasSuffix(path, ".metadata")
}
// artifactCacheVersion returns the cache key's version portion. PyPI publishes
// several files per release, so the bare version would make distinct wheels
// share one cache entry. A wheel's build and tags, or a non-tarball sdist's
// extension, are appended as a local-version suffix (PEP 440, "+") to keep
// each artifact's key unique while the package version stays readable.
func artifactCacheVersion(filename, version string) string {
base := strings.TrimSuffix(filename, metadataSuffix)
switch {
case strings.HasSuffix(base, ".whl"):
rest := strings.TrimSuffix(base, ".whl")
if parts := strings.SplitN(rest, "-", 3); len(parts) == 3 && parts[2] != "" {
return version + "+" + parts[2]
}
case strings.HasSuffix(base, ".zip"):
return version + "+zip"
case strings.HasSuffix(base, ".egg"):
return version + "+egg"
}
// ReleaseVersion returns the release version for a distribution file name
// (PEP 427 wheels, PEP 625 sdists, legacy zip and egg files).
func ReleaseVersion(filename string) string {
_, version := extractPackageFileInfo("/x/" + filename)
return version
}
+8 -8
View File
@@ -208,18 +208,18 @@ func TestCacheVersionUniquePerArtifact(t *testing.T) {
}
}
func TestArtifactCacheVersion(t *testing.T) {
func TestReleaseVersion(t *testing.T) {
tests := []struct{ filename, want string }{
{"six-1.17.0.tar.gz", "1.17.0"},
{"six-1.17.0-py2.py3-none-any.whl", "1.17.0+py2.py3-none-any"},
{"numpy-1.26.0-1-cp312-cp312-manylinux.whl", "1.26.0+1-cp312-cp312-manylinux"},
{"foo-1.0.zip", "1.0+zip"},
{"foo-1.0.egg", "1.0+egg"},
{"six-1.17.0-py2.py3-none-any.whl", "1.17.0"},
{"numpy-1.26.0rc1-cp312-cp312-manylinux_2_17_x86_64.whl", "1.26.0rc1"},
{"2captcha-1.0.0.tar.gz", "1.0.0"},
{"foo-1.0.zip", "1.0"},
{"foo-1.0.egg", "1.0"},
}
for _, tt := range tests {
_, ver := extractPackageFileInfo("/x/" + tt.filename)
if got := artifactCacheVersion(tt.filename, ver); got != tt.want {
t.Errorf("artifactCacheVersion(%q) = %q, want %q", tt.filename, got, tt.want)
if got := ReleaseVersion(tt.filename); got != tt.want {
t.Errorf("ReleaseVersion(%q) = %q, want %q", tt.filename, got, tt.want)
}
}
}