зеркало из https://github.com/golang/pkgsite.git
internal/postgres: move DeleteModule
DeleteModule is moved to delete.go. Pure code in motion. Change-Id: I2a9ab37ce4b5dd584d4163f6bf2cb2e81565c69c Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/339151 Trust: Julie Qiu <julie@golang.org> Run-TryBot: Julie Qiu <julie@golang.org> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Родитель
88f4e70d72
Коммит
dc06dc1217
|
@ -13,6 +13,34 @@ import (
|
|||
"golang.org/x/pkgsite/internal/derrors"
|
||||
)
|
||||
|
||||
// DeleteModule deletes a Version from the database.
|
||||
func (db *DB) DeleteModule(ctx context.Context, modulePath, resolvedVersion string) (err error) {
|
||||
defer derrors.WrapStack(&err, "DeleteModule(ctx, db, %q, %q)", modulePath, resolvedVersion)
|
||||
return db.db.Transact(ctx, sql.LevelDefault, func(tx *database.DB) error {
|
||||
// We only need to delete from the modules table. Thanks to ON DELETE
|
||||
// CASCADE constraints, that will trigger deletions from all other tables.
|
||||
const stmt = `DELETE FROM modules WHERE module_path=$1 AND version=$2`
|
||||
if _, err := tx.Exec(ctx, stmt, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM version_map WHERE module_path = $1 AND resolved_version = $2`, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM search_documents WHERE module_path = $1 AND version = $2`, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var x int
|
||||
err = tx.QueryRow(ctx, `SELECT 1 FROM modules WHERE module_path=$1 LIMIT 1`, modulePath).Scan(&x)
|
||||
if err != sql.ErrNoRows || err == nil {
|
||||
return err
|
||||
}
|
||||
// No versions of this module exist; remove it from imports_unique.
|
||||
_, err = tx.Exec(ctx, `DELETE FROM imports_unique WHERE from_module_path = $1`, modulePath)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePseudoversionsExcept deletes all pseudoversions for the module except
|
||||
// the provided resolvedVersion.
|
||||
func (db *DB) DeletePseudoversionsExcept(ctx context.Context, modulePath, resolvedVersion string) (err error) {
|
||||
|
|
|
@ -872,34 +872,6 @@ func (db *DB) comparePaths(ctx context.Context, m *internal.Module) (err error)
|
|||
return nil
|
||||
}
|
||||
|
||||
// DeleteModule deletes a Version from the database.
|
||||
func (db *DB) DeleteModule(ctx context.Context, modulePath, resolvedVersion string) (err error) {
|
||||
defer derrors.WrapStack(&err, "DeleteModule(ctx, db, %q, %q)", modulePath, resolvedVersion)
|
||||
return db.db.Transact(ctx, sql.LevelDefault, func(tx *database.DB) error {
|
||||
// We only need to delete from the modules table. Thanks to ON DELETE
|
||||
// CASCADE constraints, that will trigger deletions from all other tables.
|
||||
const stmt = `DELETE FROM modules WHERE module_path=$1 AND version=$2`
|
||||
if _, err := tx.Exec(ctx, stmt, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM version_map WHERE module_path = $1 AND resolved_version = $2`, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM search_documents WHERE module_path = $1 AND version = $2`, modulePath, resolvedVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var x int
|
||||
err = tx.QueryRow(ctx, `SELECT 1 FROM modules WHERE module_path=$1 LIMIT 1`, modulePath).Scan(&x)
|
||||
if err != sql.ErrNoRows || err == nil {
|
||||
return err
|
||||
}
|
||||
// No versions of this module exist; remove it from imports_unique.
|
||||
_, err = tx.Exec(ctx, `DELETE FROM imports_unique WHERE from_module_path = $1`, modulePath)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// makeValidUnicode removes null runes from a string that will be saved in a
|
||||
// column of type TEXT, because pq doesn't like them. It also replaces non-unicode
|
||||
// characters with the Unicode replacement character, which is the behavior of
|
||||
|
|
Загрузка…
Ссылка в новой задаче