aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/versionstate.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-03-17 08:34:13 -0400
committerJonathan Amsterdam <jba@google.com>2021-03-17 20:57:52 +0000
commitf1e0412e9048ed30ace5b224ca29af160a720af6 (patch)
tree9618a6c9141a387d7d066a5c76117063cbcf1a8e /internal/postgres/versionstate.go
parent9b04cb09ba14183ffb26b031969abb1f3ae368e0 (diff)
downloadgo-x-pkgsite-f1e0412e9048ed30ace5b224ca29af160a720af6.tar.xz
internal/postgres: check for go.mod includes module_version_states
We sometimes need to know whether a a particular module version's zip has a go.mod file in order to determine the latest version of the module. As an optimization, we check the DB first before getting the zip from the proxy. Up until now, we checked only the modules table, which includes only "good" modules. This CL extends that check to the module_version_states table, which includes every version we've seen. For golang/go#44710 Change-Id: If3381615ac30c0dfc0706f9e4b14ed387fad25b2 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/302529 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Julie Qiu <julie@golang.org> TryBot-Result: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/postgres/versionstate.go')
-rw-r--r--internal/postgres/versionstate.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/postgres/versionstate.go b/internal/postgres/versionstate.go
index 1667ad4c..a98f695d 100644
--- a/internal/postgres/versionstate.go
+++ b/internal/postgres/versionstate.go
@@ -443,3 +443,46 @@ func (db *DB) GetVersionStats(ctx context.Context) (_ *VersionStats, err error)
}
return stats, nil
}
+
+// HasGoMod reports whether a given module version has a go.mod file.
+// It returns a NotFound error if it can't find any information.
+func (db *DB) HasGoMod(ctx context.Context, modulePath, version string) (has bool, err error) {
+ defer derrors.WrapStack(&err, "HasGoMod(%q, %q)", modulePath, version)
+
+ // Check the module_version_states table. It has information about
+ // every module we've seen. Ignore rows with status == 0 because
+ // they haven't been processed yet.
+ var hasP *bool
+ err = db.db.QueryRow(ctx, `
+ SELECT has_go_mod
+ FROM module_version_states
+ WHERE module_path = $1
+ AND version = $2
+ AND status != 0
+ `, modulePath, version).Scan(&hasP)
+ if err == sql.ErrNoRows {
+ return false, derrors.NotFound
+ }
+ if err != nil {
+ return false, err
+ }
+ if hasP != nil {
+ return *hasP, nil
+ }
+ // the has_go_mod column hasn't been populated yet.
+ // Fall back to the modules table.
+ // This can be removed when all rows have been populated and
+ // module_version_states.has_go_mod is migrated to NOT NULL.
+ err = db.db.QueryRow(ctx, `
+ SELECT has_go_mod
+ FROM modules
+ WHERE module_path = $1 AND version = $2
+ `, modulePath, version).Scan(&has)
+ if err == sql.ErrNoRows {
+ return false, derrors.NotFound
+ }
+ if err != nil {
+ return false, err
+ }
+ return has, nil
+}