Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

make version check more compatible #381

Merged
merged 7 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions v4/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,22 @@ var ServerInfoUnknown = ServerInfo{

var (
versionRegex = regexp.MustCompile(`^\d+\.\d+\.\d+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?`)
// `select version()` result
tidbVersionRegex = regexp.MustCompile(`-[v]?\d+\.\d+\.\d+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?`)
// `select tidb_version()` result
tidbReleaseVersionRegex = regexp.MustCompile(`v\d+\.\d+\.\d+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?`)
)

// ParseServerInfo parses exported server type and version info from version string
func ParseServerInfo(tctx *tcontext.Context, src string) ServerInfo {
tctx.L().Debug("parse server info", zap.String("server info string", src))
lowerCase := strings.ToLower(src)
serverInfo := ServerInfo{}
isReleaseVersion := false
switch {
case strings.Contains(lowerCase, "release version:"):
// this version string is tidb release version
serverInfo.ServerType = ServerTypeTiDB
isReleaseVersion = true
case strings.Contains(lowerCase, "tidb"):
serverInfo.ServerType = ServerTypeTiDB
case strings.Contains(lowerCase, "mariadb"):
Expand All @@ -606,12 +613,13 @@ func ParseServerInfo(tctx *tcontext.Context, src string) ServerInfo {
serverInfo.ServerType = ServerTypeUnknown
}

tctx.L().Info("detect server type",
zap.String("type", serverInfo.ServerType.String()))

var versionStr string
if serverInfo.ServerType == ServerTypeTiDB {
versionStr = tidbVersionRegex.FindString(src)[1:]
if isReleaseVersion {
versionStr = tidbReleaseVersionRegex.FindString(src)
} else {
versionStr = tidbVersionRegex.FindString(src)[1:]
}
versionStr = strings.TrimPrefix(versionStr, "v")
} else {
versionStr = versionRegex.FindString(src)
Expand All @@ -622,11 +630,11 @@ func ParseServerInfo(tctx *tcontext.Context, src string) ServerInfo {
if err != nil {
tctx.L().Warn("fail to parse version",
zap.String("version", versionStr))
return serverInfo
}

tctx.L().Info("detect server version",
zap.String("type", serverInfo.ServerType.String()),
zap.String("version", serverInfo.ServerVersion.String()))

return serverInfo
}

Expand Down
13 changes: 10 additions & 3 deletions v4/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,18 @@ func ListAllDatabasesTables(tctx *tcontext.Context, db *sql.Conn, databaseNames
}

// SelectVersion gets the version information from the database server
func SelectVersion(db *sql.DB) (string, error) {
func SelectVersion(ctx tcontext.Context, db *sql.DB) (string, error) {
var versionInfo string
const query = "SELECT version()"
const queryTiDB = "SELECT tidb_version();"
tidbRow := db.QueryRowContext(ctx, queryTiDB)
err := tidbRow.Scan(&versionInfo)
if err == nil {
return versionInfo, nil
}
ctx.L().Warn("select tidb_version() failed, will fallback to 'select version();'", zap.Error(err))
const query = "SELECT version();"
row := db.QueryRow(query)
err := row.Scan(&versionInfo)
err = row.Scan(&versionInfo)
if err != nil {
return "", errors.Annotatef(err, "sql: %s", query)
}
Expand Down
14 changes: 11 additions & 3 deletions v4/export/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestDetectServerInfo(t *testing.T) {
{4, "5.7.25-TiDB-v3.0.7-58-g6adce2367", ServerTypeTiDB, mkVer(3, 0, 7, "58-g6adce2367")},
{5, "5.7.25-TiDB-3.0.6", ServerTypeTiDB, mkVer(3, 0, 6, "")},
{6, "invalid version", ServerTypeUnknown, (*semver.Version)(nil)},
{7, "Release Version: v5.2.1\nEdition: Community\nGit Commit Hash: cd8fb24c5f7ebd9d479ed228bb41848bd5e97445", ServerTypeTiDB, mkVer(5, 2, 1, "")},
{8, "Release Version: v5.4.0-alpha-21-g86caab907\nEdition: Community\nGit Commit Hash: 86caab907c481bbc4243b5a3346ec13907cc8721\nGit Branch: master", ServerTypeTiDB, mkVer(5, 4, 0, "alpha-21-g86caab907")},
}
dec := func(d []interface{}) (tag int, verStr string, tp ServerType, v *semver.Version) {
return d[0].(int), d[1].(string), ServerType(d[2].(int)), d[3].(*semver.Version)
Expand All @@ -70,10 +72,16 @@ func TestDetectServerInfo(t *testing.T) {
for _, datum := range data {
tag, r, serverTp, expectVer := dec(datum)
comment := fmt.Sprintf("test case number: %d", tag)
rows := sqlmock.NewRows([]string{"version"}).AddRow(r)
mock.ExpectQuery("SELECT version()").WillReturnRows(rows)
tidbVersionQuery := mock.ExpectQuery("SELECT tidb_version\\(\\);")
if strings.HasPrefix(r, "Release Version:") {
tidbVersionQuery.WillReturnRows(sqlmock.NewRows([]string{"tidb_version"}).AddRow(r))
} else {
tidbVersionQuery.WillReturnError(errors.New("mock error"))
rows := sqlmock.NewRows([]string{"version"}).AddRow(r)
mock.ExpectQuery("SELECT version\\(\\);").WillReturnRows(rows)
}

verStr, err := SelectVersion(db)
verStr, err := SelectVersion(*tcontext.Background(), db)
require.NoError(t, err, comment)

info := ParseServerInfo(tcontext.Background(), verStr)
Expand Down