Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: tidb sync test (#11051)
Danny Xuf3ffb5aa2e7e
diff --git a/backend/plugin/db/tidb/dump.go b/backend/plugin/db/tidb/dump.goindex 429a0afcfd..7b911b5af2 100644--- a/backend/plugin/db/tidb/dump.go+++ b/backend/plugin/db/tidb/dump.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("bytes"diff --git a/backend/plugin/db/tidb/dump_test.go b/backend/plugin/db/tidb/dump_test.goindex 5f6a51d9d2..fb10756fa3 100644--- a/backend/plugin/db/tidb/dump_test.go+++ b/backend/plugin/db/tidb/dump_test.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("testing"diff --git a/backend/plugin/db/tidb/query.go b/backend/plugin/db/tidb/query.goindex 8aa66dd155..a14eafbfd5 100644--- a/backend/plugin/db/tidb/query.go+++ b/backend/plugin/db/tidb/query.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("fmt"diff --git a/backend/plugin/db/tidb/query_test.go b/backend/plugin/db/tidb/query_test.goindex 8481e6e9b6..54a052a495 100644--- a/backend/plugin/db/tidb/query_test.go+++ b/backend/plugin/db/tidb/query_test.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("testing"diff --git a/backend/plugin/db/tidb/role.go b/backend/plugin/db/tidb/role.goindex 71ad4376d2..a59faf8a42 100644--- a/backend/plugin/db/tidb/role.go+++ b/backend/plugin/db/tidb/role.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("context"diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.goindex 491dd6927f..b21f7c9932 100644--- a/backend/plugin/db/tidb/sync.go+++ b/backend/plugin/db/tidb/sync.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("context"diff --git a/backend/plugin/db/tidb/sync_test.go b/backend/plugin/db/tidb/sync_test.gonew file mode 100644index 0000000000..57420021b7--- /dev/null+++ b/backend/plugin/db/tidb/sync_test.go@@ -0,0 +1,244 @@+package tidb++import (+ "database/sql"+ "testing"++ "github.com/stretchr/testify/require"+ "google.golang.org/protobuf/types/known/wrapperspb"++ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++func TestSetColumnMetadataDefault(t *testing.T) {+ /*+ CREATE TABLE hello (+ id INT AUTO_INCREMENT PRIMARY KEY,+ name0 varchar(20),+ name1 varchar(20) DEFAULT '0',+ name2 varchar(20) DEFAULT 'hello',+ age0 int NOT NULL,+ age1 tinyint(4) NOT NULL DEFAULT '0',+ age2 tinyint(4) NOT NULL DEFAULT 0,+ age3 tinyint NOT NULL DEFAULT '0',+ age4 tinyint NOT NULL DEFAULT 0,+ price double(16,2) DEFAULT '0.00',+ time0 datetime,+ time1 datetime NOT NULL,+ time2 datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,+ time3 datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,+ time4 datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,+ time5 datetime(6) DEFAULT CURRENT_TIMESTAMP(6),+ time6 datetime(6) DEFAULT NOW(6),+ time7 datetime DEFAULT CURRENT_TIMESTAMP()+ );++ // 8.0.11-TiDB-v7.6.0 created format.+ // 5.7.28-TiDB-v7.1.1-serverless created format.+ // 5.7.25-TiDB-v6.5.8 created format.+ CREATE TABLE `hello` (+ `id` int(11) NOT NULL AUTO_INCREMENT,+ `name0` varchar(20) DEFAULT NULL,+ `name1` varchar(20) DEFAULT '0',+ `name2` varchar(20) DEFAULT 'hello',+ `age0` int(11) NOT NULL,+ `age1` tinyint(4) NOT NULL DEFAULT '0',+ `age2` tinyint(4) NOT NULL DEFAULT '0',+ `age3` tinyint(4) NOT NULL DEFAULT '0',+ `age4` tinyint(4) NOT NULL DEFAULT '0',+ `price` double(16,2) DEFAULT '0',+ `time0` datetime DEFAULT NULL,+ `time1` datetime NOT NULL,+ `time2` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,+ `time3` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,+ `time4` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,+ `time5` datetime(6) DEFAULT CURRENT_TIMESTAMP(6),+ `time6` datetime(6) DEFAULT CURRENT_TIMESTAMP(6),+ `time7` datetime DEFAULT CURRENT_TIMESTAMP,+ PRIMARY KEY (`id`) ... // T![clustered_index] CLUSTERED+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;+ */+ tests := []struct {+ name string+ defaultStr sql.NullString+ nullableBool bool+ extra string+ want *storepb.ColumnMetadata+ }{+ // 8.0.11-TiDB-v7.6.0.+ // 5.7.28-TiDB-v7.1.1-serverless.+ // 5.7.25-TiDB-v6.5.8.+ {+ name: "id",+ defaultStr: sql.NullString{},+ nullableBool: false,+ extra: "auto_increment",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "AUTO_INCREMENT"},+ },+ },+ {+ name: "name0",+ defaultStr: sql.NullString{},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultNull{DefaultNull: true},+ },+ },+ {+ name: "name1",+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "name2",+ defaultStr: sql.NullString{Valid: true, String: "hello"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "hello"}},+ },+ },+ {+ name: "age0",+ defaultStr: sql.NullString{},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: nil,+ },+ },+ {+ name: "age1",+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "age2",+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "age3",+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "age4",+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "price",+ // This is strange, not "0.00".+ defaultStr: sql.NullString{Valid: true, String: "0"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: "0"}},+ },+ },+ {+ name: "time0",+ defaultStr: sql.NullString{},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultNull{DefaultNull: true},+ },+ },+ {+ name: "time1",+ defaultStr: sql.NullString{},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: nil,+ },+ },+ {+ name: "time2",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP"},+ nullableBool: false,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP"},+ },+ },+ {+ name: "time3",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP"},+ nullableBool: false,+ extra: "DEFAULT_GENERATED on update CURRENT_TIMESTAMP",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP"},+ OnUpdate: "CURRENT_TIMESTAMP",+ },+ },+ {+ name: "time4",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP"},+ nullableBool: true,+ extra: "DEFAULT_GENERATED on update CURRENT_TIMESTAMP",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP"},+ OnUpdate: "CURRENT_TIMESTAMP",+ },+ },+ {+ name: "time5",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP(6)"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP(6)"},+ },+ },+ {+ name: "time6",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP(6)"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP(6)"},+ },+ },+ {+ name: "time7",+ defaultStr: sql.NullString{Valid: true, String: "CURRENT_TIMESTAMP"},+ nullableBool: true,+ extra: "",+ want: &storepb.ColumnMetadata{+ DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "CURRENT_TIMESTAMP"},+ },+ },+ }++ a := require.New(t)+ for _, tc := range tests {+ column := &storepb.ColumnMetadata{}+ setColumnMetadataDefault(column, tc.defaultStr, tc.nullableBool, tc.extra)+ a.Equal(tc.want, column, tc.name)+ }+}diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.goindex 2915f2ae0a..4055c83a9e 100644--- a/backend/plugin/db/tidb/tidb.go+++ b/backend/plugin/db/tidb/tidb.go@@ -1,5 +1,5 @@-// Package mysql is the plugin for MySQL driver.-package mysql+// Package tidb is the plugin for TiDB driver.+package tidbimport ("context"diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.goindex b757678404..0ec755076c 100644--- a/backend/plugin/db/tidb/tidb_test.go+++ b/backend/plugin/db/tidb/tidb_test.go@@ -1,4 +1,4 @@-package mysql+package tidbimport ("testing"