Skip to content

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

chore(structurer): handle default value in structurer for MySQL (#10502)

h3n4l54589120b5ab
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index 0028a202cc..96e0a66289 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -718,7 +718,7 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
// if schema string has default value or auto_increment.
// and metdata has default value.
// we skip the schema auto_increment and only compare default value.
- skipSchemaAutoIncrement = column.hasDefault
+ skipSchemaAutoIncrement = column.defaultValue != nil
break
}
}
@@ -779,7 +779,7 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
default:
defaultValue = &defaultValueExpression{value: defaultValueText}
}
- if column.hasDefault && column.defaultValue.toString() == defaultValue.toString() {
+ if column.defaultValue != nil && column.defaultValue.toString() == defaultValue.toString() {
if _, err := g.columnDefine.WriteString(ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
Start: startPos,
Stop: attribute.GetStop().GetTokenIndex(),
@@ -787,7 +787,7 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
g.err = err
return
}
- } else if column.hasDefault {
+ } else if column.defaultValue != nil {
// todo(zp): refactor column attribute.
if strings.EqualFold(column.defaultValue.toString(), autoIncrementSymbol) {
if _, err := g.columnDefine.WriteString(ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
@@ -806,9 +806,13 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
return
}
}
- if _, err := g.columnDefine.WriteString(column.defaultValue.toString()); err != nil {
- g.err = err
- return
+ _, isNull := column.defaultValue.(*defaultValueNull)
+ dontWriteDefaultNull := isNull && column.nullable && expressionDefaultOnlyTypes[strings.ToUpper(column.tp)]
+ if !dontWriteDefaultNull {
+ if _, err := g.columnDefine.WriteString(column.defaultValue.toString()); err != nil {
+ g.err = err
+ return
+ }
}
}
case attribute.COMMENT_SYMBOL() != nil:
diff --git a/backend/plugin/schema/mysql/get_design_schema_test.go b/backend/plugin/schema/mysql/get_design_schema_test.go
index 0c2668f945..4b2b15b9cc 100644
--- a/backend/plugin/schema/mysql/get_design_schema_test.go
+++ b/backend/plugin/schema/mysql/get_design_schema_test.go
@@ -51,7 +51,7 @@ func TestGetDesignSchema(t *testing.T) {
if record {
tests[i].Result = result
} else {
- a.Equal(t.Result, result)
+ a.Equalf(t.Result, result, "test case %d: %s", i, t.Description)
}
}
diff --git a/backend/plugin/schema/mysql/parse_to_metadata.go b/backend/plugin/schema/mysql/parse_to_metadata.go
index ee2971a418..c9019812b2 100644
--- a/backend/plugin/schema/mysql/parse_to_metadata.go
+++ b/backend/plugin/schema/mysql/parse_to_metadata.go
@@ -138,7 +138,6 @@ func (t *mysqlTransformer) EnterColumnDefinition(ctx *mysql.ColumnDefinitionCont
id: len(table.columns),
name: columnName,
tp: dataType,
- hasDefault: false,
defaultValue: nil,
comment: "",
nullable: true,
@@ -154,7 +153,6 @@ func (t *mysqlTransformer) EnterColumnDefinition(ctx *mysql.ColumnDefinitionCont
Start: defaultValueStart,
Stop: attribute.GetStop().GetTokenIndex(),
})
- columnState.hasDefault = true
switch {
case strings.EqualFold(defaultValue, "NULL"):
columnState.defaultValue = &defaultValueNull{}
@@ -177,19 +175,12 @@ func (t *mysqlTransformer) EnterColumnDefinition(ctx *mysql.ColumnDefinitionCont
// todo(zp): refactor column attribute.
case attribute.AUTO_INCREMENT_SYMBOL() != nil:
defaultValue := autoIncrementSymbol
- columnState.hasDefault = true
columnState.defaultValue = &defaultValueExpression{value: defaultValue}
}
}
- if _, ok := expressionDefaultOnlyTypes[strings.ToUpper(columnState.tp)]; ok {
- if columnState.defaultValue == nil {
- columnState.defaultValue = &defaultValueNull{}
- columnState.hasDefault = false
- }
- if _, isNull := columnState.defaultValue.(*defaultValueNull); !isNull {
- columnState.hasDefault = true
- }
+ if columnState.defaultValue == nil && columnState.nullable {
+ columnState.defaultValue = &defaultValueNull{}
}
table.columns[columnName] = columnState
diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.go
index 81a284fb53..3488b8cc43 100644
--- a/backend/plugin/schema/mysql/state.go
+++ b/backend/plugin/schema/mysql/state.go
@@ -518,7 +518,6 @@ type columnState struct {
id int
name string
tp string
- hasDefault bool
defaultValue defaultValue
comment string
nullable bool
@@ -533,10 +532,11 @@ func (c *columnState) toString(buf io.StringWriter) error {
return err
}
}
- if c.hasDefault {
+ if c.defaultValue != nil {
_, isDefaultNull := c.defaultValue.(*defaultValueNull)
+ dontWriteDefaultNull := isDefaultNull && c.nullable && expressionDefaultOnlyTypes[strings.ToUpper(c.tp)]
// Some types do not default to NULL, but support default expressions.
- if _, ok := expressionDefaultOnlyTypes[strings.ToUpper(c.tp)]; !(isDefaultNull && ok) {
+ if !dontWriteDefaultNull {
// todo(zp): refactor column attribute.
if strings.EqualFold(c.defaultValue.toString(), autoIncrementSymbol) {
if _, err := buf.WriteString(fmt.Sprintf(" %s", c.defaultValue.toString())); err != nil {
@@ -568,7 +568,7 @@ func (c *columnState) convertToColumnMetadata() *storepb.ColumnMetadata {
Nullable: c.nullable,
Comment: c.comment,
}
- if c.hasDefault {
+ if c.defaultValue != nil {
switch value := c.defaultValue.(type) {
case *defaultValueNull:
result.DefaultValue = &storepb.ColumnMetadata_DefaultNull{DefaultNull: true}
@@ -577,32 +577,22 @@ func (c *columnState) convertToColumnMetadata() *storepb.ColumnMetadata {
case *defaultValueExpression:
result.DefaultValue = &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: value.value}
}
- } else if expressionDefaultOnlyTypes[strings.ToUpper(c.tp)] {
+ }
+ if result.DefaultValue == nil && c.nullable {
result.DefaultValue = &storepb.ColumnMetadata_DefaultNull{DefaultNull: true}
}
return result
}
func convertToColumnState(id int, column *storepb.ColumnMetadata) *columnState {
- hasDefault := column.GetDefaultValue() != nil
- if hasDefault {
- _, isDefaultNull := column.GetDefaultValue().(*storepb.ColumnMetadata_DefaultNull)
- // Some types do not default to NULL, but support default expressions.
- _, ok := expressionDefaultOnlyTypes[strings.ToUpper(column.Type)]
- if isDefaultNull && ok {
- hasDefault = false
- }
- }
-
result := &columnState{
- id: id,
- name: column.Name,
- tp: column.Type,
- hasDefault: hasDefault,
- nullable: column.Nullable,
- comment: column.Comment,
- }
- if result.hasDefault {
+ id: id,
+ name: column.Name,
+ tp: column.Type,
+ nullable: column.Nullable,
+ comment: column.Comment,
+ }
+ if column.GetDefaultValue() != nil {
switch value := column.GetDefaultValue().(type) {
case *storepb.ColumnMetadata_DefaultNull:
result.defaultValue = &defaultValueNull{}
diff --git a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
index 9f5364449d..bb762f8615 100644
--- a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
+++ b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
@@ -5,6 +5,7 @@
`b` VARCHAR(20) NOT NULL,
`c` int NOT NULL,
`d` text NULL,
+ `t_not_null` text NOT NULL,
`e` varchar(20),
PRIMARY KEY (`a`),
UNIQUE KEY `t_c_uniq_idx` (`c` DESC),
@@ -17,141 +18,147 @@
) ENGINE=InnoDB COLLATE=utf8mb4_0900_ai_ci COMMENT 'this is comment with '' escaped';
metadata: |-
{
- "schemas": [
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t",
- "columns": [
+ "name": "t",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "type": "int",
- "comment": "this is comment"
+ "name": "a",
+ "position": 1,
+ "type": "int",
+ "comment": "this is comment"
},
{
- "name": "b",
- "position": 2,
- "type": "VARCHAR(20)"
+ "name": "b",
+ "position": 2,
+ "type": "VARCHAR(20)"
},
{
- "name": "c",
- "position": 3,
- "type": "int"
+ "name": "c",
+ "position": 3,
+ "type": "int"
},
{
- "name": "d",
- "position": 4,
- "defaultNull": true,
- "nullable": true,
- "type": "text"
+ "name": "d",
+ "position": 4,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "text"
},
{
- "name": "e",
- "position": 5,
- "nullable": true,
- "type": "varchar(20)"
+ "name": "t_not_null",
+ "position": 5,
+ "type": "text"
+ },
+ {
+ "name": "e",
+ "position": 6,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "varchar(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "t_c_uniq_idx",
- "expressions": [
+ "name": "t_c_uniq_idx",
+ "expressions": [
"c"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
},
{
- "name": "t_c_plus_5_uniq_idx",
- "expressions": [
+ "name": "t_c_plus_5_uniq_idx",
+ "expressions": [
"((`c` + 5))"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
},
{
- "name": "t_c_plus_5_t_c_plus_3_uniq_idx",
- "expressions": [
+ "name": "t_c_plus_5_t_c_plus_3_uniq_idx",
+ "expressions": [
"((`c` + 5))",
"((`c` + 3))"
],
- "keyLength": [
+ "keyLength": [
"-1",
"-1"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
},
{
- "name": "idx_c",
- "expressions": [
+ "name": "idx_c",
+ "expressions": [
"c"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "visible": true
},
{
- "name": "idx_d",
- "expressions": [
+ "name": "idx_d",
+ "expressions": [
"d"
],
- "keyLength": [
+ "keyLength": [
"10"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "visible": true
},
{
- "name": "idx_e",
- "expressions": [
+ "name": "idx_e",
+ "expressions": [
"e"
],
- "keyLength": [
+ "keyLength": [
"10"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "visible": true
},
{
- "name": "t_d_fulltext_idx",
- "expressions": [
+ "name": "t_d_fulltext_idx",
+ "expressions": [
"d"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "type": "FULLTEXT",
- "visible": true
+ "type": "FULLTEXT",
+ "visible": true
}
],
- "engine": "InnoDB",
- "collation": "utf8mb4_0900_ai_ci",
- "comment": "this is comment with ' escaped"
+ "engine": "InnoDB",
+ "collation": "utf8mb4_0900_ai_ci",
+ "comment": "this is comment with ' escaped"
}
]
}
@@ -169,87 +176,87 @@
create table t2(b int NOT NULL default NULL, primary key (b));
metadata: |-
{
- "schemas": [
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t",
- "columns": [
+ "name": "t",
+ "columns": [
{
- "name": "c",
- "position": 1,
- "defaultExpression": "AUTO_INCREMENT",
- "nullable": true,
- "type": "int"
+ "name": "c",
+ "position": 1,
+ "defaultExpression": "AUTO_INCREMENT",
+ "nullable": true,
+ "type": "int"
},
{
- "name": "a",
- "position": 2,
- "defaultExpression": "1",
- "nullable": true,
- "type": "int",
- "comment": "abcdefg"
+ "name": "a",
+ "position": 2,
+ "defaultExpression": "1",
+ "nullable": true,
+ "type": "int",
+ "comment": "abcdefg"
},
{
- "name": "b",
- "position": 3,
- "defaultNull": true,
- "nullable": true,
- "type": "varchar(20)"
+ "name": "b",
+ "position": 3,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "varchar(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a",
"b"
],
- "keyLength": [
+ "keyLength": [
"-1",
"-1"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
],
- "comment": "this is comment with ' escaped",
- "foreignKeys": [
+ "comment": "this is comment with ' escaped",
+ "foreignKeys": [
{
- "name": "fk1",
- "columns": [
+ "name": "fk1",
+ "columns": [
"a"
],
- "referencedTable": "t2",
- "referencedColumns": [
+ "referencedTable": "t2",
+ "referencedColumns": [
"b"
]
}
]
},
{
- "name": "t2",
- "columns": [
+ "name": "t2",
+ "columns": [
{
- "name": "b",
- "position": 1,
- "defaultNull": true,
- "type": "int"
+ "name": "b",
+ "position": 1,
+ "defaultNull": true,
+ "type": "int"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"b"
],
- "keyLength": [
+ "keyLength": [
"-1"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
}
diff --git a/backend/plugin/schema/mysql/utils.go b/backend/plugin/schema/mysql/utils.go
index 4c6d073e1a..fe47844eb3 100644
--- a/backend/plugin/schema/mysql/utils.go
+++ b/backend/plugin/schema/mysql/utils.go
@@ -171,7 +171,7 @@ func extractNewAttrs(column *columnState, attrs []mysql.IColumnAttributeContext)
order: columnAttrOrder["NULL"],
})
}
- if !defaultExists && column.hasDefault {
+ if !defaultExists && column.defaultValue != nil {
// todo(zp): refactor column attribute.
if strings.EqualFold(column.defaultValue.toString(), "AUTO_INCREMENT") {
result = append(result, columnAttr{
@@ -179,10 +179,14 @@ func extractNewAttrs(column *columnState, attrs []mysql.IColumnAttributeContext)
order: columnAttrOrder["DEFAULT"],
})
} else {
- result = append(result, columnAttr{
- text: "DEFAULT " + column.defaultValue.toString(),
- order: columnAttrOrder["DEFAULT"],
- })
+ _, isNull := column.defaultValue.(*defaultValueNull)
+ dontWriteDefaultNull := isNull && column.nullable && expressionDefaultOnlyTypes[strings.ToUpper(column.tp)]
+ if !dontWriteDefaultNull {
+ result = append(result, columnAttr{
+ text: "DEFAULT " + column.defaultValue.toString(),
+ order: columnAttrOrder["DEFAULT"],
+ })
+ }
}
}
if !commentExists && column.comment != "" {