internal/postgres: delete LegacyGetDirectory

For golang/go#39629

Change-Id: I92f92fd071dced111301bf611ad8d1fdc7e88676
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/258282
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/internal/postgres/legacy_directory.go b/internal/postgres/legacy_directory.go
deleted file mode 100644
index ef02a45..0000000
--- a/internal/postgres/legacy_directory.go
+++ /dev/null
@@ -1,298 +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"
-	"sort"
-
-	"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"
-)
-
-// LegacyGetDirectory returns the directory corresponding to the provided dirPath,
-// modulePath, and version. The directory will contain all packages for that
-// version, in sorted order by package path.
-//
-// If version = internal.LatestVersion, the directory corresponding to the
-// latest matching module version will be fetched.
-//
-// fields is a set of fields to read (see internal.FieldSet and related
-// definitions). If a field is not in fields, it will not be read from the DB
-// and its value will be one of the XXXFieldMissing constants. Only certain
-// large fields are treated specially in this way.
-//
-// If more than one module ties for a given dirPath and version pair, and
-// modulePath = internal.UnknownModulePath, the directory for 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"
-// Only directories in the latter module will be returned.
-//
-// Packages will be returned for a given dirPath if: (1) the package path has a
-// prefix of dirPath (2) the dirPath has a prefix matching the package's
-// module_path
-//
-// For example, if the package "golang.org/x/tools/go/packages" in module
-// "golang.org/x/tools" is in the database, it will match on:
-// golang.org/x/tools
-// golang.org/x/tools/go
-// golang.org/x/tools/go/packages
-//
-// It will not match on:
-// golang.org/x/tools/g
-func (db *DB) LegacyGetDirectory(ctx context.Context, dirPath, modulePath, requestedVersion string, fields internal.FieldSet) (_ *internal.LegacyDirectory, err error) {
-	defer derrors.Wrap(&err, "DB.LegacyGetDirectory(ctx, %q, %q, %q)", dirPath, modulePath, requestedVersion)
-
-	if dirPath == "" || modulePath == "" || requestedVersion == "" {
-		return nil, fmt.Errorf("none of pkgPath, modulePath, or version can be empty: %w", derrors.InvalidArgument)
-	}
-
-	var (
-		query string
-		args  []interface{}
-	)
-	if modulePath == internal.UnknownModulePath || modulePath == stdlib.ModulePath {
-		query, args = directoryQueryWithoutModulePath(dirPath, requestedVersion, fields)
-	} else {
-		query, args = directoryQueryWithModulePath(dirPath, modulePath, requestedVersion, fields)
-	}
-
-	var (
-		packages []*internal.LegacyPackage
-		mi       = internal.LegacyModuleInfo{LegacyReadmeContents: internal.StringFieldMissing}
-	)
-	collect := func(rows *sql.Rows) error {
-		var (
-			pkg          internal.LegacyPackage
-			licenseTypes []string
-			licensePaths []string
-			docHTML      string = internal.StringFieldMissing
-		)
-		scanArgs := []interface{}{
-			&pkg.Path,
-			&pkg.Name,
-			&pkg.Synopsis,
-			&pkg.V1Path,
-		}
-		if fields&internal.WithDocumentation != 0 {
-			scanArgs = append(scanArgs, database.NullIsEmpty(&docHTML))
-		}
-		scanArgs = append(scanArgs,
-			pq.Array(&licenseTypes),
-			pq.Array(&licensePaths),
-			&pkg.IsRedistributable,
-			&pkg.GOOS,
-			&pkg.GOARCH,
-			&mi.Version,
-			&mi.ModulePath,
-			database.NullIsEmpty(&mi.LegacyReadmeFilePath))
-		if fields&internal.WithReadme != 0 {
-			scanArgs = append(scanArgs, database.NullIsEmpty(&mi.LegacyReadmeContents))
-		}
-		scanArgs = append(scanArgs,
-			&mi.CommitTime,
-			jsonbScanner{&mi.SourceInfo},
-			&mi.IsRedistributable,
-			&mi.HasGoMod)
-		if err := rows.Scan(scanArgs...); err != nil {
-			return fmt.Errorf("row.Scan(): %v", err)
-		}
-		lics, err := zipLicenseMetadata(licenseTypes, licensePaths)
-		if err != nil {
-			return err
-		}
-		pkg.Licenses = lics
-		pkg.DocumentationHTML = convertDocumentation(docHTML)
-		packages = append(packages, &pkg)
-		return nil
-	}
-	if err := db.db.RunQuery(ctx, query, collect, args...); err != nil {
-		return nil, err
-	}
-	if len(packages) == 0 {
-		return nil, fmt.Errorf("packages in directory not found: %w", derrors.NotFound)
-	}
-	sort.Slice(packages, func(i, j int) bool {
-		return packages[i].Path < packages[j].Path
-	})
-	ld := &internal.LegacyDirectory{
-		Path:             dirPath,
-		LegacyModuleInfo: mi,
-		Packages:         packages,
-	}
-	if db.bypassLicenseCheck {
-		ld.IsRedistributable = true
-	} else {
-		ld.RemoveNonRedistributableData()
-	}
-	return ld, nil
-}
-
-func directoryColumns(fields internal.FieldSet) string {
-	var doc, readme string
-	if fields&internal.WithDocumentation != 0 {
-		doc = "p.documentation,"
-	}
-	if fields&internal.WithReadme != 0 {
-		readme = "m.readme_contents,"
-	}
-	return `
-			p.path,
-			p.name,
-			p.synopsis,
-			p.v1_path,
-			` + doc + `
-			p.license_types,
-			p.license_paths,
-			p.redistributable,
-			p.goos,
-			p.goarch,
-			p.version,
-			p.module_path,
-			m.readme_file_path,
-			` + readme + `
-			m.commit_time,
-			m.source_info,
-			m.redistributable,
-			m.has_go_mod`
-}
-
-// directoryQueryWithoutModulePath returns the query and args needed to fetch a
-// directory when no module path is provided.
-func directoryQueryWithoutModulePath(dirPath, version string, fields internal.FieldSet) (string, []interface{}) {
-	if version == internal.LatestVersion {
-		// internal packages are filtered out from the search_documents table.
-		// However, for other packages, fetching from search_documents is
-		// significantly faster than fetching from packages.
-		var table string
-		if !isInternalPackage(dirPath) {
-			table = "search_documents"
-		} else {
-			table = "packages"
-		}
-
-		// Only dirPath is specified, so get the latest version of the
-		// package found in any module that contains that directory.
-		//
-		// This might not necessarily be the latest module version that
-		// matches the directory path. For example,
-		// github.com/hashicorp/vault@v1.2.3 does not contain
-		// github.com/hashicorp/vault/api, but
-		// github.com/hashicorp/vault/api@v1.1.5 does.
-		return fmt.Sprintf(`
-			SELECT %s
-			FROM
-				packages p
-			INNER JOIN (
-				SELECT *
-				FROM
-					modules m
-				WHERE
-					(m.module_path, m.version) IN (
-						SELECT module_path, version
-						FROM %s
-						WHERE tsv_parent_directories @@ $1::tsquery
-						GROUP BY 1, 2
-					)
-				%s
-				LIMIT 1
-			) m
-			ON
-				p.module_path = m.module_path
-				AND p.version = m.version
-			WHERE tsv_parent_directories @@ $1::tsquery;`,
-			directoryColumns(fields), table, orderByLatest), []interface{}{dirPath}
-	}
-
-	// dirPath and version are specified, so get that directory version
-	// from any module.  If it exists in multiple modules, return the one
-	// with the longest path.
-	return fmt.Sprintf(`
-		WITH potential_packages AS (
-			SELECT *
-			FROM packages
-			WHERE tsv_parent_directories @@ $1::tsquery
-		),
-		module_version AS (
-			SELECT m.*
-			FROM modules m
-			INNER JOIN potential_packages p
-			ON
-				p.module_path = m.module_path
-				AND p.version = m.version
-			WHERE
-				p.version = $2
-			ORDER BY
-				module_path DESC
-			LIMIT 1
-		)
-		SELECT %s
-		FROM potential_packages p
-		INNER JOIN module_version m
-		ON
-			p.module_path = m.module_path
-			AND p.version = m.version;`, directoryColumns(fields)), []interface{}{dirPath, version}
-}
-
-// directoryQueryWithoutModulePath returns the query and args needed to fetch a
-// directory when a module path is provided.
-func directoryQueryWithModulePath(dirPath, modulePath, requestedVersion string, fields internal.FieldSet) (string, []interface{}) {
-	if requestedVersion == internal.LatestVersion {
-		// dirPath and modulePath are specified, so get the latest version of
-		// the package in the specified module.
-		return fmt.Sprintf(`
-			SELECT %s
-			FROM packages p
-			INNER JOIN (
-				SELECT *
-				FROM modules m
-				WHERE
-					m.module_path = $2
-					AND m.version IN (
-						SELECT version
-						FROM packages
-						WHERE
-							tsv_parent_directories @@ $1::tsquery
-							AND module_path=$2
-					)
-				%s
-				LIMIT 1
-			) m
-			ON
-				p.module_path = m.module_path
-				AND p.version = m.version
-			WHERE
-				p.module_path = $2
-				AND tsv_parent_directories @@ $1::tsquery;`,
-			directoryColumns(fields), orderByLatest), []interface{}{dirPath, modulePath}
-	}
-
-	// dirPath, modulePath and version were all specified. Only one
-	// directory should ever match this query.
-	return fmt.Sprintf(`
-			SELECT %s
-			FROM
-				packages p
-			INNER JOIN
-				modules m
-			ON
-				p.module_path = m.module_path
-				AND p.version = m.version
-			WHERE
-				tsv_parent_directories @@ $1::tsquery
-				AND p.module_path = $2
-				AND p.version = $3;`, directoryColumns(fields)), []interface{}{dirPath, modulePath, requestedVersion}
-}
diff --git a/internal/postgres/legacy_directory_test.go b/internal/postgres/legacy_directory_test.go
deleted file mode 100644
index e4addcb..0000000
--- a/internal/postgres/legacy_directory_test.go
+++ /dev/null
@@ -1,298 +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 TestLegacyGetDirectory(t *testing.T) {
-	ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
-	defer cancel()
-
-	defer ResetTestDB(testDB, t)
-
-	InsertSampleDirectoryTree(ctx, t, testDB)
-
-	for _, tc := range []struct {
-		name, dirPath, modulePath, version, wantModulePath, wantVersion string
-		wantSuffixes                                                    []string
-		wantNotFoundErr                                                 bool
-	}{
-		{
-			name:           "latest with ambigious module path, should match longest module path",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     internal.UnknownModulePath,
-			version:        internal.LatestVersion,
-			wantVersion:    "v1.1.2",
-			wantModulePath: "github.com/hashicorp/vault/api",
-			wantSuffixes:   []string{""},
-		},
-		{
-			name:           "specified version with ambigious module path, should match longest module path",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     internal.UnknownModulePath,
-			version:        "v1.1.2",
-			wantVersion:    "v1.1.2",
-			wantModulePath: "github.com/hashicorp/vault/api",
-			wantSuffixes:   []string{""},
-		},
-		{
-			name:           "specified version with ambigous module path, but only shorter module path matches for specified version",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     internal.UnknownModulePath,
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes:   []string{"api"},
-		},
-		{
-			name:           "specified version with ambiguous module path, two module versions exist, but only shorter module path contains matching package",
-			dirPath:        "github.com/hashicorp/vault/builtin/audit",
-			modulePath:     internal.UnknownModulePath,
-			version:        "v1.1.2",
-			wantVersion:    "v1.1.2",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes: []string{
-				"builtin/audit/file",
-				"builtin/audit/socket",
-			},
-		},
-		{
-			name:           "specified module path and version, should match specified shorter module path",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     "github.com/hashicorp/vault",
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes:   []string{"api"},
-		},
-		{
-			name:           "directory path is the module path at latest",
-			dirPath:        "github.com/hashicorp/vault",
-			modulePath:     "github.com/hashicorp/vault",
-			version:        internal.LatestVersion,
-			wantVersion:    "v1.2.3",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes: []string{
-				"builtin/audit/file",
-				"builtin/audit/socket",
-				"internal/foo",
-				"vault/replication",
-				"vault/seal/transit",
-			},
-		},
-		{
-			name:           "directory path is the module path with specified version",
-			dirPath:        "github.com/hashicorp/vault",
-			modulePath:     "github.com/hashicorp/vault",
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes: []string{
-				"api",
-				"builtin/audit/file",
-				"builtin/audit/socket",
-			},
-		},
-		{
-			name:           "directory path is a package path",
-			dirPath:        "github.com/hashicorp/vault",
-			modulePath:     "github.com/hashicorp/vault",
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantModulePath: "github.com/hashicorp/vault",
-			wantSuffixes: []string{
-				"api",
-				"builtin/audit/file",
-				"builtin/audit/socket",
-			},
-		},
-		{
-			name:           "valid directory path with package at version, no module path",
-			dirPath:        "github.com/hashicorp/vault/builtin",
-			modulePath:     internal.UnknownModulePath,
-			wantModulePath: "github.com/hashicorp/vault",
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantSuffixes: []string{
-				"builtin/audit/file",
-				"builtin/audit/socket",
-			},
-		},
-		{
-			name:           "valid directory path with package, specified version and module path",
-			dirPath:        "github.com/hashicorp/vault/builtin",
-			modulePath:     "github.com/hashicorp/vault",
-			wantModulePath: "github.com/hashicorp/vault",
-			version:        "v1.0.3",
-			wantVersion:    "v1.0.3",
-			wantSuffixes: []string{
-				"builtin/audit/file",
-				"builtin/audit/socket",
-			},
-		},
-		{
-			name:           "latest version of github.com/hashicorp/vault/api in github.com/hashicorp/vault",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     "github.com/hashicorp/vault",
-			version:        internal.LatestVersion,
-			wantModulePath: "github.com/hashicorp/vault",
-			wantVersion:    "v1.1.2",
-			wantSuffixes: []string{
-				"api",
-			},
-		},
-		{
-			name:           "latest version of github.com/hashicorp/vault/api in github.com/hashicorp/vault/api",
-			dirPath:        "github.com/hashicorp/vault/api",
-			modulePath:     "github.com/hashicorp/vault/api",
-			version:        internal.LatestVersion,
-			wantModulePath: "github.com/hashicorp/vault/api",
-			wantVersion:    "v1.1.2",
-			wantSuffixes:   []string{""},
-		},
-		{
-			name:           "latest version of internal directory in github.com/hashicorp/vault",
-			dirPath:        "github.com/hashicorp/vault/internal",
-			modulePath:     internal.UnknownModulePath,
-			version:        internal.LatestVersion,
-			wantModulePath: "github.com/hashicorp/vault",
-			wantVersion:    "v1.2.3",
-			wantSuffixes:   []string{"internal/foo"},
-		},
-		{
-			name:            "invalid directory, incomplete last element",
-			dirPath:         "github.com/hashicorp/vault/builti",
-			modulePath:      internal.UnknownModulePath,
-			version:         "v1.0.3",
-			wantNotFoundErr: true,
-		},
-		{
-			name:           "stdlib directory",
-			dirPath:        "archive",
-			modulePath:     stdlib.ModulePath,
-			version:        internal.LatestVersion,
-			wantModulePath: stdlib.ModulePath,
-			wantVersion:    "v1.13.4",
-			wantSuffixes: []string{
-				"archive/tar",
-				"archive/zip",
-			},
-		},
-		{
-			name:           "stdlib package",
-			dirPath:        "archive/zip",
-			modulePath:     stdlib.ModulePath,
-			version:        internal.LatestVersion,
-			wantModulePath: stdlib.ModulePath,
-			wantVersion:    "v1.13.4",
-			wantSuffixes: []string{
-				"archive/zip",
-			},
-		},
-		{
-			name:            "stdlib package -  incomplete last element",
-			dirPath:         "archive/zi",
-			modulePath:      stdlib.ModulePath,
-			version:         internal.LatestVersion,
-			wantNotFoundErr: true,
-		},
-		{
-			name:           "stdlib - internal directory",
-			dirPath:        "cmd/internal",
-			modulePath:     stdlib.ModulePath,
-			version:        internal.LatestVersion,
-			wantModulePath: stdlib.ModulePath,
-			wantVersion:    "v1.13.4",
-			wantSuffixes: []string{
-				"cmd/internal/obj",
-				"cmd/internal/obj/arm",
-				"cmd/internal/obj/arm64",
-			},
-		},
-		{
-			name:           "stdlib - directory nested within an internal directory",
-			dirPath:        "cmd/internal/obj",
-			modulePath:     stdlib.ModulePath,
-			version:        internal.LatestVersion,
-			wantModulePath: stdlib.ModulePath,
-			wantVersion:    "v1.13.4",
-			wantSuffixes: []string{
-				"cmd/internal/obj",
-				"cmd/internal/obj/arm",
-				"cmd/internal/obj/arm64",
-			},
-		},
-	} {
-		t.Run(tc.name, func(t *testing.T) {
-			got, err := testDB.LegacyGetDirectory(ctx, tc.dirPath, tc.modulePath, tc.version, internal.AllFields)
-			if tc.wantNotFoundErr {
-				if !errors.Is(err, derrors.NotFound) {
-					t.Fatalf("got error %v; want %v", err, derrors.NotFound)
-				}
-				return
-			}
-			if err != nil {
-				t.Fatal(err)
-			}
-
-			mi := sample.LegacyModuleInfo(tc.wantModulePath, tc.wantVersion)
-			var wantPackages []*internal.LegacyPackage
-			for _, suffix := range tc.wantSuffixes {
-				pkg := sample.LegacyPackage(tc.wantModulePath, suffix)
-				pkg.Imports = nil
-				wantPackages = append(wantPackages, pkg)
-			}
-
-			wantDirectory := &internal.LegacyDirectory{
-				LegacyModuleInfo: *mi,
-				Packages:         wantPackages,
-				Path:             tc.dirPath,
-			}
-			opts := []cmp.Option{
-				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(wantDirectory, got, opts...); diff != "" {
-				t.Errorf("testDB.LegacyGetDirectory(ctx, %q, %q, %q) mismatch (-want +got):\n%s", tc.dirPath, tc.modulePath, tc.version, diff)
-			}
-		})
-	}
-}
-
-func TestLegacyGetDirectoryFieldSet(t *testing.T) {
-	ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
-	defer cancel()
-
-	defer ResetTestDB(testDB, t)
-
-	m := sample.Module("m.c", sample.VersionString, "d/p")
-	m.LegacyPackages[0].Imports = nil
-	if err := testDB.InsertModule(ctx, m); err != nil {
-		t.Fatal(err)
-	}
-
-	got, err := testDB.LegacyGetDirectory(ctx, "m.c/d", "m.c", sample.VersionString, internal.MinimalFields)
-	if err != nil {
-		t.Fatal(err)
-	}
-	if g, w := got.Packages[0].DocumentationHTML.String(), internal.StringFieldMissing; g != w {
-		t.Errorf("DocumentationHTML = %q, want %q", g, w)
-	}
-}
diff --git a/internal/postgres/unit_test.go b/internal/postgres/unit_test.go
index 8e387b1..d0ee8e6 100644
--- a/internal/postgres/unit_test.go
+++ b/internal/postgres/unit_test.go
@@ -341,17 +341,6 @@
 		if got := (pkgs[0].Synopsis == ""); got != test.wantEmpty {
 			t.Errorf("synopsis empty: got %t, want %t", got, test.wantEmpty)
 		}
-
-		ld, err := test.db.LegacyGetDirectory(ctx, m.ModulePath, m.ModulePath, m.Version, internal.AllFields)
-		if err != nil {
-			t.Fatal(err)
-		}
-		if got := (ld.Packages[0].Synopsis == ""); got != test.wantEmpty {
-			t.Errorf("legacy synopsis empty: got %t, want %t", got, test.wantEmpty)
-		}
-		if got := (ld.Packages[0].DocumentationHTML == safehtml.HTML{}); got != test.wantEmpty {
-			t.Errorf("legacy doc empty: got %t, want %t", got, test.wantEmpty)
-		}
 	}
 }