Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(tidb): gate CHECK_CONSTRAINTS query on TiDB >= 7.4.0 (#19441)

Vincent Huangf87766312e79
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.go
index 5630bf31b0..3549795cc6 100644
--- a/backend/plugin/db/tidb/sync.go
+++ b/backend/plugin/db/tidb/sync.go
@@ -11,6 +11,7 @@ import (
"strings"
"time"
+ "github.com/blang/semver/v4"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
@@ -138,6 +139,12 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
Name: "",
}
+ // Query TiDB version for feature gating.
+ version, err := d.getVersion(ctx)
+ if err != nil {
+ return nil, err
+ }
+
// Query index info.
indexMap := make(map[db.TableKey]map[string]*storepb.IndexMetadata)
@@ -329,10 +336,18 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
}
// Query check constraint info.
- checkMap, err := d.getCheckConstraintList(ctx, d.databaseName)
+ // 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 {
+ checkMap, err = d.getCheckConstraintList(ctx, d.databaseName)
+ if err != nil {
+ return nil, err
+ }
+ }
// Query sequence info.
sequences, err := d.getSequenceList(ctx, d.databaseName)
@@ -822,6 +837,17 @@ func (d *Driver) getCheckConstraintList(ctx context.Context, databaseName string
return 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)
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to parse TiDB version %q", version)
+ }
+ return semVersion.GE(semver.MustParse(threshold)), nil
+}
+
func stripSingleQuote(s string) string {
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
return s[1 : len(s)-1]
diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.go
index 36206ea507..8d516ff370 100644
--- a/backend/plugin/db/tidb/tidb_test.go
+++ b/backend/plugin/db/tidb/tidb_test.go
@@ -29,6 +29,29 @@ func TestParseVersion(t *testing.T) {
}
}
+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 TestIsNonTransactionStatement(t *testing.T) {
tests := []struct {
stmt string