Skip to content

Commit d55db7f

Browse files
committed
feat: add generate auto time tag logic
1 parent f5baa71 commit d55db7f

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

util/db.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ func getColumnInfos(c *CmdConfig) ([]*ColumnInfo, error) {
131131
IsUnsigned: strings.Contains(ct, "unsigned") && !c.DisableUnsigned, IsNullable: in == "YES",
132132
}
133133

134+
if isTimeType(ci.DataType) {
135+
switch ci.Name {
136+
case createAt:
137+
ci.Another = autoCreateTime
138+
case updateAt:
139+
ci.Another = autoUpdateTime
140+
}
141+
}
134142
ci.Indexes, ci.UniqueIndexes = getColumnIndexInfos(indexInfos, ci.Name)
135143
columnInfos = append(columnInfos, &ci)
136144
}

util/define.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ type ColumnInfo struct {
155155
IsAutoIncrement bool `mysql:"EXTRA"`
156156
IsUnsigned bool `mysql:"COLUMN_TYPE"`
157157
IsNullable bool `mysql:"IS_NULLABLE"`
158+
Another string `mysql:"-"`
158159
Indexes []*IndexInfo `mysql:"-"`
159160
UniqueIndexes []*IndexInfo `mysql:"-"`
160161
}

util/template.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
"github.com/pkg/errors"
1313
)
1414

15+
const (
16+
createAt = "create_at"
17+
updateAt = "update_at"
18+
autoCreateTime = "autoCreateTime:milli"
19+
autoUpdateTime = "autoUpdateTime:milli"
20+
)
21+
1522
var (
1623
generator *template.Template
1724

@@ -139,3 +146,15 @@ func uniqueStrings(slice []string) []string {
139146

140147
return result
141148
}
149+
150+
// isTimeType reports whether the data type is time type.
151+
func isTimeType(dataType string) bool {
152+
timeTypes := []string{"year", "date", "datetime", "time", "timestamp"}
153+
for _, timeType := range timeTypes {
154+
if dataType == timeType {
155+
return true
156+
}
157+
}
158+
159+
return false
160+
}

util/tpl/gormv2.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ gorm:"
77
{{- range $i, $v := .UniqueIndexes }}
88
{{- if eq $i 0 }};uniqueIndex:{{ $v.Name }}{{ else }},{{ $v.Name }}{{ end }}{{ end -}}
99
{{- if .Default }};default:{{ .Default }}{{ end -}}
10+
{{- if .Another }};{{ .Another }}{{ end -}}
1011
{{- if .Comment }};comment:{{ .Comment }}{{ end -}}
1112
"

0 commit comments

Comments
 (0)