Skip to content

Commit 5672419

Browse files
namaggarwaljulieqiu
authored andcommitted
internal/postgres: add GetModuleInfo function
GetModuleInfo returns the ModuleInfo for given module_path and version. This will replace LegacyGetModuleInfo. Fixes golang/go#40032 Change-Id: I9f24de94b93f9f21a29e4f8ca624efc0909d46d5 GitHub-Last-Rev: 5ea5b16 GitHub-Pull-Request: #2 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/241038 Reviewed-by: Julie Qiu <[email protected]>
1 parent 20e1c13 commit 5672419

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

internal/datasource.go

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type DataSource interface {
2121
// GetImports returns a slice of import paths imported by the package
2222
// specified by path and version.
2323
GetImports(ctx context.Context, pkgPath, modulePath, version string) ([]string, error)
24+
// GetModuleInfo returns the ModuleInfo corresponding to modulePath and
25+
// version.
26+
GetModuleInfo(ctx context.Context, modulePath, version string) (*ModuleInfo, error)
2427
// GetPathInfo returns information about a path.
2528
GetPathInfo(ctx context.Context, path, inModulePath, inVersion string) (outModulePath, outVersion string, isPackage bool, err error)
2629
// GetPseudoVersionsForModule returns LegacyModuleInfo for all known

internal/postgres/details.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,45 @@ func (db *DB) GetImportedBy(ctx context.Context, pkgPath, modulePath string, lim
277277
return importedby, nil
278278
}
279279

280-
// LegacyGetModuleInfo fetches a Version from the database with the primary key
280+
// GetModuleInfo fetches a module version from the database with the primary key
281+
// (module_path, version).
282+
func (db *DB) GetModuleInfo(ctx context.Context, modulePath string, version string) (_ *internal.ModuleInfo, err error) {
283+
defer derrors.Wrap(&err, "GetModuleInfo(ctx, %q, %q)", modulePath, version)
284+
285+
query := `
286+
SELECT
287+
module_path,
288+
version,
289+
commit_time,
290+
version_type,
291+
source_info,
292+
redistributable,
293+
has_go_mod
294+
FROM
295+
modules
296+
WHERE
297+
module_path = $1
298+
AND version = $2;`
299+
300+
args := []interface{}{modulePath, version}
301+
302+
var (
303+
mi internal.ModuleInfo
304+
hasGoMod sql.NullBool
305+
)
306+
row := db.db.QueryRow(ctx, query, args...)
307+
if err := row.Scan(&mi.ModulePath, &mi.Version, &mi.CommitTime, &mi.VersionType,
308+
jsonbScanner{&mi.SourceInfo}, &mi.IsRedistributable, &hasGoMod); err != nil {
309+
if err == sql.ErrNoRows {
310+
return nil, fmt.Errorf("module version %s@%s: %w", modulePath, version, derrors.NotFound)
311+
}
312+
return nil, fmt.Errorf("row.Scan(): %v", err)
313+
}
314+
setHasGoMod(&mi, hasGoMod)
315+
return &mi, nil
316+
}
317+
318+
// LegacyGetModuleInfo fetches a module version from the database with the primary key
281319
// (module_path, version).
282320
func (db *DB) LegacyGetModuleInfo(ctx context.Context, modulePath string, version string) (_ *internal.LegacyModuleInfo, err error) {
283321
defer derrors.Wrap(&err, "LegacyGetModuleInfo(ctx, %q, %q)", modulePath, version)

internal/postgres/details_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,77 @@ import (
2121
"golang.org/x/pkgsite/internal/testing/sample"
2222
)
2323

24+
func TestPostgres_GetModuleInfo(t *testing.T) {
25+
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
26+
defer cancel()
27+
28+
defer ResetTestDB(testDB, t)
29+
30+
testCases := []struct {
31+
name, path, version string
32+
modules []*internal.Module
33+
wantIndex int // index into versions
34+
wantErr error
35+
}{
36+
{
37+
name: "version present",
38+
path: "mod.1",
39+
version: "v1.0.2",
40+
modules: []*internal.Module{
41+
sample.Module("mod.1", "v1.1.0", sample.Suffix),
42+
sample.Module("mod.1", "v1.0.2", sample.Suffix),
43+
sample.Module("mod.1", "v1.0.0", sample.Suffix),
44+
},
45+
wantIndex: 1,
46+
},
47+
{
48+
name: "version not present",
49+
path: "mod.2",
50+
version: "v1.0.3",
51+
modules: []*internal.Module{
52+
sample.Module("mod.2", "v1.1.0", sample.Suffix),
53+
sample.Module("mod.2", "v1.0.2", sample.Suffix),
54+
sample.Module("mod.2", "v1.0.0", sample.Suffix),
55+
},
56+
wantErr: derrors.NotFound,
57+
},
58+
{
59+
name: "no versions",
60+
path: "mod3",
61+
version: "v1.2.3",
62+
wantErr: derrors.NotFound,
63+
},
64+
}
65+
66+
for _, tc := range testCases {
67+
t.Run(tc.name, func(t *testing.T) {
68+
for _, v := range tc.modules {
69+
if err := testDB.InsertModule(ctx, v); err != nil {
70+
t.Error(err)
71+
}
72+
}
73+
74+
gotVI, err := testDB.GetModuleInfo(ctx, tc.path, tc.version)
75+
if err != nil {
76+
if tc.wantErr == nil {
77+
t.Fatalf("got unexpected error %v", err)
78+
}
79+
if !errors.Is(err, tc.wantErr) {
80+
t.Fatalf("got error %v, want Is(%v)", err, tc.wantErr)
81+
}
82+
return
83+
}
84+
if tc.wantIndex >= len(tc.modules) {
85+
t.Fatal("wantIndex too large")
86+
}
87+
wantVI := &tc.modules[tc.wantIndex].ModuleInfo
88+
if diff := cmp.Diff(wantVI, gotVI, cmpopts.EquateEmpty(), cmp.AllowUnexported(source.Info{})); diff != "" {
89+
t.Errorf("mismatch (-want +got):\n%s", diff)
90+
}
91+
})
92+
}
93+
}
94+
2495
func TestPostgres_GetVersionInfo_Latest(t *testing.T) {
2596
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
2697
defer cancel()

internal/proxydatasource/datasource.go

+11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ func (ds *DataSource) GetTaggedVersionsForPackageSeries(ctx context.Context, pkg
219219
return ds.listPackageVersions(ctx, pkgPath, false)
220220
}
221221

222+
// GetModuleInfo returns the ModuleInfo as fetched from the proxy for module
223+
// version specified by modulePath and version.
224+
func (ds *DataSource) GetModuleInfo(ctx context.Context, modulePath, version string) (_ *internal.ModuleInfo, err error) {
225+
defer derrors.Wrap(&err, "GetModuleInfo(%q, %q)", modulePath, version)
226+
m, err := ds.getModule(ctx, modulePath, version)
227+
if err != nil {
228+
return nil, err
229+
}
230+
return &m.ModuleInfo, nil
231+
}
232+
222233
// LegacyGetModuleInfo returns the LegacyModuleInfo as fetched from the proxy for module
223234
// version specified by modulePath and version.
224235
func (ds *DataSource) LegacyGetModuleInfo(ctx context.Context, modulePath, version string) (_ *internal.LegacyModuleInfo, err error) {

internal/proxydatasource/datasource_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ func TestDataSource_LegacyGetModuleInfo_Latest(t *testing.T) {
132132
}
133133
}
134134

135+
func TestDataSource_GetModuleInfo(t *testing.T) {
136+
ctx, ds, teardown := setup(t)
137+
defer teardown()
138+
got, err := ds.GetModuleInfo(ctx, "foo.com/bar", "v1.2.0")
139+
if err != nil {
140+
t.Fatal(err)
141+
}
142+
if diff := cmp.Diff(&wantModuleInfo, got, cmpOpts...); diff != "" {
143+
t.Errorf("GetModuleInfo diff (-want +got):\n%s", diff)
144+
}
145+
}
146+
135147
func TestDataSource_LegacyGetModuleLicenses(t *testing.T) {
136148
ctx, ds, teardown := setup(t)
137149
defer teardown()

0 commit comments

Comments
 (0)