SQL: Add more legacy helpers (#92006)
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package legacysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
|
||||
)
|
||||
|
||||
// The database may depend on the request context
|
||||
type LegacyDatabaseProvider func(ctx context.Context) (*LegacyDatabaseHelper, error)
|
||||
|
||||
// NewDatabaseProvider returns a simple provider that always uses the same database implementation
|
||||
func NewDatabaseProvider(db db.DB) LegacyDatabaseProvider {
|
||||
helper := &LegacyDatabaseHelper{
|
||||
DB: db,
|
||||
Table: func(n string) string {
|
||||
return n
|
||||
},
|
||||
}
|
||||
return func(ctx context.Context) (*LegacyDatabaseHelper, error) {
|
||||
return helper, nil
|
||||
}
|
||||
}
|
||||
|
||||
type LegacyDatabaseHelper struct {
|
||||
// The database connection
|
||||
DB db.DB
|
||||
|
||||
// table name locator
|
||||
Table func(n string) string
|
||||
}
|
||||
|
||||
// Helper to pick the correct dialect
|
||||
func (h *LegacyDatabaseHelper) DialectForDriver() sqltemplate.Dialect {
|
||||
if h.DB == nil {
|
||||
return nil
|
||||
}
|
||||
return sqltemplate.DialectForDriver(string(h.DB.GetDBType()))
|
||||
}
|
||||
|
||||
// Get a resource version from the max value the updated field
|
||||
func (h *LegacyDatabaseHelper) GetResourceVersion(ctx context.Context, table string, column string) (int64, error) {
|
||||
table = h.Table(table)
|
||||
column = h.DB.Quote(column)
|
||||
|
||||
var rv int64
|
||||
_ = h.DB.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
v := struct {
|
||||
M time.Time `db:"m"` // xorm date parsing magic
|
||||
}{}
|
||||
ok, err := sess.Table(h.Table(table)).Select("MAX(" + column + ") as m").Get(&v)
|
||||
if ok {
|
||||
rv = v.M.UnixMilli()
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
// When no RV, use a stable non-zero number
|
||||
if rv < 1 {
|
||||
return startup, nil
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
var startup = time.Now().UnixMilli()
|
||||
@@ -1,51 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user