Skip to content

Commit

Permalink
fix: broken queries for Postgres Database (#634)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
fjogeleit authored Dec 16, 2024
1 parent 2c98eac commit 08bc37b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pkg/config/database_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func (f *DatabaseFactory) NewPostgres(config Database) *bun.DB {
sqldb.SetMaxIdleConns(25)
sqldb.SetConnMaxLifetime(15 * time.Minute)

return bun.NewDB(sqldb, pgdialect.New())
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook())

return db
}

func (f *DatabaseFactory) NewMySQL(config Database) *bun.DB {
Expand Down
17 changes: 10 additions & 7 deletions pkg/database/bun.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (s *Store) FetchResource(ctx context.Context, id string) (ResourceResult, e
Columns("res.id", "resource_uid", "resource_kind", "resource_api_version", "resource_namespace", "resource_name", "res.source", "res.category").
SelectStatusSummaries().
FilterValue("res.id", id).
Group("res.id", "resource_uid", "resource_kind", "resource_api_version", "resource_namespace", "resource_name", "res.source", "res.category").
GetQuery().
Limit(1).
Scan(ctx)
Expand Down Expand Up @@ -386,6 +387,7 @@ func (s *Store) FetchNamespaceResourceResults(ctx context.Context, filter Filter

func (s *Store) CountNamespaceResourceResults(ctx context.Context, filter Filter) (int, error) {
query := FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
Columns("res.id").
FilterMap(map[string][]string{
"source": filter.Sources,
"category": filter.Categories,
Expand Down Expand Up @@ -429,6 +431,7 @@ func (s *Store) FetchClusterResourceResults(ctx context.Context, filter Filter,

func (s *Store) CountClusterResourceResults(ctx context.Context, filter Filter) (int, error) {
query := FromQuery(s.db.NewSelect().Model((*ResourceResult)(nil))).
Columns("res.id").
FilterMap(map[string][]string{
"source": filter.Sources,
"category": filter.Categories,
Expand Down Expand Up @@ -656,14 +659,14 @@ func (s *Store) FetchNamespaceStatusCounts(ctx context.Context, source string, f
"f.resource_kind": filter.Kinds,
"f.resource_namespace": filter.Namespaces,
"f.policy": filter.Policies,
"status": filter.Status,
"f.result": filter.Status,
}).
FilterValue("f.source", source).
FilterReportLabels(filter.ReportLabel).
Exclude(filter, "f").
NamespaceScope().
Group("f.resource_namespace", "status").
Order("f.resource_namespace ASC", "status ASC").
Group("f.resource_namespace", "f.result").
Order("f.resource_namespace ASC", "f.result ASC").
Scan(ctx, &results)

return results, err
Expand Down Expand Up @@ -700,7 +703,7 @@ func (s *Store) FetchTotalStatusCounts(ctx context.Context, source string, filte
err := FromQuery(s.db.
NewSelect().
TableExpr("policy_report_filter as f").
ColumnExpr("SUM(f.count) as count, f.result as status")).
ColumnExpr("SUM(f.count) as count, f.result AS status")).
FilterMap(map[string][]string{
"category": filter.Categories,
"policy": filter.Policies,
Expand All @@ -709,7 +712,7 @@ func (s *Store) FetchTotalStatusCounts(ctx context.Context, source string, filte
FilterValue("f.source", source).
FilterReportLabels(filter.ReportLabel).
Exclude(filter, "f").
Group("status").
Group("f.result").
Scan(ctx, &results)

return results, err
Expand Down Expand Up @@ -810,11 +813,11 @@ func (s *Store) FetchFindingCounts(ctx context.Context, filter Filter) ([]Status
"category": filter.Categories,
"resource_kind": filter.Kinds,
"policy": filter.Policies,
"status": filter.Status,
"f.result": filter.Status,
}).
FilterReportLabels(filter.ReportLabel).
Exclude(filter, "f").
Group("f.source", "status").
Group("f.source", "f.result").
Order("f.source").
Scan(ctx, &results)

Expand Down

0 comments on commit 08bc37b

Please sign in to comment.