sql: remove xorm dependency from postgres/mysql/mssql (#77870)

* sql: remove xorm

* sql: remove pkg/util dependency (#78821)

sql: removed md5-util dependency
This commit is contained in:
Gábor Farkas
2023-12-06 09:35:05 +01:00
committed by GitHub
parent d85ac7cea7
commit 9f0fc60477
9 changed files with 87 additions and 187 deletions
@@ -2,6 +2,7 @@ package postgres
import (
"context"
"database/sql"
"fmt"
"math/rand"
"os"
@@ -13,7 +14,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
@@ -185,13 +185,13 @@ func TestIntegrationPostgres(t *testing.T) {
x := InitPostgresTestDB(t)
origXormEngine := sqleng.NewXormEngine
origDB := sqleng.NewDB
origInterpolate := sqleng.Interpolate
t.Cleanup(func() {
sqleng.NewXormEngine = origXormEngine
sqleng.NewDB = origDB
sqleng.Interpolate = origInterpolate
})
sqleng.NewXormEngine = func(d, c string) (*xorm.Engine, error) {
sqleng.NewDB = func(d, c string) (*sql.DB, error) {
return x, nil
}
sqleng.Interpolate = func(query backend.DataQuery, timeRange backend.TimeRange, timeInterval string, sql string) (string, error) {
@@ -229,9 +229,7 @@ func TestIntegrationPostgres(t *testing.T) {
require.NoError(t, err)
sess := x.NewSession()
t.Cleanup(sess.Close)
db := sess.DB()
db := x
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
t.Run("Given a table with different native data types", func(t *testing.T) {
@@ -615,17 +613,17 @@ func TestIntegrationPostgres(t *testing.T) {
t.Run("Given a table with metrics having multiple values and measurements", func(t *testing.T) {
type metric_values struct {
Time time.Time
TimeInt64 int64 `xorm:"bigint 'timeInt64' not null"`
TimeInt64Nullable *int64 `xorm:"bigint 'timeInt64Nullable' null"`
TimeFloat64 float64 `xorm:"double 'timeFloat64' not null"`
TimeFloat64Nullable *float64 `xorm:"double 'timeFloat64Nullable' null"`
TimeInt32 int32 `xorm:"int(11) 'timeInt32' not null"`
TimeInt32Nullable *int32 `xorm:"int(11) 'timeInt32Nullable' null"`
TimeFloat32 float32 `xorm:"double 'timeFloat32' not null"`
TimeFloat32Nullable *float32 `xorm:"double 'timeFloat32Nullable' null"`
TimeInt64 int64
TimeInt64Nullable *int64
TimeFloat64 float64
TimeFloat64Nullable *float64
TimeInt32 int32
TimeInt32Nullable *int32
TimeFloat32 float32
TimeFloat32Nullable *float32
Measurement string
ValueOne int64 `xorm:"integer 'valueOne'"`
ValueTwo int64 `xorm:"integer 'valueTwo'"`
ValueOne int64
ValueTwo int64
}
_, err := db.Exec("DROP TABLE IF EXISTS metric_values")
@@ -1392,16 +1390,10 @@ func TestIntegrationPostgres(t *testing.T) {
})
}
func InitPostgresTestDB(t *testing.T) *xorm.Engine {
func InitPostgresTestDB(t *testing.T) *sql.DB {
connStr := postgresTestDBConnString()
x, err := xorm.NewEngine("postgres", connStr)
x, err := sql.Open("postgres", connStr)
require.NoError(t, err, "Failed to init postgres DB")
x.DatabaseTZ = time.UTC
x.TZLocation = time.UTC
// x.ShowSQL()
return x
}
@@ -2,34 +2,28 @@ package postgres
import (
"context"
"crypto/md5"
"database/sql"
"database/sql/driver"
"fmt"
"net"
"slices"
"time"
sdkproxy "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
"github.com/grafana/grafana/pkg/util"
"github.com/lib/pq"
"golang.org/x/net/proxy"
"xorm.io/core"
)
// createPostgresProxyDriver creates and registers a new sql driver that uses a postgres connector and updates the dialer to
// route connections through the secure socks proxy
func createPostgresProxyDriver(cnnstr string, opts *sdkproxy.Options) (string, error) {
sqleng.XormDriverMu.Lock()
defer sqleng.XormDriverMu.Unlock()
// create a unique driver per connection string
hash, err := util.Md5SumString(cnnstr)
if err != nil {
return "", err
}
hash := fmt.Sprintf("%x", md5.Sum([]byte(cnnstr)))
driverName := "postgres-proxy-" + hash
// only register the driver once
if core.QueryDriver(driverName) == nil {
if !slices.Contains(sql.Drivers(), driverName) {
connector, err := pq.NewConnector(cnnstr)
if err != nil {
return "", err
@@ -41,7 +35,6 @@ func createPostgresProxyDriver(cnnstr string, opts *sdkproxy.Options) (string, e
}
sql.Register(driverName, driver)
core.RegisterDriver(driverName, driver)
}
return driverName, nil
}
@@ -53,7 +46,6 @@ type postgresProxyDriver struct {
}
var _ driver.DriverContext = (*postgresProxyDriver)(nil)
var _ core.Driver = (*postgresProxyDriver)(nil)
// newPostgresProxyDriver updates the dialer for a postgres connector with a dialer that proxies connections through the secure socks proxy
// and returns a new postgres driver to register
@@ -87,14 +79,6 @@ func (p *postgresProxyDialer) DialTimeout(network, address string, timeout time.
return p.d.(proxy.ContextDialer).DialContext(ctx, network, address)
}
// Parse uses the xorm postgres dialect for the driver (this has to be implemented to register the driver with xorm)
func (d *postgresProxyDriver) Parse(a string, b string) (*core.Uri, error) {
sqleng.XormDriverMu.RLock()
defer sqleng.XormDriverMu.RUnlock()
return core.QueryDriver("postgres").Parse(a, b)
}
// OpenConnector returns the normal postgres connector that has the updated dialer context
func (d *postgresProxyDriver) OpenConnector(name string) (driver.Connector, error) {
return d.c, nil
@@ -10,11 +10,9 @@ import (
"github.com/grafana/grafana/pkg/tsdb/sqleng/proxyutil"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"xorm.io/core"
)
func TestPostgresProxyDriver(t *testing.T) {
dialect := "postgres"
settings := proxyutil.SetupTestSecureSocksProxySettings(t)
proxySettings := setting.SecureSocksDSProxySettings{
Enabled: true,
@@ -42,17 +40,6 @@ func TestPostgresProxyDriver(t *testing.T) {
require.NotEqual(t, driverName, testDriver)
})
t.Run("Parse should have the same result as xorm mssql parse", func(t *testing.T) {
xormDriver := core.QueryDriver(dialect)
xormResult, err := xormDriver.Parse(dialect, cnnstr)
require.NoError(t, err)
xormNewDriver := core.QueryDriver(driverName)
xormNewResult, err := xormNewDriver.Parse(dialect, cnnstr)
require.NoError(t, err)
require.Equal(t, xormResult, xormNewResult)
})
t.Run("Connector should use dialer context that routes through the socks proxy to db", func(t *testing.T) {
connector, err := pq.NewConnector(cnnstr)
require.NoError(t, err)