Skip to content

Commit

Permalink
internal/postgres: insert module path even if not a package
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jba committed Sep 4, 2024
1 parent f5afe02 commit 1551f08
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/postgres/insert_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,20 @@ func (pdb *DB) insertUnits(ctx context.Context, tx *database.DB,
// ID in the paths table.
// Should be run inside a transaction.
func insertPaths(ctx context.Context, tx *database.DB, m *internal.Module) (pathToID map[string]int, err error) {
curPathsSet := map[string]bool{}
return upsertPaths(ctx, tx, pathsToInsert(m))
}

// pathsToInsert returns the paths of m that should be added to the paths table if they are not
// already there.
func pathsToInsert(m *internal.Module) []string {
s := map[string]bool{}
s[m.ModulePath] = true
s[internal.SeriesPathForModule(m.ModulePath)] = true
for _, u := range m.Units {
curPathsSet[u.Path] = true
curPathsSet[internal.V1Path(u.Path, m.ModulePath)] = true
curPathsSet[internal.SeriesPathForModule(m.ModulePath)] = true
s[u.Path] = true
s[internal.V1Path(u.Path, m.ModulePath)] = true
}
return upsertPaths(ctx, tx, slices.Collect(maps.Keys(curPathsSet)))
return slices.Collect(maps.Keys(s))
}

func insertUnits(ctx context.Context, db *database.DB, unitValues []any) (pathIDToUnitID map[int]int, err error) {
Expand Down
42 changes: 42 additions & 0 deletions internal/postgres/insert_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -55,6 +56,10 @@ func TestInsertModule(t *testing.T) {
name: "valid test for pseudoversion version",
module: sample.Module(sample.ModulePath, "v0.0.0-20210212193344-7015347762c1", "internal/foo"),
},
{
name: "v2 version",
module: sample.Module(sample.ModulePath+"/v2", "v2.0.0", "foo"),
},
{
name: "valid test with go.mod missing",
module: func() *internal.Module {
Expand Down Expand Up @@ -744,3 +749,40 @@ func TestReconcileSearch(t *testing.T) {
insert("m.com/c", "v1.0.0", "pkg", 200, imports1, "")
check(modPath3, "v1.0.0", "", imports1)
}

func TestPathsToInsert(t *testing.T) {
for _, test := range []struct {
name string
m *internal.Module
want []string
}{
{
name: "v1 module",
m: &internal.Module{
ModuleInfo: internal.ModuleInfo{ModulePath: "a.com/m"},
Units: []*internal.Unit{
{UnitMeta: internal.UnitMeta{Path: "a.com/m/u"}},
},
},
want: []string{"a.com/m", "a.com/m/u"},
},
{
name: "v2 module",
m: &internal.Module{
ModuleInfo: internal.ModuleInfo{ModulePath: "a.com/m/v2"},
Units: []*internal.Unit{
{UnitMeta: internal.UnitMeta{Path: "a.com/m/v2/u"}},
},
},
want: []string{"a.com/m", "a.com/m/u", "a.com/m/v2", "a.com/m/v2/u"},
},
} {
t.Run(test.name, func(t *testing.T) {
got := pathsToInsert(test.m)
slices.Sort(got)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 1551f08

Please sign in to comment.