Skip to content

Commit

Permalink
fix:: fix default value
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Aug 3, 2023
1 parent 9bacf67 commit 40d0c52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
26 changes: 13 additions & 13 deletions driver/mysql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
func Test_Tidb_SQL_Parse(t *testing.T) {
sql :=
"CREATE TABLE `announce` (" +
"`id` bigint NOT NULL AUTO_INCREMENT DEFAULT 'abc' COMMENT '主键'," +
"`title` varchar(255) NOT NULL COMMENT '标题'," +
"`content` varchar(2048) NOT NULL DEFAULT '{}' COMMENT '内容'," +
"`value1` float unsigned NOT NULL DEFAULT '1' COMMENT '值1'," +
"`value2` float(10,1) unsigned NOT NULL DEFAULT '2' COMMENT '值2'," +
"`value2` double(16,2) NOT NULL DEFAULT '3' COMMENT '值3'," +
"`value3` enum('00','SH') NOT NULL DEFAULT '4' COMMENT '值4'," +
"`created_at` datetime NOT NULL COMMENT '发布时间'," +
"`updated_at` datetime NOT NULL," +
"`deleted_at` bigint NOT NULL DEFAULT '0'," +
"PRIMARY KEY (`id`) USING BTREE," +
"KEY `uk_value1_value2` (`value1`,`value2`) USING BTREE," +
"CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`value1`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT" +
"`id` bigint NOT NULL AUTO_INCREMENT DEFAULT '111' COMMENT '主键'," +
"`title` varchar(255) DEFAULT NULL COMMENT '标题'" +
// "`content` varchar(2048) NOT NULL DEFAULT '{}' COMMENT '内容'," +
// "`value1` float unsigned NOT NULL DEFAULT '1' COMMENT '值1'," +
// "`value2` float(10,1) unsigned NOT NULL DEFAULT '2' COMMENT '值2'," +
// "`value2` double(16,2) NOT NULL DEFAULT '3' COMMENT '值3'," +
// "`value3` enum('00','SH') NOT NULL DEFAULT '4' COMMENT '值4'," +
// "`created_at` datetime NOT NULL COMMENT '发布时间'," +
// "`updated_at` datetime NOT NULL," +
// "`deleted_at` bigint NOT NULL DEFAULT '0'," +
// "PRIMARY KEY (`id`) USING BTREE," +
// "KEY `uk_value1_value2` (`value1`,`value2`) USING BTREE," +
// "CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`value1`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT" +
")ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='公告-面向所有人的消息';"

d := &SQLTidb{
Expand Down
10 changes: 7 additions & 3 deletions driver/mysql/sql_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ func parserCreateTableStmtColumn(col *ast.ColumnDef) (*schema.Column, error) {
case ast.ColumnOptionAutoIncrement:
coldef.AddAttrs(&mysql.AutoIncrement{})
case ast.ColumnOptionDefaultValue:
coldef.Default = &schema.Literal{V: formatExprNode(opt.Expr)}
val := formatExprNode(opt.Expr)
if !strings.EqualFold(val, "null") {
coldef.Default = &schema.Literal{V: strings.Trim(formatExprNode(opt.Expr), `"`)}
}

case ast.ColumnOptionComment:
coldef.AddAttrs(&schema.Comment{Text: formatExprNode(opt.Expr)})
coldef.AddAttrs(&schema.Comment{Text: strings.Trim(formatExprNode(opt.Expr), `"`)})
}
}

Expand Down Expand Up @@ -338,5 +342,5 @@ func formatExprNode(e ast.ExprNode) string {
}
b := &strings.Builder{}
e.Format(b)
return strings.Trim(b.String(), `"`)
return b.String()
}

0 comments on commit 40d0c52

Please sign in to comment.