Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(tidb): sync fails on TiDB Cloud version strings (BYT-10062) (#21206)
Danny Xub9e2cb6965c6
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.goindex 872dacd945..ab68393106 100644--- a/backend/plugin/db/tidb/sync.go+++ b/backend/plugin/db/tidb/sync.go@@ -333,13 +333,8 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad}// Query check constraint info.- // information_schema.CHECK_CONSTRAINTS was added in TiDB v7.4.0.checkMap := make(map[db.TableKey][]*storepb.CheckConstraintMetadata)- hasCheckConstraints, err := tidbVersionAtLeast(version, "7.4.0")- if err != nil {- return nil, err- }- if hasCheckConstraints {+ if supportsCheckConstraintTable(version) {checkMap, err = d.getCheckConstraintList(ctx, d.databaseName)if err != nil {return nil, err@@ -834,15 +829,43 @@ func (d *Driver) getCheckConstraintList(ctx context.Context, databaseName stringreturn checkMap, nil}-// tidbVersionAtLeast checks if a TiDB version string (e.g., "v7.4.0") is-// greater than or equal to the given threshold (e.g., "7.4.0").-func tidbVersionAtLeast(version, threshold string) (bool, error) {- v := strings.TrimPrefix(version, "v")- semVersion, err := semver.Make(v)+var (+ // The TiDB version within a VERSION() string, e.g. "8.0.11-TiDB-v8.5.0" and+ // "8.0.11-TiDB-v7.5.2-serverless".+ tidbSemverRegex = regexp.MustCompile(`v\d+\.\d+\.\d+`)+ // The leading MySQL compatibility version every TiDB build reports.+ mysqlCompatVersionRegex = regexp.MustCompile(`^\d+\.\d+\.\d+`)+)++// supportsCheckConstraintTable reports whether the server exposes+// information_schema.CHECK_CONSTRAINTS, which TiDB added in v7.4.0. rawVersion is the+// verbatim VERSION() output, carrying a MySQL compatibility version and, usually, the+// TiDB version: "8.0.11-TiDB-v8.5.0".+//+// The TiDB version decides whenever it is readable. When it is not — TiDB Cloud reports a+// calendar version ("8.0.11-TiDB-CLOUD.202603.4"), and a custom server-version can drop it+// altogether — fall back to the MySQL prefix, which marks the same boundary: TiDB moved+// that prefix from 5.7.25 to 8.0.11 in v7.4.0, the very release that added the table.+//+// Do not read the prefix as a real MySQL version. MySQL added the table in 8.0.16, so+// comparing against that would answer false for every TiDB build, all of which report+// 8.0.11.+func supportsCheckConstraintTable(rawVersion string) bool {+ if loc := tidbSemverRegex.FindStringIndex(rawVersion); loc != nil {+ // Skip the leading "v" that semver.Make does not accept.+ if tidbVersion, err := semver.Make(rawVersion[loc[0]+1 : loc[1]]); err == nil {+ return tidbVersion.GE(semver.MustParse("7.4.0"))+ }+ }+ loc := mysqlCompatVersionRegex.FindStringIndex(rawVersion)+ if loc == nil {+ return false+ }+ mysqlVersion, err := semver.Make(rawVersion[loc[0]:loc[1]])if err != nil {- return false, errors.Wrapf(err, "failed to parse TiDB version %q", version)+ return false}- return semVersion.GE(semver.MustParse(threshold)), nil+ return mysqlVersion.GE(semver.MustParse("8.0.0"))}func stripSingleQuote(s string) string {diff --git a/backend/plugin/db/tidb/sync_test.go b/backend/plugin/db/tidb/sync_test.goindex fb79c3a21a..b1c2a30996 100644--- a/backend/plugin/db/tidb/sync_test.go+++ b/backend/plugin/db/tidb/sync_test.go@@ -297,3 +297,41 @@ func TestStripSingleQuote(t *testing.T) {})}}++func TestSupportsCheckConstraintTable(t *testing.T) {+ tests := []struct {+ version string+ want bool+ }{+ // information_schema.CHECK_CONSTRAINTS landed in TiDB v7.4.0, the same release+ // that moved the reported MySQL prefix from 5.7.25 to 8.0.11. The TiDB version+ // decides whenever it is readable.+ {version: "5.7.25-TiDB-v7.1.1", want: false},+ {version: "5.7.25-TiDB-v7.2.0", want: false},+ {version: "5.7.25-TiDB-v7.3.0", want: false},+ {version: "8.0.11-TiDB-v7.4.0", want: true},+ {version: "8.0.11-TiDB-v8.5.0", want: true},+ {version: "8.0.11-TiDB-v7.5.2-serverless", want: true},+ // No readable TiDB version, so the MySQL prefix decides. TiDB Cloud reports a+ // calendar version; a custom server-version can drop the TiDB half entirely.+ {version: "8.0.11-TiDB-CLOUD.202603.4", want: true},+ {version: "5.7.25-TiDB-CLOUD.202603.4", want: false},+ {version: "8.0.11", want: true},+ {version: "5.7.25", want: false},+ // A future prefix bump must not regress.+ {version: "8.4.0-TiDB-CLOUD.202701.1", want: true},+ // The TiDB version wins over the prefix when both are readable.+ {version: "8.0.11-TiDB-v7.3.0", want: false},+ // MySQL added the table in 8.0.16; comparing the prefix against that would+ // answer false for every TiDB build, all of which report 8.0.11.+ {version: "8.0.11-TiDB-CLOUD.202512.1", want: true},+ {version: "", want: false},+ {version: "not-a-version", want: false},+ }++ for _, tc := range tests {+ t.Run(tc.version, func(t *testing.T) {+ require.Equal(t, tc.want, supportsCheckConstraintTable(tc.version))+ })+ }+}diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.goindex a0250ed3bf..abe08c6d3e 100644--- a/backend/plugin/db/tidb/tidb.go+++ b/backend/plugin/db/tidb/tidb.go@@ -8,7 +8,6 @@ import ("fmt""log/slog""net"- "regexp""strings""time"@@ -155,7 +154,10 @@ func (d *Driver) GetDB() *sql.DB {return d.db}-// getVersion gets the version.+// getVersion returns the verbatim VERSION() output, e.g. "8.0.11-TiDB-v8.5.0". It is+// stored and displayed as-is; callers that gate behavior on a version parse it+// themselves, so a version string we do not recognize costs a feature rather than the+// whole sync.func (d *Driver) getVersion(ctx context.Context) (string, error) {query := "SELECT VERSION()"var version string@@ -166,15 +168,7 @@ func (d *Driver) getVersion(ctx context.Context) (string, error) {return "", util.FormatErrorWithQuery(err, query)}- return parseVersion(version)-}--func parseVersion(version string) (string, error) {- // Examples: 8.0.11-TiDB-v8.5.0, 8.0.11-TiDB-v7.5.2-serverless.- if loc := regexp.MustCompile(`v\d+\.\d+\.\d+`).FindStringIndex(version); loc != nil {- return version[loc[0]:loc[1]], nil- }- return "", errors.Errorf("failed to parse version %q", version)+ return version, nil}func buildExecuteCommands(statement string) ([]base.Statement, error) {diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.goindex 4b4f8f9036..d52434d2eb 100644--- a/backend/plugin/db/tidb/tidb_test.go+++ b/backend/plugin/db/tidb/tidb_test.go@@ -53,52 +53,6 @@ func TestGetTiDBConnectionRejectsAllowAllFiles(t *testing.T) {require.Contains(t, err.Error(), "allowAllFiles")}-func TestParseVersion(t *testing.T) {- tests := []struct {- version string- want string- }{- {- version: "8.0.11-TiDB-v8.5.0",- want: "v8.5.0",- },- {- version: "8.0.11-TiDB-v7.5.2-serverless",- want: "v7.5.2",- },- }-- a := require.New(t)- for _, tc := range tests {- version, err := parseVersion(tc.version)- a.NoError(err)- a.Equal(tc.want, version)- }-}--func TestTiDBVersionAtLeast(t *testing.T) {- tests := []struct {- version string- threshold string- want bool- }{- {version: "v7.1.1", threshold: "7.4.0", want: false},- {version: "v7.2.0", threshold: "7.4.0", want: false},- {version: "v7.3.0", threshold: "7.4.0", want: false},- {version: "v7.4.0", threshold: "7.4.0", want: true},- {version: "v7.5.2", threshold: "7.4.0", want: true},- {version: "v8.0.0", threshold: "7.4.0", want: true},- {version: "v8.5.0", threshold: "7.4.0", want: true},- }-- a := require.New(t)- for _, tc := range tests {- got, err := tidbVersionAtLeast(tc.version, tc.threshold)- a.NoError(err)- a.Equal(tc.want, got, "version=%s threshold=%s", tc.version, tc.threshold)- }-}-func TestBuildExecuteCommandsNormalizesDelimiter(t *testing.T) {statement := "DELIMITER //\nCREATE PROCEDURE p()\nBEGIN\n SELECT 1;\nEND//\nDELIMITER ;\n"