From 415c4c91fc5d5ea06be4f06e7ab2a01021ecbd67 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 9 Apr 2022 00:25:23 +0800 Subject: [PATCH] fix: model unique keys generated differently in each re-generation (#1771) --- tools/goctl/model/sql/gen/delete.go | 9 +++++++-- tools/goctl/model/sql/gen/findonebyfield.go | 3 ++- tools/goctl/model/sql/gen/insert.go | 9 +++++++-- tools/goctl/model/sql/gen/update.go | 9 +++++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/tools/goctl/model/sql/gen/delete.go b/tools/goctl/model/sql/gen/delete.go index 55af120d..0b000518 100644 --- a/tools/goctl/model/sql/gen/delete.go +++ b/tools/goctl/model/sql/gen/delete.go @@ -1,6 +1,7 @@ package gen import ( + "sort" "strings" "github.com/zeromicro/go-zero/core/collection" @@ -19,6 +20,10 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) keySet.AddStr(key.DataKeyExpression) keyVariableSet.AddStr(key.KeyLeft) } + keys := keySet.KeysStr() + sort.Strings(keys) + keyVars := keyVariableSet.KeysStr() + sort.Strings(keyVars) camel := table.Name.ToCamel() text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete) @@ -34,9 +39,9 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) "containsIndexCache": table.ContainsUniqueCacheKey, "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "dataType": table.PrimaryKey.DataType, - "keys": strings.Join(keySet.KeysStr(), "\n"), + "keys": strings.Join(keys, "\n"), "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), - "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), + "keyValues": strings.Join(keyVars, ", "), "postgreSql": postgreSql, "data": table, }) diff --git a/tools/goctl/model/sql/gen/findonebyfield.go b/tools/goctl/model/sql/gen/findonebyfield.go index 97abf4cf..8c4f8aef 100644 --- a/tools/goctl/model/sql/gen/findonebyfield.go +++ b/tools/goctl/model/sql/gen/findonebyfield.go @@ -82,7 +82,8 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e } if withCache { - text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod) + text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, + template.FindOneByFieldExtraMethod) if err != nil { return nil, err } diff --git a/tools/goctl/model/sql/gen/insert.go b/tools/goctl/model/sql/gen/insert.go index 1ca7b312..36cfe8e1 100644 --- a/tools/goctl/model/sql/gen/insert.go +++ b/tools/goctl/model/sql/gen/insert.go @@ -2,6 +2,7 @@ package gen import ( "fmt" + "sort" "strings" "github.com/zeromicro/go-zero/core/collection" @@ -20,6 +21,10 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) keySet.AddStr(key.DataKeyExpression) keyVariableSet.AddStr(key.KeyLeft) } + keys := keySet.KeysStr() + sort.Strings(keys) + keyVars := keyVariableSet.KeysStr() + sort.Strings(keyVars) expressions := make([]string, 0) expressionValues := make([]string, 0) @@ -59,8 +64,8 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) "lowerStartCamelObject": stringx.From(camel).Untitle(), "expression": strings.Join(expressions, ", "), "expressionValues": strings.Join(expressionValues, ", "), - "keys": strings.Join(keySet.KeysStr(), "\n"), - "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), + "keys": strings.Join(keys, "\n"), + "keyValues": strings.Join(keyVars, ", "), "data": table, }) if err != nil { diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index 5dab0706..57ce3625 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -1,6 +1,7 @@ package gen import ( + "sort" "strings" "github.com/zeromicro/go-zero/core/collection" @@ -33,6 +34,10 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) keySet.AddStr(key.DataKeyExpression) keyVariableSet.AddStr(key.KeyLeft) } + keys := keySet.KeysStr() + sort.Strings(keys) + keyVars := keyVariableSet.KeysStr() + sort.Strings(keyVars) if postgreSql { expressionValues = append([]string{"data." + table.PrimaryKey.Name.ToCamel()}, expressionValues...) @@ -50,8 +55,8 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) Execute(map[string]interface{}{ "withCache": withCache, "upperStartCamelObject": camelTableName, - "keys": strings.Join(keySet.KeysStr(), "\n"), - "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), + "keys": strings.Join(keys, "\n"), + "keyValues": strings.Join(keyVars, ", "), "primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression, "primaryKeyVariable": table.PrimaryCacheKey.KeyLeft, "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),