Skip to content

Commit

Permalink
internal/postgres: create GetLatestMajorVersion in postgres package
Browse files Browse the repository at this point in the history
This change completes the GetLatestMajorVersion function in the
DataSource interface that retrieves the latest major version of a module
path by adding the implementation for the postgres package.

Updates golang/go#37765

Change-Id: I48eadab8b71ffe73cec49f5c8bcd4f48f13b490d
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/250937
Reviewed-by: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
  • Loading branch information
Miguel Acero committed Aug 28, 2020
1 parent 3e82b7c commit a457b9e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/postgres/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"strings"

"golang.org/x/mod/module"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/version"
Expand Down Expand Up @@ -230,3 +231,32 @@ func getPathVersions(ctx context.Context, db *DB, path string, versionTypes ...v
}
return versions, nil
}

// GetLatestMajorVersion returns the latest major version string of a module
// path. For example, in the module path "github.com/casbin/casbin", there
// is another path with a greater major version
// "github.com/casbin/casbin/v3". This function will return "/v3" or an
// empty string if there is no major version string at the end.
func (db *DB) GetLatestMajorVersion(ctx context.Context, seriesPath string) (_ string, err error) {
defer derrors.Wrap(&err, "DB.GetLatestMajorVersion(ctx, %q)", seriesPath)

var latestPath string
latestModulePathQuery := fmt.Sprintf(`
SELECT
m.module_path
FROM
modules m
WHERE
m.series_path = $1
%s
LIMIT 1;`, orderByLatest)
row := db.db.QueryRow(ctx, latestModulePathQuery, seriesPath)
if err := row.Scan(&latestPath); err != nil {
return "", err
}
_, majorPath, ok := module.SplitPathVersion(latestPath)
if !ok {
return "", fmt.Errorf("module.SplitPathVersion(%q): %v", latestPath, majorPath)
}
return majorPath, nil
}
51 changes: 51 additions & 0 deletions internal/postgres/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package postgres

import (
"context"
"database/sql"
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -364,3 +366,52 @@ func TestGetVersions(t *testing.T) {
})
}
}
func TestGetLatestMajorVersion(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

defer ResetTestDB(testDB, t)
for _, modulePath := range []string{
"foo.com/bar",
"foo.com/bar/v2",
"foo.com/bar/v3",
"bar.com/foo",
} {
m := sample.Module(modulePath, sample.VersionString, sample.Suffix)
if err := testDB.InsertModule(ctx, m); err != nil {
t.Fatal(err)
}
}

for _, tc := range []struct {
seriesPath string
wantVersion string
wantErr error
}{
{
seriesPath: "foo.com/bar",
wantVersion: "/v3",
},
{
seriesPath: "bar.com/foo",
wantVersion: "",
},
{
seriesPath: "boo.com/far",
wantErr: sql.ErrNoRows,
},
} {
gotVersion, err := testDB.GetLatestMajorVersion(ctx, tc.seriesPath)
if err != nil {
if tc.wantErr == nil {
t.Fatalf("got unexpected error %v", err)
}
if !errors.Is(err, tc.wantErr) {
t.Errorf("got err = %v, want Is(%v)", err, tc.wantErr)
}
}
if gotVersion != tc.wantVersion {
t.Errorf("testDB.GetLatestMajorVersion(%v) = %v, want = %v", tc.seriesPath, gotVersion, tc.wantVersion)
}
}
}

0 comments on commit a457b9e

Please sign in to comment.