Skip to content

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

feat: handle auto random equivalent (#13464)

h3n4lc1d86af1530a
diff --git a/backend/api/v1/branch_merge.go b/backend/api/v1/branch_merge.go
index 48d5a6b728..5d87325300 100644
--- a/backend/api/v1/branch_merge.go
+++ b/backend/api/v1/branch_merge.go
@@ -80,6 +80,69 @@ func buildTimestampDefaultValue(input string) timestampDefaultValue {
return nil
}
+var (
+ autoRandomRegexp = regexp.MustCompile(`(?mis)^AUTO_RANDOM(\(\s*(?P<shardBit>(\d+))?\s*(,\s*(?P<allocationRange>(\d+)))?\s*\))?`)
+)
+
+// https://docs.pingcap.com/tidb/stable/auto-random
+type autoRandomDefaultValue struct {
+ shardBit int
+ allocationRange int
+}
+
+func buildAutoRandomDefaultValue(input string) *autoRandomDefaultValue {
+ matches := autoRandomRegexp.FindStringSubmatch(input)
+ if matches != nil {
+ result := &autoRandomDefaultValue{}
+ for i, name := range autoRandomRegexp.SubexpNames() {
+ if name == "shardBit" {
+ if matches[i] != "" {
+ shardBit, err := strconv.ParseInt(matches[i], 10, 64)
+ if err != nil {
+ return nil
+ }
+ result.shardBit = int(shardBit)
+ }
+ } else if name == "allocationRange" {
+ if matches[i] != "" {
+ allocationRange, err := strconv.ParseInt(matches[i], 10, 64)
+ if err != nil {
+ return nil
+ }
+ result.allocationRange = int(allocationRange)
+ }
+ }
+ }
+ return result
+ }
+
+ return nil
+}
+
+func isAutoRandomEquivalent(a, b *autoRandomDefaultValue) bool {
+ if a == nil || b == nil {
+ return (a == nil) == (b == nil)
+ }
+ // https://docs.pingcap.com/tidb/stable/auto-random
+ canonicalShardBitA, canonicalShardBitB := a.shardBit, b.shardBit
+ if canonicalShardBitA == 0 {
+ canonicalShardBitA = 5
+ }
+ if canonicalShardBitB == 0 {
+ canonicalShardBitB = 5
+ }
+
+ canonicalAllocationRangeA, canonicalAllocationRangeB := a.allocationRange, b.allocationRange
+ if canonicalAllocationRangeA == 0 {
+ canonicalAllocationRangeA = 64
+ }
+ if canonicalAllocationRangeB == 0 {
+ canonicalAllocationRangeB = 64
+ }
+
+ return (canonicalShardBitA == canonicalShardBitB) && (canonicalAllocationRangeA == canonicalAllocationRangeB)
+}
+
type diffAction string
const (
@@ -770,6 +833,12 @@ func compareColumnDefaultValue(a, b any) bool {
if aTimestampDefaultValue != nil && bTimestampDefaultValue != nil {
return aTimestampDefaultValue.getFsp() == bTimestampDefaultValue.getFsp()
}
+
+ aAutoRandomDefaultValue := buildAutoRandomDefaultValue(aExpr.DefaultExpression)
+ bAutoRandomDefaultValue := buildAutoRandomDefaultValue(bExpr.DefaultExpression)
+ if aAutoRandomDefaultValue != nil && bAutoRandomDefaultValue != nil {
+ return isAutoRandomEquivalent(aAutoRandomDefaultValue, bAutoRandomDefaultValue)
+ }
}
r := cmp.Diff(a, b, protocmp.Transform())
diff --git a/backend/api/v1/branch_merge_test.go b/backend/api/v1/branch_merge_test.go
index a60000481a..4907f8e00e 100644
--- a/backend/api/v1/branch_merge_test.go
+++ b/backend/api/v1/branch_merge_test.go
@@ -105,3 +105,36 @@ func TestNormalizeMySQLViewDefinition(t *testing.T) {
require.Equal(t, test.want, got, i)
}
}
+
+func TestAutoRandomEqual(t *testing.T) {
+ testCases := []struct {
+ aExpr string
+ bExpr string
+ equal bool
+ }{
+ {
+ aExpr: "AUTO_RANDOM",
+ bExpr: "auto_random(5, 64)",
+ equal: true,
+ },
+ {
+ aExpr: "AUTO_RANDOM(5)",
+ bExpr: "auto_random(5, 64)",
+ equal: true,
+ },
+ {
+ aExpr: "AUTO_RANDOM(10, 32)",
+ bExpr: "auto_random",
+ equal: false,
+ },
+ }
+
+ for _, tc := range testCases {
+ a := buildAutoRandomDefaultValue(tc.aExpr)
+ require.NotNil(t, a)
+ b := buildAutoRandomDefaultValue(tc.bExpr)
+ require.NotNil(t, b)
+ got := isAutoRandomEquivalent(a, b)
+ require.Equal(t, tc.equal, got)
+ }
+}
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 6f44061c58..7f7c47c4f6 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -26,6 +26,7 @@ import (
"github.com/bytebase/bytebase/backend/component/iam"
enterprise "github.com/bytebase/bytebase/backend/enterprise/api"
mysqldb "github.com/bytebase/bytebase/backend/plugin/db/mysql"
+ tidbdb "github.com/bytebase/bytebase/backend/plugin/db/tidb"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/plugin/parser/mysql"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -1174,7 +1175,7 @@ func reconcileMetadata(metadata *storepb.DatabaseSchemaMetadata, engine storepb.
if engine == storepb.Engine_MYSQL {
reconcileMySQLPartitionMetadata(table.Partitions, "")
}
- if engine == storepb.Engine_MYSQL || engine == storepb.Engine_TIDB {
+ if engine == storepb.Engine_MYSQL {
for _, column := range table.GetColumns() {
// If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.
if column.Nullable && column.DefaultValue == nil {
@@ -1184,6 +1185,16 @@ func reconcileMetadata(metadata *storepb.DatabaseSchemaMetadata, engine storepb.
}
column.Type = mysqldb.GetColumnTypeCanonicalSynonym(column.Type)
}
+ } else if engine == storepb.Engine_TIDB {
+ for _, column := range table.GetColumns() {
+ // If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.
+ if column.Nullable && column.DefaultValue == nil {
+ column.DefaultValue = &storepb.ColumnMetadata_DefaultNull{
+ DefaultNull: true,
+ }
+ }
+ column.Type = tidbdb.GetColumnTypeCanonicalSynonym(column.Type)
+ }
}
}
for _, view := range schema.Views {
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.go
index 8b84cfd43c..c1130f3ad4 100644
--- a/backend/plugin/db/tidb/sync.go
+++ b/backend/plugin/db/tidb/sync.go
@@ -275,7 +275,7 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
defer columnRows.Close()
for columnRows.Next() {
column := &storepb.ColumnMetadata{}
- var tableName, nullable, extra string
+ var tableName, nullable, extra, tp string
var defaultStr sql.NullString
if err := columnRows.Scan(
&tableName,
@@ -283,7 +283,7 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
&column.Position,
&defaultStr,
&nullable,
- &column.Type,
+ &tp,
&column.CharacterSet,
&column.Collation,
&column.Comment,
@@ -301,6 +301,7 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
if err != nil {
return nil, err
}
+ column.Type = GetColumnTypeCanonicalSynonym(tp)
column.Nullable = nullableBool
setColumnMetadataDefault(column, defaultStr, nullableBool, extra)
key := db.TableKey{Schema: "", Table: tableName}