internal/postgres/details: GetNestedModules supports input paths which have major version

GetNestedModules figures out which nested modules to return using
the module_path. However, this means that modules will not be returned
when the major version is v2 or higher. This CL solves the issue by
trying to split input path into prefix and path major version and only
use the prefix for query nested modules.

Fixes golang/go#41802

Change-Id: Icb94bbcdf6970f58fcca171033fa207fedbf091d
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/260277
Reviewed-by: Julie Qiu <julie@golang.org>
Trust: Julie Qiu <julie@golang.org>
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/internal/postgres/details.go b/internal/postgres/details.go
index 7250bc4..37006fe 100644
--- a/internal/postgres/details.go
+++ b/internal/postgres/details.go
@@ -20,7 +20,7 @@
 )
 
 // GetNestedModules returns the latest major version of all nested modules
-// given a modulePath path prefix.
+// given a modulePath path prefix with or without major version.
 func (db *DB) GetNestedModules(ctx context.Context, modulePath string) (_ []*internal.ModuleInfo, err error) {
 	defer derrors.Wrap(&err, "GetNestedModules(ctx, %v)", modulePath)
 
@@ -58,7 +58,8 @@
 		}
 		return nil
 	}
-	if err := db.db.RunQuery(ctx, query, collect, modulePath); err != nil {
+	seriesPath := internal.SeriesPathForModule(modulePath)
+	if err := db.db.RunQuery(ctx, query, collect, seriesPath); err != nil {
 		return nil, err
 	}
 
diff --git a/internal/postgres/details_test.go b/internal/postgres/details_test.go
index 5ffebb8..2e89172 100644
--- a/internal/postgres/details_test.go
+++ b/internal/postgres/details_test.go
@@ -56,6 +56,17 @@
 				"cloud.google.com/go/storage/v11",
 			},
 		},
+		{
+			name: "Nested Modules in golang.org/x/tools/v2 that have the same module prefix path",
+			path: "golang.org/x/tools/v2",
+			modules: []*internal.Module{
+				sample.LegacyModule("golang.org/x/tools", "v0.0.1", sample.Suffix),
+				sample.LegacyModule("golang.org/x/tools/gopls", "v0.5.1", sample.Suffix),
+			},
+			wantModulePaths: []string{
+				"golang.org/x/tools/gopls",
+			},
+		},
 	} {
 		t.Run(tc.name, func(t *testing.T) {
 			defer ResetTestDB(testDB, t)
@@ -65,7 +76,7 @@
 				}
 			}
 
-			gotModules, err := testDB.GetNestedModules(ctx, "cloud.google.com/go")
+			gotModules, err := testDB.GetNestedModules(ctx, tc.path)
 			if err != nil {
 				t.Fatal(err)
 			}