From cce335c42ffa5524101fcaa25ecbb112397f43a9 Mon Sep 17 00:00:00 2001 From: SimoneDutto Date: Fri, 6 Sep 2024 15:12:26 +0200 Subject: [PATCH] change from where condition to case --- internal/db/resource.go | 14 +++++--------- internal/rebac_admin/resources_integration_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/db/resource.go b/internal/db/resource.go index d8b5500bc..7eaab7749 100644 --- a/internal/db/resource.go +++ b/internal/db/resource.go @@ -116,31 +116,27 @@ func (d *Database) ListResources(ctx context.Context, limit, offset int, namePre // namePrefixFilter: used to match resources name prefix. // typeFilter: used to match resources type. If this is not empty the resources are fetched from a single table. func buildQuery(db *gorm.DB, offset, limit int, namePrefixFilter, typeFilter string) (*gorm.DB, error) { - // if namePrefixEmpty set to true we have a 'TRUE IS TRUE' in the SQL WHERE statement, which disable the filtering. - namePrefixEmpty := namePrefixFilter == "" - namePrefixFilter += "%" - applicationOffersQuery := db.Select(selectApplicationOffers). Model(&dbmodel.ApplicationOffer{}). - Where("? IS TRUE OR application_offers.name LIKE ?", namePrefixEmpty, namePrefixFilter). + Where("(CASE WHEN ? = '' THEN TRUE ELSE application_offers.name LIKE ? END)", namePrefixFilter, namePrefixFilter+"%"). Joins("JOIN models ON application_offers.model_id = models.id") cloudsQuery := db.Select(selectClouds). Model(&dbmodel.Cloud{}). - Where("? IS TRUE OR clouds.name LIKE ?", namePrefixEmpty, namePrefixFilter) + Where("(CASE WHEN ? = '' THEN TRUE ELSE clouds.name LIKE ? END)", namePrefixFilter, namePrefixFilter+"%") controllersQuery := db.Select(selectControllers). Model(&dbmodel.Controller{}). - Where("? IS TRUE OR controllers.name LIKE ?", namePrefixEmpty, namePrefixFilter) + Where("(CASE WHEN ? = '' THEN TRUE ELSE controllers.name LIKE ? END)", namePrefixFilter, namePrefixFilter+"%") modelsQuery := db.Select(selectModels). Model(&dbmodel.Model{}). - Where("? IS TRUE OR models.name LIKE ?", namePrefixEmpty, namePrefixFilter). + Where("(CASE WHEN ? = '' THEN TRUE ELSE models.name LIKE ? END)", namePrefixFilter, namePrefixFilter+"%"). Joins("JOIN controllers ON models.controller_id = controllers.id") serviceAccountsQuery := db.Select(selectIdentities). Model(&dbmodel.Identity{}). - Where("name LIKE '%@serviceaccount' AND (? IS TRUE OR identities.name LIKE ?)", namePrefixEmpty, namePrefixFilter) + Where("name LIKE '%@serviceaccount' AND (CASE WHEN ? = '' THEN TRUE ELSE identities.name LIKE ? END)", namePrefixFilter, namePrefixFilter+"%") // if the typeFilter is set we only return the query for that specif entityType, otherwise the union. if typeFilter == "" { diff --git a/internal/rebac_admin/resources_integration_test.go b/internal/rebac_admin/resources_integration_test.go index a22c9d15f..fc2344110 100644 --- a/internal/rebac_admin/resources_integration_test.go +++ b/internal/rebac_admin/resources_integration_test.go @@ -192,6 +192,16 @@ func (s *resourcesSuite) TestListResources(c *gc.C) { return filteredIds }(), }, + { + desc: "test big page with model entity type", + size: utils.IntToPointer(10), + page: utils.IntToPointer(0), + typeFilter: rebac_admin.Model, + wantPage: 0, + wantSize: 3, + wantNextpage: nil, + ids: []testEntity{}, + }, } for _, t := range testCases { resources, err := resourcesSvc.ListResources(ctx, &resources.GetResourcesParams{