Skip to content

Commit

Permalink
feat: 在数据库配置中指定时间维护字段
Browse files Browse the repository at this point in the history
fixed: 取消pgsql的default声明, pgsql的default大多为函数,会影响数据更新,应交由pgsql自身处理
  • Loading branch information
zodial committed Nov 21, 2024
1 parent 1b10607 commit 2746bc4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
7 changes: 6 additions & 1 deletion config/database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ connections:
port: env("DB_PORT")
database: env("DB_DATABASE")
username: env("DB_USERNAME")
password: env("DB_PASSWORD")
password: env("DB_PASSWORD")
# If you need to specify maintenance time fields, declare the following settings.
# The following values are the default; if they are consistent, you do not need to declare them.
#created_field: created_at
#updated_field: updated_at
#deleted_field: deleted_at # The soft delete mechanism in Gorm.
16 changes: 12 additions & 4 deletions console/commands/orm/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ func getImports(infos map[string]TableInfos, tableColumns map[string][]tableColu

func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships []*Relationship) string {
TableName := parser.StringToHump(table)

config := services.NewConfig(conf)
deletedField := config.GetString("deleted_field")
hasField := make(map[string]bool)
str := `type {TableName} struct {`
for _, column := range columns {
p := ""
if *column.IS_NULLABLE == "YES" && !(column.COLUMN_NAME == "deleted_at" && column.GoType == "database.Time") {
p = "*"
}
if column.COLUMN_NAME == "deleted_at" && column.GoType == "database.Time" {
if column.GoType == "database.Time" && (column.COLUMN_NAME == deletedField || (deletedField == "" && column.COLUMN_NAME == "deleted_at")) {
column.GoType = "gorm.DeletedAt"
}

Expand All @@ -278,7 +279,7 @@ func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships

hasField[column.COLUMN_NAME] = true
fieldName := parser.StringToHump(column.COLUMN_NAME)
str += fmt.Sprintf("\n\t%v %v%v`%v json:\"%v\"` // %v", fieldName, p, column.GoType, genGormTag(column), column.COLUMN_NAME, strings.ReplaceAll(column.COLUMN_COMMENT, "\n", " "))
str += fmt.Sprintf("\n\t%v %v%v`%v json:\"%v\"` // %v", fieldName, p, column.GoType, genGormTag(column, conf), column.COLUMN_NAME, strings.ReplaceAll(column.COLUMN_COMMENT, "\n", " "))
}
// 表关系
if len(relationships) > 0 {
Expand Down Expand Up @@ -333,7 +334,7 @@ func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships
return "\n" + str + "\n"
}

func genGormTag(column tableColumn) string {
func genGormTag(column tableColumn, conf Conf) string {
var arr []string
// 字段
arr = append(arr, "column:"+column.COLUMN_NAME)
Expand Down Expand Up @@ -369,6 +370,13 @@ func genGormTag(column tableColumn) string {
if column.COLUMN_DEFAULT != nil {
arr = append(arr, "default:"+*column.COLUMN_DEFAULT)
}
// created_at & updated_at
if field, ok := conf["created_field"]; ok && field == column.ColumnName {
arr = append(arr, "autoCreateTime")
}
if field, ok := conf["updated_field"]; ok && field == column.ColumnName {
arr = append(arr, "autoUpdateTime")
}

if column.COLUMN_COMMENT != "" {
arr = append(arr, fmt.Sprintf("comment:'%v'", strings.ReplaceAll(column.COLUMN_COMMENT, "'", "")))
Expand Down
26 changes: 13 additions & 13 deletions console/commands/pgorm/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,16 @@ func getImports(infos map[string]orm.TableInfos, tableColumns map[string][]table

func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships []*orm.Relationship) string {
TableName := parser.StringToHump(table)

config := services.NewConfig(conf)
deletedField := config.GetString("deleted_field")
hasField := make(map[string]bool)
str := `type {TableName} struct {`
for _, column := range columns {
p := ""
if column.IsNullable && !(column.ColumnName == "deleted_at" && column.GoType == "database.Time") && column.PgType != "bytea" {
p = "*"
}
if column.ColumnName == "deleted_at" && column.GoType == "database.Time" {
if column.GoType == "database.Time" && (column.ColumnName == deletedField || (deletedField == "" && column.ColumnName == "deleted_at")) {
column.GoType = "gorm.DeletedAt"
}

Expand All @@ -273,7 +274,7 @@ func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships

hasField[column.ColumnName] = true
fieldName := parser.StringToHump(column.ColumnName)
str += fmt.Sprintf("\n\t%v %v%v`%v` // %v", fieldName, p, column.GoType, genGormTag(column), strings.ReplaceAll(column.Comment, "\n", " "))
str += fmt.Sprintf("\n\t%v %v%v`%v` // %v", fieldName, p, column.GoType, genGormTag(column, conf), strings.ReplaceAll(column.Comment, "\n", " "))
}
// 表关系
if len(relationships) > 0 {
Expand Down Expand Up @@ -328,7 +329,7 @@ func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships
return "\n" + str + "\n"
}

func genGormTag(column tableColumn) string {
func genGormTag(column tableColumn, conf Conf) string {
var arr []string
// 字段
arr = append(arr, "column:"+column.ColumnName)
Expand All @@ -346,11 +347,14 @@ func genGormTag(column tableColumn) string {
} else if column.IndexName != "" {
arr = append(arr, "index:"+column.ColumnName)
}
// default
if column.ColumnDefault != "" {
arr = append(arr, "default:"+column.ColumnDefault)
// pgsql取消default声明, pgsql的default大多为函数,会影响数据更新,由pgsql自身处理
// created_at & updated_at
if field, ok := conf["created_field"]; ok && field == column.ColumnName {
arr = append(arr, "autoCreateTime")
}
if field, ok := conf["updated_field"]; ok && field == column.ColumnName {
arr = append(arr, "autoUpdateTime")
}

if column.Comment != "" {
arr = append(arr, fmt.Sprintf("comment:'%v'", strings.ReplaceAll(column.Comment, "'", "")))
}
Expand Down Expand Up @@ -529,11 +533,7 @@ func PgTypeToGoType(pgType string, columnName string) string {
return "[]byte"
default:
if strings.Contains(pgType, "timestamp") {
if columnName == "deleted_at" {
return "gorm.DeletedAt"
} else {
return "database.Time"
}
return "database.Time"
}
return "string"
}
Expand Down

0 comments on commit 2746bc4

Please sign in to comment.