Skip to content

Commit

Permalink
RHINENG-10791: common package join
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Jul 1, 2024
1 parent c20584d commit fcce7ac
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
17 changes: 11 additions & 6 deletions base/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
6 changes: 2 additions & 4 deletions evaluator/package_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/package_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions manager/controllers/package_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion manager/controllers/package_systems_export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"app/base/database"
"app/base/utils"
"app/manager/middlewares"
"errors"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit fcce7ac

Please sign in to comment.