add tag on_update

This commit is contained in:
Jerry 2020-10-11 23:35:15 +08:00 committed by Jerry
parent b39ff4b4d3
commit b1fd75fc95
5 changed files with 40 additions and 20 deletions

View File

@ -280,5 +280,14 @@ func ColumnString(dialect Dialect, col *schemas.Column, includePrimaryKey bool)
}
}
if col.IsOnUpdate {
if _, err := bd.WriteString("ON UPDATE CURRENT_TIMESTAMP"); err != nil {
return "", err
}
if err := bd.WriteByte(' '); err != nil {
return "", err
}
}
return bd.String(), nil
}

View File

@ -414,7 +414,9 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
if extra == "auto_increment" {
col.IsAutoIncrement = true
}
if strings.Contains(extra, "ON UPDATE CURRENT_TIMESTAMP") {
col.IsOnUpdate = true
}
if !col.DefaultIsEmpty {
if !alreadyQuoted && col.SQLType.IsText() {
col.Default = "'" + col.Default + "'"

View File

@ -30,6 +30,7 @@ type Column struct {
Length2 int
Nullable bool
Default string
IsOnUpdate bool
Indexes map[string]int
IsPrimaryKey bool
IsAutoIncrement bool

View File

@ -296,6 +296,7 @@ func (session *Session) Sync2(beans ...interface{}) error {
for _, col2 := range oriTable.Columns() {
if strings.EqualFold(col.Name, col2.Name) {
oriCol = col2
oriCol.IsOnUpdate = col.IsOnUpdate
break
}
}

View File

@ -76,6 +76,7 @@ var (
"CACHE": CacheTagHandler,
"NOCACHE": NoCacheTagHandler,
"COMMENT": CommentTagHandler,
"ON_UPDATE": OnUpdateTagHandler,
}
)
@ -215,6 +216,12 @@ func UniqueTagHandler(ctx *Context) error {
return nil
}
// OnUpdateTagHandler
func OnUpdateTagHandler(ctx *Context) error {
ctx.col.IsOnUpdate = true
return nil
}
// CommentTagHandler add comment to column
func CommentTagHandler(ctx *Context) error {
if len(ctx.params) > 0 {