feat:add cache prefix support to pg (#4741)

Co-authored-by: 郑好 <zheng.hao1@outlook.com>
This commit is contained in:
hoshi
2025-05-02 16:10:47 +08:00
committed by GitHub
parent e416d01f8d
commit 808b4e496a
3 changed files with 6 additions and 3 deletions

View File

@@ -185,6 +185,7 @@
}, },
"pg": { "pg": {
"short": "Generate postgresql model", "short": "Generate postgresql model",
"prefix": "The cache prefix, effective when --cache is true",
"datasource": { "datasource": {
"short": "Generate model from datasource", "short": "Generate model from datasource",
"url": "The data source of database,like \"postgres://root:password@127.0.0.1:5432/database?sslmode=disable\"", "url": "The data source of database,like \"postgres://root:password@127.0.0.1:5432/database?sslmode=disable\"",

View File

@@ -58,6 +58,7 @@ func init() {
pgDatasourceCmdFlags.StringVar(&command.VarStringBranch, "branch") pgDatasourceCmdFlags.StringVar(&command.VarStringBranch, "branch")
pgCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns, pgCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns,
"ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"}) "ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"})
pgCmd.PersistentFlags().StringVarPWithDefaultValue(&command.VarStringCachePrefix, "prefix", "p", "cache")
mongoCmdFlags.StringSliceVarP(&mongo.VarStringSliceType, "type", "t") mongoCmdFlags.StringSliceVarP(&mongo.VarStringSliceType, "type", "t")
mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c") mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c")

View File

@@ -204,6 +204,7 @@ func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
home := VarStringHome home := VarStringHome
remote := VarStringRemote remote := VarStringRemote
branch := VarStringBranch branch := VarStringBranch
prefix := VarStringCachePrefix
if len(remote) > 0 { if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch) repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 { if len(repo) > 0 {
@@ -225,7 +226,7 @@ func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
} }
ignoreColumns := mergeColumns(VarStringSliceIgnoreColumns) ignoreColumns := mergeColumns(VarStringSliceIgnoreColumns)
return fromPostgreSqlDataSource(url, patterns, dir, schema, cfg, cache, idea, VarBoolStrict, ignoreColumns) return fromPostgreSqlDataSource(url, patterns, dir, schema, prefix, cfg, cache, idea, VarBoolStrict, ignoreColumns)
} }
type ddlArg struct { type ddlArg struct {
@@ -339,7 +340,7 @@ func fromMysqlDataSource(arg dataSourceArg) error {
return generator.StartFromInformationSchema(matchTables, arg.cache, arg.strict) return generator.StartFromInformationSchema(matchTables, arg.cache, arg.strict)
} }
func fromPostgreSqlDataSource(url string, pattern pattern, dir, schema string, cfg *config.Config, cache, idea, strict bool, ignoreColumns []string) error { func fromPostgreSqlDataSource(url string, pattern pattern, dir, schema string, prefix string, cfg *config.Config, cache, idea, strict bool, ignoreColumns []string) error {
log := console.NewConsole(idea) log := console.NewConsole(idea)
if len(url) == 0 { if len(url) == 0 {
log.Error("%v", "expected data source of postgresql, but nothing found") log.Error("%v", "expected data source of postgresql, but nothing found")
@@ -381,7 +382,7 @@ func fromPostgreSqlDataSource(url string, pattern pattern, dir, schema string, c
return errors.New("no tables matched") return errors.New("no tables matched")
} }
generator, err := gen.NewDefaultGenerator("", dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns)) generator, err := gen.NewDefaultGenerator(prefix, dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns))
if err != nil { if err != nil {
return err return err
} }