From c319341ec525f3a68639cab8d6e89e09795387bf Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 27 Sep 2024 07:50:24 +0000 Subject: [PATCH 1/2] Do a case insensitive search for tags This change switches to a case insensitive search for pool tags. Signed-off-by: Gabriel Adrian Samfira --- database/sql/pools.go | 2 +- database/sql/util.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/database/sql/pools.go b/database/sql/pools.go index 0cb4a094..fdcf3f5a 100644 --- a/database/sql/pools.go +++ b/database/sql/pools.go @@ -206,7 +206,7 @@ func (s *sqlDatabase) findPoolByTags(id string, poolType params.GithubEntityType } var pools []Pool - where := fmt.Sprintf("tags.name in ? and %s = ? and enabled = true", fieldName) + where := fmt.Sprintf("tags.name COLLATE NOCASE in ? and %s = ? and enabled = true", fieldName) q := s.conn.Joins("JOIN pool_tags on pool_tags.pool_id=pools.id"). Joins("JOIN tags on tags.id=pool_tags.tag_id"). Group("pools.id"). diff --git a/database/sql/util.go b/database/sql/util.go index 063c68a6..0b47b962 100644 --- a/database/sql/util.go +++ b/database/sql/util.go @@ -331,7 +331,7 @@ func (s *sqlDatabase) sqlToParamsUser(user User) params.User { func (s *sqlDatabase) getOrCreateTag(tx *gorm.DB, tagName string) (Tag, error) { var tag Tag - q := tx.Where("name = ?", tagName).First(&tag) + q := tx.Where("name = ? COLLATE NOCASE", tagName).First(&tag) if q.Error == nil { return tag, nil } From e5fd70254487c4c468bd7dd91fb614c361f8cc67 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Fri, 27 Sep 2024 08:05:43 +0000 Subject: [PATCH 2/2] Fix tests Signed-off-by: Gabriel Adrian Samfira --- database/sql/enterprise_test.go | 8 ++++---- database/sql/organizations_test.go | 8 ++++---- database/sql/repositories_test.go | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/database/sql/enterprise_test.go b/database/sql/enterprise_test.go index 9e1a86dd..b7e2ddbb 100644 --- a/database/sql/enterprise_test.go +++ b/database/sql/enterprise_test.go @@ -507,7 +507,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBFetchTagErr() { WithArgs(s.Fixtures.Enterprises[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Enterprises[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnError(fmt.Errorf("mocked fetching tag error")) entity, err := s.Fixtures.Enterprises[0].GetEntity() @@ -527,7 +527,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBAddingPoolErr() { WithArgs(s.Fixtures.Enterprises[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Enterprises[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -555,7 +555,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBSaveTagErr() { WithArgs(s.Fixtures.Enterprises[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Enterprises[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -586,7 +586,7 @@ func (s *EnterpriseTestSuite) TestCreateEnterprisePoolDBFetchPoolErr() { WithArgs(s.Fixtures.Enterprises[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Enterprises[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). diff --git a/database/sql/organizations_test.go b/database/sql/organizations_test.go index 11be72d0..ba24bb98 100644 --- a/database/sql/organizations_test.go +++ b/database/sql/organizations_test.go @@ -509,7 +509,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBFetchTagErr() { WithArgs(s.Fixtures.Orgs[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Orgs[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnError(fmt.Errorf("mocked fetching tag error")) entity, err := s.Fixtures.Orgs[0].GetEntity() @@ -530,7 +530,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBAddingPoolErr() { WithArgs(s.Fixtures.Orgs[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Orgs[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -558,7 +558,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBSaveTagErr() { WithArgs(s.Fixtures.Orgs[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Orgs[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -589,7 +589,7 @@ func (s *OrgTestSuite) TestCreateOrganizationPoolDBFetchPoolErr() { WithArgs(s.Fixtures.Orgs[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Orgs[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). diff --git a/database/sql/repositories_test.go b/database/sql/repositories_test.go index 313396f8..90fb3402 100644 --- a/database/sql/repositories_test.go +++ b/database/sql/repositories_test.go @@ -551,7 +551,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBFetchTagErr() { WithArgs(s.Fixtures.Repos[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Repos[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnError(fmt.Errorf("mocked fetching tag error")) entity, err := s.Fixtures.Repos[0].GetEntity() @@ -573,7 +573,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBAddingPoolErr() { WithArgs(s.Fixtures.Repos[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Repos[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -602,7 +602,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBSaveTagErr() { WithArgs(s.Fixtures.Repos[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Repos[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")). @@ -633,7 +633,7 @@ func (s *RepoTestSuite) TestCreateRepositoryPoolDBFetchPoolErr() { WithArgs(s.Fixtures.Repos[0].ID, 1). WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(s.Fixtures.Repos[0].ID)) s.Fixtures.SQLMock. - ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). + ExpectQuery(regexp.QuoteMeta("SELECT * FROM `tags` WHERE name = ? COLLATE NOCASE AND `tags`.`deleted_at` IS NULL ORDER BY `tags`.`id` LIMIT ?")). WillReturnRows(sqlmock.NewRows([]string{"linux"})) s.Fixtures.SQLMock. ExpectExec(regexp.QuoteMeta("INSERT INTO `tags`")).