Skip to content

Commit 1551f08

Browse files
committed
internal/postgres: insert module path even if not a package
When inserting the paths of a module into the paths table, we were inserting all the package paths and the v1 module path (the module path without "/vN" suffix). That is almost always sufficient. But if a module has a version suffix and is not itself a package, then we were not inserting its path. The module path is needed by insertSymbols, so this was causing that function to fail. Add the module path, refactor the code to simplify it and allow testing, and add a test. (For a vN module the code also adds the v1 package path for every package. I think this is unnecessary, but it's too risky to remove it.) Change-Id: I8caa3dd945692a4ec0a1128199237de6b77aa719 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/609117 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent f5afe02 commit 1551f08

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

internal/postgres/insert_module.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,20 @@ func (pdb *DB) insertUnits(ctx context.Context, tx *database.DB,
438438
// ID in the paths table.
439439
// Should be run inside a transaction.
440440
func insertPaths(ctx context.Context, tx *database.DB, m *internal.Module) (pathToID map[string]int, err error) {
441-
curPathsSet := map[string]bool{}
441+
return upsertPaths(ctx, tx, pathsToInsert(m))
442+
}
443+
444+
// pathsToInsert returns the paths of m that should be added to the paths table if they are not
445+
// already there.
446+
func pathsToInsert(m *internal.Module) []string {
447+
s := map[string]bool{}
448+
s[m.ModulePath] = true
449+
s[internal.SeriesPathForModule(m.ModulePath)] = true
442450
for _, u := range m.Units {
443-
curPathsSet[u.Path] = true
444-
curPathsSet[internal.V1Path(u.Path, m.ModulePath)] = true
445-
curPathsSet[internal.SeriesPathForModule(m.ModulePath)] = true
451+
s[u.Path] = true
452+
s[internal.V1Path(u.Path, m.ModulePath)] = true
446453
}
447-
return upsertPaths(ctx, tx, slices.Collect(maps.Keys(curPathsSet)))
454+
return slices.Collect(maps.Keys(s))
448455
}
449456

450457
func insertUnits(ctx context.Context, db *database.DB, unitValues []any) (pathIDToUnitID map[int]int, err error) {

internal/postgres/insert_module_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"path/filepath"
14+
"slices"
1415
"sort"
1516
"strings"
1617
"sync"
@@ -55,6 +56,10 @@ func TestInsertModule(t *testing.T) {
5556
name: "valid test for pseudoversion version",
5657
module: sample.Module(sample.ModulePath, "v0.0.0-20210212193344-7015347762c1", "internal/foo"),
5758
},
59+
{
60+
name: "v2 version",
61+
module: sample.Module(sample.ModulePath+"/v2", "v2.0.0", "foo"),
62+
},
5863
{
5964
name: "valid test with go.mod missing",
6065
module: func() *internal.Module {
@@ -744,3 +749,40 @@ func TestReconcileSearch(t *testing.T) {
744749
insert("m.com/c", "v1.0.0", "pkg", 200, imports1, "")
745750
check(modPath3, "v1.0.0", "", imports1)
746751
}
752+
753+
func TestPathsToInsert(t *testing.T) {
754+
for _, test := range []struct {
755+
name string
756+
m *internal.Module
757+
want []string
758+
}{
759+
{
760+
name: "v1 module",
761+
m: &internal.Module{
762+
ModuleInfo: internal.ModuleInfo{ModulePath: "a.com/m"},
763+
Units: []*internal.Unit{
764+
{UnitMeta: internal.UnitMeta{Path: "a.com/m/u"}},
765+
},
766+
},
767+
want: []string{"a.com/m", "a.com/m/u"},
768+
},
769+
{
770+
name: "v2 module",
771+
m: &internal.Module{
772+
ModuleInfo: internal.ModuleInfo{ModulePath: "a.com/m/v2"},
773+
Units: []*internal.Unit{
774+
{UnitMeta: internal.UnitMeta{Path: "a.com/m/v2/u"}},
775+
},
776+
},
777+
want: []string{"a.com/m", "a.com/m/u", "a.com/m/v2", "a.com/m/v2/u"},
778+
},
779+
} {
780+
t.Run(test.name, func(t *testing.T) {
781+
got := pathsToInsert(test.m)
782+
slices.Sort(got)
783+
if diff := cmp.Diff(test.want, got); diff != "" {
784+
t.Errorf("mismatch (-want +got):\n%s", diff)
785+
}
786+
})
787+
}
788+
}

0 commit comments

Comments
 (0)