CloudMigrations: Make table sort case insensitive (#103898)

* case insensitive sort

* fix for all db types

* add comment

* add unit test

* add a TODO to fix later
This commit is contained in:
Michael Mandrus
2025-04-11 20:29:07 +01:00
committed by GitHub
parent 5efb620f1b
commit 652c374c4c
2 changed files with 79 additions and 1 deletions
@@ -12,6 +12,7 @@ import (
"github.com/grafana/grafana/pkg/services/secrets"
secretskv "github.com/grafana/grafana/pkg/services/secrets/kvstore"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/util"
)
@@ -434,7 +435,13 @@ func (ss *sqlStore) getSnapshotResources(ctx context.Context, snapshotUid string
if errorsOnly {
sess.Where("status = ?", cloudmigration.ItemStatusError)
}
return sess.OrderBy(fmt.Sprintf("%s %s", col, dir)).Find(&resources, &cloudmigration.CloudMigrationResource{
// TODO: It would be better if the query builder supported a case-insensitive flag for the .OrderBy() method
orderByClause := fmt.Sprintf("lower(%s) %s", col, dir)
if ss.db.GetDBType() == migrator.Postgres || // Postgres does not support lower() in ORDER BY -- sorts by case-insensitive by default
params.SortColumn == cloudmigration.SortColumnID { // Don't apply a string sort to a numeric column
orderByClause = fmt.Sprintf("%s %s", col, dir)
}
return sess.OrderBy(orderByClause).Find(&resources, &cloudmigration.CloudMigrationResource{
SnapshotUID: snapshotUid,
})
})