internal/postgres: delete LegacyGetPackage
For golang/go#39629
Change-Id: I4e8cb716f16821bfe884cfe1e4b5f05ef62f16ef
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/258317
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go
index ac2500f..2ffad6b 100644
--- a/internal/postgres/insert_module_test.go
+++ b/internal/postgres/insert_module_test.go
@@ -93,24 +93,6 @@
t.Fatalf("testDB.GetModuleInfo(%q, %q) mismatch (-want +got):\n%s", want.ModulePath, want.Version, diff)
}
- for _, wantp := range want.LegacyPackages {
- got, err := testDB.LegacyGetPackage(ctx, wantp.Path, want.ModulePath, want.Version)
- if err != nil {
- t.Fatal(err)
- }
- opts := cmp.Options{
- // The packages table only includes partial license information; it
- // omits the Coverage field.
- cmpopts.IgnoreFields(internal.LegacyPackage{}, "Imports"),
- cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
- cmpopts.EquateEmpty(),
- cmp.AllowUnexported(safehtml.HTML{}),
- }
- if diff := cmp.Diff(*wantp, got.LegacyPackage, opts...); diff != "" {
- t.Fatalf("testDB.LegacyGetPackage(%q, %q) mismatch (-want +got):\n%s", wantp.Path, want.Version, diff)
- }
- }
-
for _, wantu := range want.Units {
got, err := testDB.GetUnit(ctx, &wantu.UnitMeta, internal.AllFields)
if err != nil {
diff --git a/internal/postgres/package.go b/internal/postgres/package.go
deleted file mode 100644
index dd808c8..0000000
--- a/internal/postgres/package.go
+++ /dev/null
@@ -1,152 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package postgres
-
-import (
- "context"
- "database/sql"
- "fmt"
-
- "github.com/lib/pq"
- "golang.org/x/pkgsite/internal"
- "golang.org/x/pkgsite/internal/database"
- "golang.org/x/pkgsite/internal/derrors"
- "golang.org/x/pkgsite/internal/stdlib"
-)
-
-// LegacyGetPackage returns the a package from the database with the corresponding
-// pkgPath, modulePath and version.
-//
-// If version = internal.LatestVersion, the package corresponding to
-// the latest matching module version will be fetched.
-//
-// If more than one module tie for a given dirPath and version pair, and
-// modulePath = internal.UnknownModulePath, the package in the module with the
-// longest module path will be fetched.
-// For example, if there are
-// two rows in the packages table:
-// (1) path = "github.com/hashicorp/vault/api"
-// module_path = "github.com/hashicorp/vault"
-// AND
-// (2) path = "github.com/hashicorp/vault/api"
-// module_path = "github.com/hashicorp/vault/api"
-// The latter will be returned.
-//
-// The returned error may be checked with
-// errors.Is(err, derrors.InvalidArgument) to determine if it was caused by an
-// invalid pkgPath, modulePath or version.
-//
-// The returned error may be checked with
-// errors.Is(err, derrors.InvalidArgument) to determine if it was caused by an
-// invalid path or version.
-func (db *DB) LegacyGetPackage(ctx context.Context, pkgPath, modulePath, requestedVersion string) (_ *internal.LegacyVersionedPackage, err error) {
- defer derrors.Wrap(&err, "DB.LegacyGetPackage(ctx, %q, %q)", pkgPath, requestedVersion)
- if pkgPath == "" || modulePath == "" || requestedVersion == "" {
- return nil, fmt.Errorf("none of pkgPath, modulePath, or version can be empty: %w", derrors.InvalidArgument)
- }
-
- args := []interface{}{pkgPath}
- query := `
- SELECT
- p.path,
- p.name,
- p.synopsis,
- p.v1_path,
- p.license_types,
- p.license_paths,
- p.redistributable,
- p.documentation,
- p.goos,
- p.goarch,
- m.version,
- m.commit_time,
- m.readme_file_path,
- m.readme_contents,
- m.module_path,
- m.source_info,
- m.redistributable,
- m.has_go_mod
- FROM
- modules m
- INNER JOIN
- packages p
- ON
- p.module_path = m.module_path
- AND m.version = p.version`
-
- if modulePath == internal.UnknownModulePath || modulePath == stdlib.ModulePath {
- if requestedVersion == internal.LatestVersion {
- // Only pkgPath is specified, so get the latest version of the
- // package found in any module.
- query += fmt.Sprintf(`
- WHERE
- p.path = $1
- %s
- LIMIT 1;`, orderByLatest)
- } else {
- // pkgPath and version are specified, so get that package version
- // from any module. If it exists in multiple modules, return the
- // one with the longest path.
- query += `
- WHERE
- p.path = $1
- AND p.version = $2
- ORDER BY
- p.module_path DESC
- LIMIT 1;`
- args = append(args, requestedVersion)
- }
- } else if requestedVersion == internal.LatestVersion {
- // pkgPath and modulePath are specified, so get the latest version of
- // the package in the specified module.
- query += fmt.Sprintf(`
- WHERE
- p.path = $1
- AND p.module_path = $2
- %s
- LIMIT 1;`, orderByLatest)
- args = append(args, modulePath)
- } else {
- // pkgPath, modulePath and version were all specified. Only one
- // directory should ever match this query.
- query += `
- WHERE
- p.path = $1
- AND p.version = $2
- AND p.module_path = $3`
- args = append(args, requestedVersion, modulePath)
- }
-
- var (
- pkg internal.LegacyVersionedPackage
- licenseTypes, licensePaths []string
- docHTML string
- )
- row := db.db.QueryRow(ctx, query, args...)
- err = row.Scan(&pkg.Path, &pkg.Name, &pkg.Synopsis,
- &pkg.V1Path, pq.Array(&licenseTypes), pq.Array(&licensePaths), &pkg.LegacyPackage.IsRedistributable,
- database.NullIsEmpty(&docHTML), &pkg.GOOS, &pkg.GOARCH, &pkg.Version,
- &pkg.CommitTime, database.NullIsEmpty(&pkg.LegacyReadmeFilePath), database.NullIsEmpty(&pkg.LegacyReadmeContents),
- &pkg.ModulePath, jsonbScanner{&pkg.SourceInfo}, &pkg.LegacyModuleInfo.IsRedistributable,
- &pkg.HasGoMod)
- if err != nil {
- if err == sql.ErrNoRows {
- return nil, fmt.Errorf("package %s@%s: %w", pkgPath, requestedVersion, derrors.NotFound)
- }
- return nil, fmt.Errorf("row.Scan(): %v", err)
- }
- lics, err := zipLicenseMetadata(licenseTypes, licensePaths)
- if err != nil {
- return nil, err
- }
- pkg.Licenses = lics
- pkg.DocumentationHTML = convertDocumentation(docHTML)
- if db.bypassLicenseCheck {
- pkg.IsRedistributable = true
- } else {
- pkg.RemoveNonRedistributableData()
- }
- return &pkg, nil
-}
diff --git a/internal/postgres/package_test.go b/internal/postgres/package_test.go
deleted file mode 100644
index bcd87ef..0000000
--- a/internal/postgres/package_test.go
+++ /dev/null
@@ -1,277 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package postgres
-
-import (
- "context"
- "errors"
- "testing"
-
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- "github.com/google/safehtml"
- "golang.org/x/pkgsite/internal"
- "golang.org/x/pkgsite/internal/derrors"
- "golang.org/x/pkgsite/internal/licenses"
- "golang.org/x/pkgsite/internal/source"
- "golang.org/x/pkgsite/internal/stdlib"
- "golang.org/x/pkgsite/internal/testing/sample"
-)
-
-func TestLegacyGetPackage(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
- defer cancel()
-
- defer ResetTestDB(testDB, t)
-
- suffix := func(pkgPath, modulePath string) string {
- if modulePath == stdlib.ModulePath {
- return pkgPath
- }
- if pkgPath == modulePath {
- return ""
- }
- return pkgPath[len(modulePath)+1:]
- }
-
- insertModule := func(pkgPath, modulePath, version string) {
- t.Helper()
- m := sample.LegacyModule(modulePath, version, suffix(pkgPath, modulePath))
- if err := testDB.InsertModule(ctx, m); err != nil {
- t.Fatal(err)
- }
- }
- checkPackage := func(got *internal.LegacyVersionedPackage, pkgPath, modulePath, version string) {
- t.Helper()
- want := &internal.LegacyVersionedPackage{
- LegacyModuleInfo: *sample.LegacyModuleInfo(modulePath, version),
- LegacyPackage: *sample.LegacyPackage(modulePath, suffix(pkgPath, modulePath)),
- }
- want.Imports = nil
- opts := cmp.Options{
- cmpopts.EquateEmpty(),
- cmp.AllowUnexported(source.Info{}, safehtml.HTML{}),
- // The packages table only includes partial license information; it omits the Coverage field.
- cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
- }
- if diff := cmp.Diff(want, got, opts...); diff != "" {
- t.Errorf("testDB.LegacyGetPackage(ctx, %q, %q, %q) mismatch (-want +got):\n%s", pkgPath, modulePath, version, diff)
- }
- }
-
- for _, data := range []struct {
- pkgPath, modulePath, version string
- }{
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault",
- "v1.1.2",
- },
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault/api",
- "v1.0.3",
- },
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault",
- "v1.0.3",
- },
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault",
- "v1.1.0-alpha.1",
- },
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault",
- "v1.0.0-20190311183353-d8887717615a",
- },
- {
- "github.com/hashicorp/vault/api",
- "github.com/hashicorp/vault",
- "v2.0.0+incompatible",
- },
- {
- "archive/zip",
- stdlib.ModulePath,
- "v1.13.1",
- },
- {
- "archive/zip",
- stdlib.ModulePath,
- "v1.13.0",
- },
- } {
- insertModule(data.pkgPath, data.modulePath, data.version)
- }
-
- for _, tc := range []struct {
- name, pkgPath, modulePath, version, wantPkgPath, wantModulePath, wantVersion string
- wantNotFoundErr bool
- }{
- {
- name: "want latest package to be most recent release version",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: internal.LatestVersion,
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault",
- wantVersion: "v1.1.2",
- },
- {
- name: "want package@version for ambigious module path to be longest module path",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: "v1.0.3",
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault/api",
- wantVersion: "v1.0.3",
- },
- {
- name: "want package with prerelease version and module path",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: "github.com/hashicorp/vault",
- version: "v1.1.0-alpha.1",
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault",
- wantVersion: "v1.1.0-alpha.1",
- },
- {
- name: "want package for pseudoversion, only one version for module path",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: "v1.1.0-alpha.1",
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault",
- wantVersion: "v1.1.0-alpha.1",
- },
- {
- name: "latest version of github.com/hashicorp/vault/api in github.com/hashicorp/vault/api",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: "github.com/hashicorp/vault/api",
- version: internal.LatestVersion,
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault/api",
- wantVersion: "v1.0.3",
- },
- {
- name: "latest version of github.com/hashicorp/vault/api in github.com/hashicorp/vault",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: "github.com/hashicorp/vault",
- version: internal.LatestVersion,
- wantPkgPath: "github.com/hashicorp/vault/api",
- wantModulePath: "github.com/hashicorp/vault",
- wantVersion: "v1.1.2",
- },
- {
- name: "module@version/suffix does not exist ",
- pkgPath: "github.com/hashicorp/vault/api",
- modulePath: "github.com/hashicorp/vault/api",
- wantPkgPath: "github.com/hashicorp/vault/api",
- version: "v1.1.2",
- wantNotFoundErr: true,
- },
- {
- name: "latest version of archive/zip",
- pkgPath: "archive/zip",
- modulePath: stdlib.ModulePath,
- version: internal.LatestVersion,
- wantPkgPath: "archive/zip",
- wantModulePath: stdlib.ModulePath,
- wantVersion: "v1.13.1",
- },
- {
- name: "specific version of archive/zip",
- pkgPath: "archive/zip",
- modulePath: stdlib.ModulePath,
- version: "v1.13.0",
- wantPkgPath: "archive/zip",
- wantModulePath: stdlib.ModulePath,
- wantVersion: "v1.13.0",
- },
- } {
- t.Run(tc.name, func(t *testing.T) {
- got, err := testDB.LegacyGetPackage(ctx, tc.pkgPath, tc.modulePath, tc.version)
- if tc.wantNotFoundErr {
- if !errors.Is(err, derrors.NotFound) {
- t.Fatalf("want derrors.NotFound; got = %v", err)
- }
- return
- }
- if err != nil {
- t.Fatal(err)
- }
- checkPackage(got, tc.wantPkgPath, tc.wantModulePath, tc.wantVersion)
- })
- }
-}
-
-func TestLegacyGetPackageInvalidArguments(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
- defer cancel()
-
- defer ResetTestDB(testDB, t)
-
- for _, tc := range []struct {
- name, modulePath, version string
- wantInvalidArgumentErr bool
- }{
- {
- name: "version cannot be empty",
- modulePath: internal.UnknownModulePath,
- version: "",
- wantInvalidArgumentErr: true,
- },
- {
- name: "module path cannot be empty",
- modulePath: "",
- version: internal.LatestVersion,
- wantInvalidArgumentErr: true,
- },
- } {
- t.Run(tc.name, func(t *testing.T) {
- got, err := testDB.LegacyGetPackage(ctx, tc.modulePath+"/package", tc.modulePath, tc.version)
- if !errors.Is(err, derrors.InvalidArgument) {
- t.Fatalf("want %v; got = \n%+v, %v", derrors.InvalidArgument, got, err)
- }
- })
- }
-}
-
-func TestLegacyGetPackageBypass(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
- defer cancel()
- defer ResetTestDB(testDB, t)
- bypassDB := NewBypassingLicenseCheck(testDB.db)
-
- m := nonRedistributableModule()
- if err := bypassDB.InsertModule(ctx, m); err != nil {
- t.Fatal(err)
- }
-
- for _, test := range []struct {
- db *DB
- wantEmpty bool
- }{
- {testDB, true},
- {bypassDB, false},
- } {
- pkg, err := test.db.LegacyGetPackage(ctx, m.ModulePath, m.ModulePath, m.Version)
- if err != nil {
- t.Fatal(err)
- }
- for _, got := range []string{
- pkg.LegacyPackage.Synopsis,
- pkg.LegacyPackage.DocumentationHTML.String(),
- pkg.LegacyModuleInfo.LegacyReadmeFilePath,
- pkg.LegacyModuleInfo.LegacyReadmeContents,
- } {
- if (got == "") != test.wantEmpty {
- t.Errorf("got %q, want empty %t", got, test.wantEmpty)
- }
- }
- }
-}