Skip to content

Commit

Permalink
internal/postgres: populate modules.incompatible field
Browse files Browse the repository at this point in the history
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 <julie@golang.org>
  • Loading branch information
Miguel Acero committed Aug 13, 2020
1 parent a1b9579 commit 67f483b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/postgres/insert_module.go
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions internal/postgres/insert_module_test.go
Expand Up @@ -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()
Expand Down

0 comments on commit 67f483b

Please sign in to comment.