diff options
| author | Jonathan Amsterdam <jba@google.com> | 2021-06-14 08:34:13 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2021-06-14 15:29:59 +0000 |
| commit | b23d3c6e62bb91fdbeb0bfaac12e7cd359b0b553 (patch) | |
| tree | b164e6198e8e9bd652ed2defeb44c0c12c650a67 /internal/postgres/insert_module.go | |
| parent | 06a364cdacf24055dbd0a5c4d24897ea007f550a (diff) | |
| download | go-x-pkgsite-b23d3c6e62bb91fdbeb0bfaac12e7cd359b0b553.tar.xz | |
internal/postgres: don't touch package_imports
If the read-imports experiment is on, replace all references to the
package_imports table with the imports table: don't write into or read
from package_imports.
Change-Id: I02631aba574b9938a44b06bce855997606984939
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/327749
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/postgres/insert_module.go')
| -rw-r--r-- | internal/postgres/insert_module.go | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go index 0643d2d5..70b06868 100644 --- a/internal/postgres/insert_module.go +++ b/internal/postgres/insert_module.go @@ -23,6 +23,7 @@ import ( "golang.org/x/pkgsite/internal" "golang.org/x/pkgsite/internal/database" "golang.org/x/pkgsite/internal/derrors" + "golang.org/x/pkgsite/internal/experiment" "golang.org/x/pkgsite/internal/licenses" "golang.org/x/pkgsite/internal/log" "golang.org/x/pkgsite/internal/stdlib" @@ -581,24 +582,25 @@ func insertImports(ctx context.Context, tx *database.DB, pathToImports map[string][]string) (err error) { defer derrors.WrapStack(&err, "insertImports") - // Insert into package_imports. This table is going to go away soon, - // but for now it is still the table we read from. - var pkgImportValues []interface{} - for _, pkgPath := range paths { - imports, ok := pathToImports[pkgPath] - if !ok { - continue + if !experiment.IsActive(ctx, internal.ExperimentReadImports) { + // Insert into package_imports. This table is going to go away soon, + // but for now it is still the table we read from. + var pkgImportValues []interface{} + for _, pkgPath := range paths { + imports, ok := pathToImports[pkgPath] + if !ok { + continue + } + unitID := pathToUnitID[pkgPath] + for _, toPath := range imports { + pkgImportValues = append(pkgImportValues, unitID, toPath) + } } - unitID := pathToUnitID[pkgPath] - for _, toPath := range imports { - pkgImportValues = append(pkgImportValues, unitID, toPath) + pkgImportCols := []string{"unit_id", "to_path"} + if err := tx.BulkUpsert(ctx, "package_imports", pkgImportCols, pkgImportValues, pkgImportCols); err != nil { + return err } } - pkgImportCols := []string{"unit_id", "to_path"} - if err := tx.BulkUpsert(ctx, "package_imports", pkgImportCols, pkgImportValues, pkgImportCols); err != nil { - return err - } - // Insert into imports. This will eventually replace package_imports. importPathSet := map[string]bool{} for _, pkgPath := range paths { @@ -752,16 +754,31 @@ func (db *DB) ReInsertLatestVersion(ctx context.Context, modulePath string) (err } // Insert this version's imports into imports_unique. - if _, err := tx.Exec(ctx, ` - INSERT INTO imports_unique (from_path, from_module_path, to_path) - SELECT p.path, m.module_path, i.to_path - FROM units u - INNER JOIN package_imports i ON (u.id = i.unit_id) - INNER JOIN paths p ON (p.id = u.path_id) - INNER JOIN modules m ON (m.id=u.module_id) - WHERE m.module_path = $1 and m.version = $2 + if experiment.IsActive(ctx, internal.ExperimentReadImports) { + if _, err := tx.Exec(ctx, ` + INSERT INTO imports_unique (from_path, from_module_path, to_path) + SELECT p1.path, m.module_path, p2.path + FROM units u + INNER JOIN imports i ON u.id = i.unit_id + INNER JOIN paths p1 ON p1.id = u.path_id + INNER JOIN modules m ON m.id = u.module_id + INNER JOIN paths p2 ON p2.id = i.to_path_id + WHERE m.module_path = $1 and m.version = $2 `, modulePath, lmv.GoodVersion); err != nil { - return err + return err + } + } else { + if _, err := tx.Exec(ctx, ` + INSERT INTO imports_unique (from_path, from_module_path, to_path) + SELECT p.path, m.module_path, i.to_path + FROM units u + INNER JOIN package_imports i ON (u.id = i.unit_id) + INNER JOIN paths p ON (p.id = u.path_id) + INNER JOIN modules m ON (m.id=u.module_id) + WHERE m.module_path = $1 and m.version = $2 + `, modulePath, lmv.GoodVersion); err != nil { + return err + } } log.Debugf(ctx, "ReInsertLatestVersion(%q): re-inserted at latest good version %s", modulePath, lmv.GoodVersion) |
