add tag on_update
This commit is contained in:
parent
b39ff4b4d3
commit
b1fd75fc95
|
@ -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
|
return bd.String(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -414,7 +414,9 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
if extra == "auto_increment" {
|
if extra == "auto_increment" {
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
}
|
}
|
||||||
|
if strings.Contains(extra, "ON UPDATE CURRENT_TIMESTAMP") {
|
||||||
|
col.IsOnUpdate = true
|
||||||
|
}
|
||||||
if !col.DefaultIsEmpty {
|
if !col.DefaultIsEmpty {
|
||||||
if !alreadyQuoted && col.SQLType.IsText() {
|
if !alreadyQuoted && col.SQLType.IsText() {
|
||||||
col.Default = "'" + col.Default + "'"
|
col.Default = "'" + col.Default + "'"
|
||||||
|
|
|
@ -30,6 +30,7 @@ type Column struct {
|
||||||
Length2 int
|
Length2 int
|
||||||
Nullable bool
|
Nullable bool
|
||||||
Default string
|
Default string
|
||||||
|
IsOnUpdate bool
|
||||||
Indexes map[string]int
|
Indexes map[string]int
|
||||||
IsPrimaryKey bool
|
IsPrimaryKey bool
|
||||||
IsAutoIncrement bool
|
IsAutoIncrement bool
|
||||||
|
|
|
@ -296,6 +296,7 @@ func (session *Session) Sync2(beans ...interface{}) error {
|
||||||
for _, col2 := range oriTable.Columns() {
|
for _, col2 := range oriTable.Columns() {
|
||||||
if strings.EqualFold(col.Name, col2.Name) {
|
if strings.EqualFold(col.Name, col2.Name) {
|
||||||
oriCol = col2
|
oriCol = col2
|
||||||
|
oriCol.IsOnUpdate = col.IsOnUpdate
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
45
tags/tag.go
45
tags/tag.go
|
@ -57,25 +57,26 @@ type Handler func(ctx *Context) error
|
||||||
var (
|
var (
|
||||||
// defaultTagHandlers enumerates all the default tag handler
|
// defaultTagHandlers enumerates all the default tag handler
|
||||||
defaultTagHandlers = map[string]Handler{
|
defaultTagHandlers = map[string]Handler{
|
||||||
"<-": OnlyFromDBTagHandler,
|
"<-": OnlyFromDBTagHandler,
|
||||||
"->": OnlyToDBTagHandler,
|
"->": OnlyToDBTagHandler,
|
||||||
"PK": PKTagHandler,
|
"PK": PKTagHandler,
|
||||||
"NULL": NULLTagHandler,
|
"NULL": NULLTagHandler,
|
||||||
"NOT": IgnoreTagHandler,
|
"NOT": IgnoreTagHandler,
|
||||||
"AUTOINCR": AutoIncrTagHandler,
|
"AUTOINCR": AutoIncrTagHandler,
|
||||||
"DEFAULT": DefaultTagHandler,
|
"DEFAULT": DefaultTagHandler,
|
||||||
"CREATED": CreatedTagHandler,
|
"CREATED": CreatedTagHandler,
|
||||||
"UPDATED": UpdatedTagHandler,
|
"UPDATED": UpdatedTagHandler,
|
||||||
"DELETED": DeletedTagHandler,
|
"DELETED": DeletedTagHandler,
|
||||||
"VERSION": VersionTagHandler,
|
"VERSION": VersionTagHandler,
|
||||||
"UTC": UTCTagHandler,
|
"UTC": UTCTagHandler,
|
||||||
"LOCAL": LocalTagHandler,
|
"LOCAL": LocalTagHandler,
|
||||||
"NOTNULL": NotNullTagHandler,
|
"NOTNULL": NotNullTagHandler,
|
||||||
"INDEX": IndexTagHandler,
|
"INDEX": IndexTagHandler,
|
||||||
"UNIQUE": UniqueTagHandler,
|
"UNIQUE": UniqueTagHandler,
|
||||||
"CACHE": CacheTagHandler,
|
"CACHE": CacheTagHandler,
|
||||||
"NOCACHE": NoCacheTagHandler,
|
"NOCACHE": NoCacheTagHandler,
|
||||||
"COMMENT": CommentTagHandler,
|
"COMMENT": CommentTagHandler,
|
||||||
|
"ON_UPDATE": OnUpdateTagHandler,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -215,6 +216,12 @@ func UniqueTagHandler(ctx *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnUpdateTagHandler
|
||||||
|
func OnUpdateTagHandler(ctx *Context) error {
|
||||||
|
ctx.col.IsOnUpdate = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// CommentTagHandler add comment to column
|
// CommentTagHandler add comment to column
|
||||||
func CommentTagHandler(ctx *Context) error {
|
func CommentTagHandler(ctx *Context) error {
|
||||||
if len(ctx.params) > 0 {
|
if len(ctx.params) > 0 {
|
||||||
|
|
Loading…
Reference in New Issue