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(sync): sync MySQL default null column (#10075)
h3n4lad0e2f2f2494
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.goindex 0a44b239b0..f864131677 100644--- a/backend/plugin/db/mysql/sync.go+++ b/backend/plugin/db/mysql/sync.go@@ -266,24 +266,29 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema); err != nil {return nil, err}+ nullableBool, err := util.ConvertYesNo(nullable)+ if err != nil {+ return nil, err+ }+ column.Nullable = nullableBoolif defaultStr.Valid {if strings.Contains(extra, "DEFAULT_GENERATED") {column.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: fmt.Sprintf("(%s)", defaultStr.String)}} else {column.DefaultValue = &storepb.ColumnMetadata_Default{Default: &wrapperspb.StringValue{Value: defaultStr.String}}}- } else {+ } else if strings.Contains(strings.ToUpper(extra), autoIncrementSymbol) {// TODO(zp): refactor column default value.- if strings.Contains(strings.ToUpper(extra), autoIncrementSymbol) {- // Use the upper case to consistent with MySQL Dump.- column.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: autoIncrementSymbol}+ // Use the upper case to consistent with MySQL Dump.+ column.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: autoIncrementSymbol}+ } else if nullableBool {+ // This is NULL if the column has an explicit default of NULL,+ // or if the column definition includes no DEFAULT clause.+ // https://dev.mysql.com/doc/refman/8.0/en/information-schema-columns-table.html+ column.DefaultValue = &storepb.ColumnMetadata_DefaultNull{+ DefaultNull: true,}}- isNullBool, err := util.ConvertYesNo(nullable)- if err != nil {- return nil, err- }- column.Nullable = isNullBoolkey := db.TableKey{Schema: "", Table: tableName}columnMap[key] = append(columnMap[key], column)diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.goindex ce0b814569..26dfd92c98 100644--- a/backend/tests/schema_update_test.go+++ b/backend/tests/schema_update_test.go@@ -1819,16 +1819,24 @@ func TestGetLatestSchema(t *testing.T) {DataSize: 16384,Columns: []*v1pb.ColumnMetadata{{- Name: "id",- Position: 1,- Nullable: true,- Type: "int",+ Name: "id",+ Position: 1,+ Nullable: true,+ HasDefault: true,+ Default: &v1pb.ColumnMetadata_DefaultNull{+ DefaultNull: true,+ },+ Type: "int",},{- Name: "name",- Position: 2,- Nullable: true,- Type: "text",+ Name: "name",+ Position: 2,+ Nullable: true,+ Type: "text",+ HasDefault: true,+ Default: &v1pb.ColumnMetadata_DefaultNull{+ DefaultNull: true,+ },CharacterSet: "utf8mb4",Collation: "utf8mb4_general_ci",},diff --git a/backend/tests/syncer_test.go b/backend/tests/syncer_test.goindex 8379a9f4fc..4bb736e16d 100644--- a/backend/tests/syncer_test.go+++ b/backend/tests/syncer_test.go@@ -289,19 +289,25 @@ func TestSyncerForMySQL(t *testing.T) {"name":"a","position":1,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true},{"name":"b","position":2,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true},{"name":"c","position":3,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true}],"indexes":[@@ -360,19 +366,25 @@ func TestSyncerForMySQL(t *testing.T) {"name":"a","position":1,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true},{"name":"b","position":2,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true},{"name":"c","position":3,"nullable":true,- "type":"int"+ "type":"int",+ "defaultNull":true,+ "hasDefault":true}],"indexes":[