Refactor: Move sql_engine to sub package of tsdb (#18991)

this way importing the tsdb package does not come with xorm dependencies
This commit is contained in:
Kyle Brandt
2019-09-10 15:50:04 -04:00
committed by GitHub
parent 7520ebadac
commit e5bf3027fc
11 changed files with 111 additions and 95 deletions
+6 -5
View File
@@ -8,21 +8,22 @@ import (
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)
const rsIdentifier = `([_a-zA-Z0-9]+)`
const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
type postgresMacroEngine struct {
*tsdb.SqlMacroEngineBase
*sqleng.SqlMacroEngineBase
timeRange *tsdb.TimeRange
query *tsdb.Query
timescaledb bool
}
func newPostgresMacroEngine(timescaledb bool) tsdb.SqlMacroEngine {
func newPostgresMacroEngine(timescaledb bool) sqleng.SqlMacroEngine {
return &postgresMacroEngine{
SqlMacroEngineBase: tsdb.NewSqlMacroEngineBase(),
SqlMacroEngineBase: sqleng.NewSqlMacroEngineBase(),
timescaledb: timescaledb,
}
}
@@ -101,7 +102,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
return "", fmt.Errorf("error parsing interval %v", args[1])
}
if len(args) == 3 {
err := tsdb.SetupFillmode(m.query, interval, args[2])
err := sqleng.SetupFillmode(m.query, interval, args[2])
if err != nil {
return "", err
}
@@ -145,7 +146,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
return "", fmt.Errorf("error parsing interval %v", args[1])
}
if len(args) == 3 {
err := tsdb.SetupFillmode(m.query, interval, args[2])
err := sqleng.SetupFillmode(m.query, interval, args[2])
if err != nil {
return "", err
}
+5 -3
View File
@@ -2,14 +2,16 @@ package postgres
import (
"database/sql"
"github.com/grafana/grafana/pkg/setting"
"net/url"
"strconv"
"github.com/grafana/grafana/pkg/setting"
"github.com/go-xorm/core"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)
func init() {
@@ -24,7 +26,7 @@ func newPostgresQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndp
logger.Debug("getEngine", "connection", cnnstr)
}
config := tsdb.SqlQueryEndpointConfiguration{
config := sqleng.SqlQueryEndpointConfiguration{
DriverName: "postgres",
ConnectionString: cnnstr,
Datasource: datasource,
@@ -37,7 +39,7 @@ func newPostgresQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndp
timescaledb := datasource.JsonData.Get("timescaledb").MustBool(false)
return tsdb.NewSqlQueryEndpoint(&config, &rowTransformer, newPostgresMacroEngine(timescaledb), logger)
return sqleng.NewSqlQueryEndpoint(&config, &rowTransformer, newPostgresMacroEngine(timescaledb), logger)
}
func generateConnectionString(datasource *models.DataSource) string {
+12 -10
View File
@@ -15,6 +15,8 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
_ "github.com/lib/pq"
. "github.com/smartystreets/goconvey/convey"
)
@@ -39,13 +41,13 @@ func TestPostgres(t *testing.T) {
Convey("PostgreSQL", t, func() {
x := InitPostgresTestDB(t)
origXormEngine := tsdb.NewXormEngine
tsdb.NewXormEngine = func(d, c string) (*xorm.Engine, error) {
origXormEngine := sqleng.NewXormEngine
sqleng.NewXormEngine = func(d, c string) (*xorm.Engine, error) {
return x, nil
}
origInterpolate := tsdb.Interpolate
tsdb.Interpolate = func(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) {
origInterpolate := sqleng.Interpolate
sqleng.Interpolate = func(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) {
return sql, nil
}
@@ -60,8 +62,8 @@ func TestPostgres(t *testing.T) {
Reset(func() {
sess.Close()
tsdb.NewXormEngine = origXormEngine
tsdb.Interpolate = origInterpolate
sqleng.NewXormEngine = origXormEngine
sqleng.Interpolate = origInterpolate
})
Convey("Given a table with different native data types", func() {
@@ -230,11 +232,11 @@ func TestPostgres(t *testing.T) {
})
Convey("When doing a metric query using timeGroup and $__interval", func() {
mockInterpolate := tsdb.Interpolate
tsdb.Interpolate = origInterpolate
mockInterpolate := sqleng.Interpolate
sqleng.Interpolate = origInterpolate
Reset(func() {
tsdb.Interpolate = mockInterpolate
sqleng.Interpolate = mockInterpolate
})
Convey("Should replace $__interval", func() {
@@ -686,7 +688,7 @@ func TestPostgres(t *testing.T) {
})
Convey("When doing a query with timeFrom,timeTo,unixEpochFrom,unixEpochTo macros", func() {
tsdb.Interpolate = origInterpolate
sqleng.Interpolate = origInterpolate
query := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", fromStart),
Queries: []*tsdb.Query{