From ff76d5b95b38e11ebb470fe6b73294a4fcc83865 Mon Sep 17 00:00:00 2001 From: Patrik Segedy Date: Tue, 18 Jun 2024 15:50:07 +0200 Subject: [PATCH] RHINENG-10791: common package join --- base/database/utils.go | 17 +++++++++++------ evaluator/package_cache.go | 6 ++---- manager/controllers/package_detail.go | 4 ++-- manager/controllers/package_systems.go | 8 +------- manager/controllers/package_systems_export.go | 3 ++- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/base/database/utils.go b/base/database/utils.go index 51bb0e031..4c289ca7a 100644 --- a/base/database/utils.go +++ b/base/database/utils.go @@ -54,15 +54,13 @@ func SystemPackages(tx *gorm.DB, accountID int, groups map[string]string, joins func Packages(tx *gorm.DB, joins ...join) *gorm.DB { tx = tx.Table("package p"). - Joins("JOIN package_name pn on p.name_id = pn.id"). - Joins("JOIN strings descr ON p.description_hash = descr.id"). - Joins("JOIN strings sum ON p.summary_hash = sum.id"). - Joins("LEFT JOIN advisory_metadata am ON p.advisory_id = am.id") + Joins("JOIN package_name pn on p.name_id = pn.id") return (joinsT)(joins).apply(tx) } -func PackageByName(tx *gorm.DB, pkgName string) *gorm.DB { - return Packages(tx).Where("pn.name = ?", pkgName) +func PackageByName(tx *gorm.DB, pkgName string, joins ...join) *gorm.DB { + tx = Packages(tx).Where("pn.name = ?", pkgName) + return (joinsT)(joins).apply(tx) } func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID string, @@ -278,3 +276,10 @@ func JoinInstallableApplicablePackages(tx *gorm.DB) *gorm.DB { return tx.Joins("LEFT JOIN package pi ON pi.id = spkg.installable_id"). Joins("LEFT JOIN package pa ON pa.id = spkg.applicable_id") } + +// JOIN package description, summary, advisory +func JoinPackageDetails(tx *gorm.DB) *gorm.DB { + return tx.Joins("JOIN strings descr ON p.description_hash = descr.id"). + Joins("JOIN strings sum ON p.summary_hash = sum.id"). + Joins("LEFT JOIN advisory_metadata am ON p.advisory_id = am.id") +} diff --git a/evaluator/package_cache.go b/evaluator/package_cache.go index 47b28355b..cc351ef2b 100644 --- a/evaluator/package_cache.go +++ b/evaluator/package_cache.go @@ -82,9 +82,8 @@ func (c *PackageCache) Load() { defer tx.Rollback() // load N last recently added packages, i.e. newest - rows, err := tx.Table("package p"). + rows, err := database.Packages(tx). Select("p.id, p.name_id, pn.name, p.evra, p.summary_hash, p.description_hash"). - Joins("JOIN package_name pn ON pn.id = p.name_id"). Order("id DESC"). Limit(c.size). Rows() @@ -263,9 +262,8 @@ func readPackageFromDB(where string, order string, args ...interface{}) *Package defer tx.Rollback() var pkg PackageCacheMetadata - query := tx.Table("package p"). + query := database.Packages(tx). Select("p.id, p.name_id, pn.name, p.evra, p.summary_hash, p.description_hash"). - Joins("JOIN package_name pn ON pn.id = p.name_id"). Where(where, args...) if order != "" { query = query.Order(order) diff --git a/manager/controllers/package_detail.go b/manager/controllers/package_detail.go index e0239b5ae..171411278 100644 --- a/manager/controllers/package_detail.go +++ b/manager/controllers/package_detail.go @@ -44,7 +44,7 @@ var PackageSelect = database.MustGetSelect(&PackageDetailAttributes{}) func packageLatestHandler(c *gin.Context, packageName string) { db := middlewares.DBFromContext(c) - query := database.PackageByName(db, packageName) + query := database.PackageByName(db, packageName, database.JoinPackageDetails) var pkg PackageDetailAttributes // Perform 'soft-filtering' by ordering on boolean column first err := query.Select(PackageSelect). @@ -72,7 +72,7 @@ func packageEvraHandler(c *gin.Context, nevra *utils.Nevra) { return } - query := database.PackageByName(db, nevra.Name) + query := database.PackageByName(db, nevra.Name, database.JoinPackageDetails) var pkg PackageDetailAttributes err := query.Select(PackageSelect).Where("p.evra = ?", nevra.EVRAString()).Take(&pkg).Error if err != nil { diff --git a/manager/controllers/package_systems.go b/manager/controllers/package_systems.go index 38e005f9e..8dc721720 100644 --- a/manager/controllers/package_systems.go +++ b/manager/controllers/package_systems.go @@ -54,12 +54,6 @@ type PackageSystemsResponse struct { Meta ListMeta `json:"meta"` } -func packagesByNameQuery(db *gorm.DB, pkgName string) *gorm.DB { - return db.Table("package p"). - Joins("INNER JOIN package_name pn ON p.name_id = pn.id"). - Where("pn.name = ?", pkgName) -} - func packageSystemsQuery(db *gorm.DB, acc int, groups map[string]string, packageName string, packageIDs []int, ) *gorm.DB { query := database.SystemPackages(db, acc, groups, @@ -83,7 +77,7 @@ func packageSystemsCommon(db *gorm.DB, c *gin.Context) (*gorm.DB, *ListMeta, []s } var packageIDs []int - if err := packagesByNameQuery(db, packageName).Pluck("p.id", &packageIDs).Error; err != nil { + if err := database.PackageByName(db, packageName).Pluck("p.id", &packageIDs).Error; err != nil { LogAndRespError(c, err, "database error") return nil, nil, nil, err } diff --git a/manager/controllers/package_systems_export.go b/manager/controllers/package_systems_export.go index ad94aa62c..69baef804 100644 --- a/manager/controllers/package_systems_export.go +++ b/manager/controllers/package_systems_export.go @@ -1,6 +1,7 @@ package controllers import ( + "app/base/database" "app/base/utils" "app/manager/middlewares" "errors" @@ -42,7 +43,7 @@ func PackageSystemsExportHandler(c *gin.Context) { db := middlewares.DBFromContext(c) var packageIDs []int - if err := packagesByNameQuery(db, packageName).Pluck("p.id", &packageIDs).Error; err != nil { + if err := database.PackageByName(db, packageName).Pluck("p.id", &packageIDs).Error; err != nil { LogAndRespError(c, err, "database error") return }