2022-10-19 08:02:15 -05:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"xorm.io/core"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DB interface {
|
2023-07-31 10:19:59 -05:00
|
|
|
// WithTransactionalDbSession creates a new SQL transaction to ensure consistency
|
|
|
|
// for the database operations done within the [sqlstore.DBTransactionFunc].
|
|
|
|
// It's better to combine InTransaction and WithDbSession instead, as the context
|
|
|
|
// variable is not updated when using this method.
|
2022-10-19 08:02:15 -05:00
|
|
|
WithTransactionalDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
|
2023-07-31 10:19:59 -05:00
|
|
|
// WithDbSession runs database operations either in an existing transaction available
|
|
|
|
// through [context.Context] or if that's not present, as non-transactional database
|
|
|
|
// operations.
|
2022-10-19 08:02:15 -05:00
|
|
|
WithDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
|
2023-07-31 10:19:59 -05:00
|
|
|
// WithNewDbSession behaves like [DB.WithDbSession] without picking up a transaction
|
|
|
|
// from the context.
|
2022-10-19 08:02:15 -05:00
|
|
|
WithNewDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
|
2023-07-31 10:19:59 -05:00
|
|
|
// GetDialect returns an object that contains information about the peculiarities of
|
|
|
|
// the particular database type available to the runtime.
|
2022-10-19 08:02:15 -05:00
|
|
|
GetDialect() migrator.Dialect
|
2023-07-31 10:19:59 -05:00
|
|
|
// GetDBType returns the name of the database type available to the runtime.
|
2022-10-19 08:02:15 -05:00
|
|
|
GetDBType() core.DbType
|
2023-07-31 10:19:59 -05:00
|
|
|
// GetSqlxSession is an experimental extension to use sqlx instead of xorm to
|
|
|
|
// communicate with the database.
|
2022-10-19 08:02:15 -05:00
|
|
|
GetSqlxSession() *session.SessionDB
|
2023-07-31 10:19:59 -05:00
|
|
|
// InTransaction creates a new SQL transaction that is placed on the context.
|
|
|
|
// Use together with [DB.WithDbSession] to run database operations.
|
2022-10-19 08:02:15 -05:00
|
|
|
InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
|
2023-07-31 10:19:59 -05:00
|
|
|
// Quote wraps an identifier so that it cannot be mistaken for an SQL keyword.
|
2022-11-23 11:40:58 -06:00
|
|
|
Quote(value string) string
|
2023-03-20 03:27:08 -05:00
|
|
|
// RecursiveQueriesAreSupported runs a dummy recursive query and it returns true
|
|
|
|
// if the query runs successfully or false if it fails with mysqlerr.ER_PARSE_ERROR error or any other error
|
|
|
|
RecursiveQueriesAreSupported() (bool, error)
|
2022-10-19 08:02:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Session = sqlstore.DBSession
|
|
|
|
type InitTestDBOpt = sqlstore.InitTestDBOpt
|
|
|
|
|
|
|
|
var InitTestDB = sqlstore.InitTestDB
|
|
|
|
var InitTestDBwithCfg = sqlstore.InitTestDBWithCfg
|
|
|
|
var ProvideService = sqlstore.ProvideService
|
|
|
|
|
2022-12-09 01:11:56 -06:00
|
|
|
func IsTestDbSQLite() bool {
|
|
|
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); !present || db == "sqlite" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return !IsTestDbMySQL() && !IsTestDbPostgres()
|
|
|
|
}
|
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
func IsTestDbMySQL() bool {
|
|
|
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
|
|
|
return db == migrator.MySQL
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsTestDbPostgres() bool {
|
|
|
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
|
|
|
return db == migrator.Postgres
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsTestDBMSSQL() bool {
|
|
|
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
|
|
|
return db == migrator.MSSQL
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|