Alerting: Collate rule_group column as binary (#114365)
* fix: collate rule_group column as utf8mb4_bin This ensures this column is sorted identically by the database and golang, which should eliminate issues related to a mismatch in sort order. * chore: make test stricter * fix: also add binary collation for postgres
This commit is contained in:
@@ -1516,11 +1516,9 @@ func TestIntegrationListAlertRulesByGroupCaseSensitiveOrdering(t *testing.T) {
|
||||
}
|
||||
|
||||
// Verify case-sensitive alphabetical ordering
|
||||
// different databases may sort uppercase before lowercase or vice versa depending on character set, the important part is that the order is consistent and case-sensitive
|
||||
expectedOrder := []string{"test", "Test", "TEST"}
|
||||
alternateExpectedOrder := []string{"TEST", "Test", "test"}
|
||||
if !slices.Equal(groupOrder, expectedOrder) && !slices.Equal(groupOrder, alternateExpectedOrder) {
|
||||
t.Fatalf("groups are not ordered case-sensitively as expected. got: %v, want: %v or %v", groupOrder, expectedOrder, alternateExpectedOrder)
|
||||
expectedOrder := []string{"TEST", "Test", "test"}
|
||||
if !slices.Equal(groupOrder, expectedOrder) {
|
||||
t.Fatalf("groups are not ordered case-sensitively as expected. got: %v, want: %v", groupOrder, expectedOrder)
|
||||
}
|
||||
|
||||
// Verify each group contains the correct rules
|
||||
|
||||
@@ -162,4 +162,6 @@ func (oss *OSSMigrations) AddMigration(mg *Migrator) {
|
||||
ualert.ExpandAlertRuleUpdatedByMigration(mg)
|
||||
|
||||
ualert.AddAlertRuleGroupIndexMigration(mg)
|
||||
|
||||
ualert.CollateBinAlertRuleGroup(mg)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package ualert
|
||||
|
||||
import "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
// CollateBinAlertRuleGroup ensures that rule_group column collates in the same way go sorts strings.
|
||||
func CollateBinAlertRuleGroup(mg *migrator.Migrator) {
|
||||
mg.AddMigration("ensure rule_group column sorts the same way as golang", migrator.NewRawSQLMigration("").
|
||||
Mysql("ALTER TABLE alert_rule MODIFY rule_group VARCHAR(190) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;").
|
||||
Postgres(`ALTER TABLE alert_rule ALTER COLUMN rule_group SET DATA TYPE varchar(190) COLLATE "C";`))
|
||||
}
|
||||
Reference in New Issue
Block a user