internal/postgres: delete old versions from search_documents on insert

When inserting the latest version of a module, delete all versions
from search_documents before inserting the latest one. That will
remove from search all packages that are not in the latest version.

Change-Id: I58fe9a1e8974d7f3fbd1fa64212c4b0381895d09
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/337751
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Jonathan Amsterdam 2021-07-27 11:06:29 -04:00
Родитель e281c2aa07
Коммит bc1baae772
3 изменённых файлов: 13 добавлений и 7 удалений

Просмотреть файл

@ -29,10 +29,9 @@ func TestCleanBulk(t *testing.T) {
}
for _, mv := range append(want,
// These should not be cleaned.
"a.c@v1.0.0", // tagged
"b.c@v0.0.0-20170101000000-abcdef012345/p", // p is in search_documents (must be inserted first)
"b.c@v0.0.0-20190101000000-abcdef012345", // latest version
"b.c@v0.0.0-20180101000000-abcdef012345", // 'main' in version_map (see UpsertVersionMap below)
"a.c@v1.0.0", // tagged
"b.c@v0.0.0-20190101000000-abcdef012345", // latest version
"b.c@v0.0.0-20180101000000-abcdef012345", // 'main' in version_map (see UpsertVersionMap below)
) {
mod, ver, pkg := parseModuleVersionPackage(mv)
m := sample.Module(mod, ver, pkg)

Просмотреть файл

@ -149,6 +149,13 @@ func (db *DB) saveModule(ctx context.Context, m *internal.Module, lmv *internal.
return err
}
// Delete this module from search_documents completely. Below we'll
// insert the packages from this module. This will effectively remove
// packages from older module versions that are not in the latest one.
if err := deleteModuleFromSearchDocuments(ctx, tx, m.ModulePath, nil); err != nil {
return err
}
// If the most recent version of this module has an alternative module
// path, then do not insert its packages into search_documents (and
// delete whatever is there). This happens when a module that initially
@ -176,7 +183,7 @@ func (db *DB) saveModule(ctx context.Context, m *internal.Module, lmv *internal.
}
if alt {
log.Infof(ctx, "%s@%s: not inserting into search documents", m.ModulePath, m.Version)
return deleteModuleFromSearchDocuments(ctx, tx, m.ModulePath, nil)
return nil
}
// Insert the module's packages into search_documents.
if err := upsertSearchDocuments(ctx, tx, m); err != nil {

Просмотреть файл

@ -1300,7 +1300,6 @@ func TestDeleteFromSearch(t *testing.T) {
const modulePath = "deleteme.com"
initial := []searchDocumentRow{
// These must be from oldest to newest version, or older ones won't be inserted.
{modulePath + "/p1", modulePath, "v0.0.9"}, // oldest version of same module
{modulePath + "/p2", modulePath, "v1.1.0"}, // older version of same module
{modulePath + "/p4", modulePath, "v1.9.0"}, // newer version of same module
@ -1313,7 +1312,8 @@ func TestDeleteFromSearch(t *testing.T) {
sm := sample.Module(r.ModulePath, r.Version, strings.TrimPrefix(r.PackagePath, r.ModulePath+"/"))
MustInsertModule(ctx, t, db, sm)
}
checkSearchDocuments(ctx, t, db, initial)
// Older versions are deleted by InsertModule, so only the newest versions are here.
checkSearchDocuments(ctx, t, db, initial[2:])
}
t.Run("DeleteOlderVersionFromSearchDocuments", func(t *testing.T) {