Skip to content

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

fix(structurer): keep default null for json/geometry (#10304)

h3n4l75f4cb7bd396
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index 06b7bea614..c8c18ff301 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
"io"
- "slices"
"sort"
"strings"
@@ -746,9 +745,7 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
return
}
if column.nullable {
- if !slices.ContainsFunc(expressionDefaultOnlyTypes, func(s string) bool {
- return strings.EqualFold(s, column.tp)
- }) {
+ if _, ok := expressionDefaultOnlyTypes[strings.ToUpper(column.tp)]; !ok {
if _, err := g.columnDefine.WriteString(" NULL"); err != nil {
g.err = err
return
diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.go
index c9acb4dda6..3f9fc22ba7 100644
--- a/backend/plugin/schema/mysql/state.go
+++ b/backend/plugin/schema/mysql/state.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
"io"
- "slices"
"sort"
"strings"
@@ -494,9 +493,7 @@ func (c *columnState) toString(buf io.StringWriter) error {
return err
}
if c.nullable {
- if !slices.ContainsFunc(expressionDefaultOnlyTypes, func(s string) bool {
- return strings.EqualFold(s, c.tp)
- }) {
+ if _, ok := expressionDefaultOnlyTypes[strings.ToUpper(c.tp)]; !ok {
if _, err := buf.WriteString(" NULL"); err != nil {
return err
}
@@ -555,9 +552,8 @@ func convertToColumnState(id int, column *storepb.ColumnMetadata) *columnState {
if hasDefault {
_, isDefaultNull := column.GetDefaultValue().(*storepb.ColumnMetadata_DefaultNull)
// Some types do not default to NULL, but support default expressions.
- if isDefaultNull && slices.ContainsFunc(expressionDefaultOnlyTypes, func(s string) bool {
- return strings.EqualFold(s, column.Type)
- }) {
+ _, ok := expressionDefaultOnlyTypes[strings.ToUpper(column.Type)]
+ if isDefaultNull && ok {
hasDefault = false
}
}
diff --git a/backend/plugin/schema/mysql/utils.go b/backend/plugin/schema/mysql/utils.go
index 1d09d3c574..9a2735a124 100644
--- a/backend/plugin/schema/mysql/utils.go
+++ b/backend/plugin/schema/mysql/utils.go
@@ -17,23 +17,35 @@ const (
var (
// https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html
// expressionDefaultOnlyTypes is a list of types that only accept expression as default
- // value.
- expressionDefaultOnlyTypes = []string{
+ // value. While we restore the following types, we should not restore the default null.
+ // +-------+--------------------------------------------------------------------+
+ // | Table | Create Table |
+ // +-------+--------------------------------------------------------------------+
+ // | u | CREATE TABLE `u` ( |
+ // | | `b` blob, |
+ // | | `t` text, |
+ // | | `g` geometry DEFAULT NULL, |
+ // | | `j` json DEFAULT NULL |
+ // | | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+ // +-------+--------------------------------------------------------------------+.
+ expressionDefaultOnlyTypes = map[string]bool{
// BLOB & TEXT
// https://dev.mysql.com/doc/refman/8.0/en/blob.html
- "TINYBLOB",
- "BLOB",
- "MEIDUMBLOB",
- "LONGBLOB",
- "TINYTEXT",
- "TEXT",
- "MEDIUMTEXT",
- "LONGTEXT",
- // GEOMETRY
- "GEOMETRY",
- // JSON
- // https://dev.mysql.com/doc/refman/8.0/en/json.html
- "JSON",
+ "TINYBLOB": true,
+ "BLOB": true,
+ "MEIDUMBLOB": true,
+ "LONGBLOB": true,
+ "TINYTEXT": true,
+ "TEXT": true,
+ "MEDIUMTEXT": true,
+ "LONGTEXT": true,
+
+ // In practice, the following types restore the default null by mysqldump.
+ // // GEOMETRY
+ // "GEOMETRY": true,
+ // // JSON
+ // // https://dev.mysql.com/doc/refman/8.0/en/json.html
+ // "JSON": true,
}
)