Skip to content

Commit

Permalink
fix(gen): fix #112
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Nov 19, 2024
1 parent c07b527 commit 1a7f94f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 12 additions & 5 deletions internal/gen/genmodel/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 1a7f94f

Please sign in to comment.