Identity: Add endpoint to get display info for an identifier (#91828)

This commit is contained in:
Ryan McKinley
2024-08-15 14:38:43 +03:00
committed by GitHub
parent c7fdf8ce70
commit a0cd89860e
67 changed files with 1535 additions and 282 deletions
+8
View File
@@ -0,0 +1,8 @@
# Legacy SQL
As we transition from our internal sql store towards unified storage, we can sometimes use existing
services to implement a k8s compatible storage that can then dual write into unified storage.
However sometimes it is more efficient and cleaner to write explicit SQL commands designed for this goal.
This package provides some helper functions to make this easier.
+51
View File
@@ -0,0 +1,51 @@
package legacysql
import (
"context"
"time"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
// The database may depend on the request context
type NamespacedDBProvider func(ctx context.Context) (db.DB, error)
// Get the list RV from the maximum updated time
type ResourceVersionLookup = func(ctx context.Context) (int64, error)
// Get a resource version from the max value the updated field
func GetResourceVersionLookup(sql NamespacedDBProvider, table string, column string) ResourceVersionLookup {
return func(ctx context.Context) (int64, error) {
db, err := sql(ctx)
if err != nil {
return 1, err
}
table = db.GetDialect().Quote(table)
column = db.GetDialect().Quote(column)
switch db.GetDBType() {
case migrator.Postgres:
max := time.Now()
err := db.GetSqlxSession().Get(ctx, &max, "SELECT MAX("+column+") FROM "+table)
if err != nil {
return 1, nil
}
return max.UnixMilli(), nil
case migrator.MySQL:
max := int64(1)
_ = db.GetSqlxSession().Get(ctx, &max, "SELECT UNIX_TIMESTAMP(MAX("+column+")) FROM "+table)
return max, nil
default:
// fallthrough to string version
}
max := ""
err = db.GetSqlxSession().Get(ctx, &max, "SELECT MAX("+column+") FROM "+table)
if err == nil && max != "" {
t, _ := time.Parse(time.DateTime, max) // ignore null errors
return t.UnixMilli(), nil
}
return 1, nil
}
}