From 1a7f94fd92ced7ba45958366ffd31e04d7ae74bc Mon Sep 17 00:00:00 2001 From: jaron Date: Tue, 19 Nov 2024 13:59:14 +0800 Subject: [PATCH] fix(gen): fix https://github.com/jzero-io/jzero/issues/112 --- cmd/gen.go | 1 + config/config.go | 1 + internal/gen/genmodel/gen.go | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/gen.go b/cmd/gen.go index 2a85e3d2..2c830448 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -269,6 +269,7 @@ func init() { genCmd.Flags().BoolP("model-mysql-strict", "", false, "goctl model mysql strict mode, see [https://go-zero.dev/docs/tutorials/cli/model]") genCmd.Flags().StringSliceP("model-mysql-ignore-columns", "", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"}, "ignore columns of mysql model") + genCmd.Flags().StringP("model-mysql-ddl-database", "", "", "goctl model mysql ddl database") genCmd.Flags().BoolP("model-mysql-datasource", "", false, "goctl model mysql datasource") genCmd.Flags().StringP("model-mysql-datasource-url", "", "", "goctl model mysql datasource url") genCmd.Flags().StringSliceP("model-mysql-datasource-table", "", []string{"*"}, "goctl model mysql datasource table") diff --git a/config/config.go b/config/config.go index df1259c9..69e1b62d 100644 --- a/config/config.go +++ b/config/config.go @@ -84,6 +84,7 @@ type GenConfig struct { ModelMysqlStrict bool `mapstructure:"model-mysql-strict"` ModelMysqlIgnoreColumns []string `mapstructure:"model-mysql-ignore-columns"` + ModelMysqlDDLDatabase string `mapstructure:"model-mysql-ddl-database"` ModelMysqlDatasource bool `mapstructure:"model-mysql-datasource"` ModelMysqlDatasourceUrl string `mapstructure:"model-mysql-datasource-url"` ModelMysqlDatasourceTable []string `mapstructure:"model-mysql-datasource-table"` diff --git a/internal/gen/genmodel/gen.go b/internal/gen/genmodel/gen.go index 78441fa8..602e0f05 100644 --- a/internal/gen/genmodel/gen.go +++ b/internal/gen/genmodel/gen.go @@ -102,8 +102,15 @@ func (jm *JzeroModel) Gen() error { } if jm.ModelMysqlCachePrefix != "" && jm.ModelMysqlCache { - namingFormat, _ := format.FileNamingFormat(table, jm.Style) - err = jm.addModelMysqlCachePrefix(filepath.Join("internal", "model", strings.ToLower(table), namingFormat+"model_gen.go")) + namingFormat, err := format.FileNamingFormat(jm.Style, table) + if err != nil { + return + } + file := namingFormat + "model_gen.go" + if jm.Style == "go_zero" { + file = namingFormat + "_model_gen.go" + } + err = jm.addModelMysqlCachePrefix(filepath.Join("internal", "model", strings.ToLower(table), file)) if err != nil { console.Warning("[warning]: %s", err.Error()) return @@ -149,7 +156,7 @@ func (jm *JzeroModel) Gen() error { genCodeFiles = append(genCodeFiles, v) } } else { - specifiedSqlFiles, err := jzerodesc.FindApiFiles(v) + specifiedSqlFiles, err := jzerodesc.FindSqlFiles(v) if err != nil { return err } @@ -187,7 +194,7 @@ func (jm *JzeroModel) Gen() error { fmt.Printf("%s to generate model code from sql files.\n", color.WithColor("Start", color.FgGreen)) for _, f := range genCodeFiles { fmt.Printf("%s sql file %s\n", color.WithColor("Using", color.FgGreen), f) - tableParsers, err := parser.Parse(filepath.Join(jm.Wd, f), "", jm.ModelMysqlStrict) + tableParsers, err := parser.Parse(filepath.Join(jm.Wd, f), jm.ModelMysqlDDLDatabase, jm.ModelMysqlStrict) if err != nil { return err } @@ -198,7 +205,7 @@ func (jm *JzeroModel) Gen() error { bf := filepath.Base(f) modelDir := filepath.Join("internal", "model", strings.ToLower(bf[0:len(bf)-len(path.Ext(bf))])) - cmd := exec.Command("goctl", "model", "mysql", "ddl", "--src", f, "--dir", modelDir, "--home", goctlHome, "--style", jm.Style, "-i", strings.Join(jm.ModelMysqlIgnoreColumns, ","), "--cache="+fmt.Sprintf("%t", jm.ModelMysqlCache), "--strict="+fmt.Sprintf("%t", jm.ModelMysqlStrict)) + cmd := exec.Command("goctl", "model", "mysql", "ddl", "--database", jm.ModelMysqlDDLDatabase, "--src", f, "--dir", modelDir, "--home", goctlHome, "--style", jm.Style, "-i", strings.Join(jm.ModelMysqlIgnoreColumns, ","), "--cache="+fmt.Sprintf("%t", jm.ModelMysqlCache), "--strict="+fmt.Sprintf("%t", jm.ModelMysqlStrict)) resp, err := cmd.CombinedOutput() if err != nil { return errors.Errorf("gen model code meet error. Err: %s:%s", err.Error(), resp)