From 67f483bf812dbb53ae472097c9f729cd5ebbdd2c Mon Sep 17 00:00:00 2001 From: Miguel Acero Date: Mon, 10 Aug 2020 15:08:37 -0400 Subject: internal/postgres: populate modules.incompatible field This change modifies the InsertModule function to insert modules with an Incompatible field for the new Incompatible column in the modules table. A test is added to check the latest version of inserted modules. Updates golang/go#37714 Change-Id: I7ef04b8709f9499d747d9795531cbc83b5de25ad Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/247757 Reviewed-by: Julie Qiu --- internal/postgres/insert_module.go | 14 ++++++-- internal/postgres/insert_module_test.go | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) (limited to 'internal/postgres') diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go index e33f57ca..512c803b 100644 --- a/internal/postgres/insert_module.go +++ b/internal/postgres/insert_module.go @@ -183,8 +183,9 @@ func insertModule(ctx context.Context, db *database.DB, m *internal.Module) (_ i series_path, source_info, redistributable, - has_go_mod) - VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10, $11) + has_go_mod, + incompatible) + VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) ON CONFLICT (module_path, version) DO UPDATE SET @@ -204,6 +205,7 @@ func insertModule(ctx context.Context, db *database.DB, m *internal.Module) (_ i sourceInfoJSON, m.IsRedistributable, m.HasGoMod, + isIncompatible(m.Version), ).Scan(&moduleID) if err != nil { return 0, err @@ -557,13 +559,19 @@ func lock(ctx context.Context, tx *database.DB, modulePath string) (err error) { return nil } +// isIncompatible reports whether the build metadata of the version is +// "+incompatible", https://semver.org clause 10. +func isIncompatible(version string) bool { + return strings.HasSuffix(version, "+incompatible") +} + // isLatestVersion reports whether version is the latest version of the module. func isLatestVersion(ctx context.Context, db *database.DB, modulePath, version string) (_ bool, err error) { defer derrors.Wrap(&err, "isLatestVersion(ctx, tx, %q)", modulePath) row := db.QueryRow(ctx, ` SELECT version FROM modules WHERE module_path = $1 - ORDER BY version_type = 'release' DESC, sort_version DESC + ORDER BY incompatible, version_type = 'release' DESC, sort_version DESC LIMIT 1`, modulePath) var v string diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go index db7b9c01..d07f407e 100644 --- a/internal/postgres/insert_module_test.go +++ b/internal/postgres/insert_module_test.go @@ -343,6 +343,67 @@ func TestPostgres_ReadAndWriteModuleOtherColumns(t *testing.T) { } } +func TestLatestVersion(t *testing.T) { + // Verify that finding the latest version of a module prefers + // non-incompatible versions first. + defer ResetTestDB(testDB, t) + ctx := context.Background() + + for _, mod := range []struct { + version string + modulePath string + Incompatible bool + }{ + { + version: "v1.5.2", + modulePath: sample.ModulePath, + }, + { + version: "v2.0.0+incompatible", + modulePath: sample.ModulePath, + }, + { + version: "v2.0.1", + modulePath: sample.ModulePath + "/v2", + }, + } { + m := sample.DefaultModule() + m.Version = mod.version + m.ModulePath = mod.modulePath + + if err := testDB.InsertModule(ctx, m); err != nil { + t.Fatal(err) + } + } + + for _, tc := range []struct { + name string + modulePath string + wantVersion string + }{ + { + name: "test v1 version", + modulePath: sample.ModulePath, + wantVersion: "v1.5.2", + }, + { + name: "test v2 version", + modulePath: sample.ModulePath + "/v2", + wantVersion: "v2.0.1", + }, + } { + t.Run(tc.name, func(t *testing.T) { + isLatest, err := isLatestVersion(ctx, testDB.db, tc.modulePath, tc.wantVersion) + if err != nil { + t.Fatal(err) + } + if !isLatest { + t.Errorf("\n%+v is not the Latest version: %+v", tc.modulePath, tc.wantVersion) + } + }) + } +} + func TestDeleteModule(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() -- cgit v1.3-5-g9baa