Skip to content

Commit 7a4818d

Browse files
authored
Generate caches that support custom key prefix. (#4643)
1 parent 48d0709 commit 7a4818d

File tree

9 files changed

+60
-45
lines changed

9 files changed

+60
-45
lines changed

tools/goctl/internal/flags/default_en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
"short": "Generate mysql model",
152152
"strict": "Generate model in strict mode",
153153
"ignore-columns": "Ignore columns while creating or updating rows",
154+
"prefix": "The cache prefix, effective when --cache is true",
154155
"datasource": {
155156
"short": "Generate model from datasource",
156157
"url": "The data source of database,like \"root:password@tcp(127.0.0.1:3306)/database",

tools/goctl/model/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func init() {
7171
mysqlCmd.PersistentFlags().BoolVar(&command.VarBoolStrict, "strict")
7272
mysqlCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns,
7373
"ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"})
74+
mysqlCmd.PersistentFlags().StringVarPWithDefaultValue(&command.VarStringCachePrefix, "prefix", "p", "cache")
7475

7576
mysqlCmd.AddCommand(datasourceCmd, ddlCmd)
7677
pgCmd.AddCommand(pgDatasourceCmd)

tools/goctl/model/sql/command/command.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,18 @@ var (
5050
VarBoolStrict bool
5151
// VarStringSliceIgnoreColumns represents the columns which are ignored.
5252
VarStringSliceIgnoreColumns []string
53+
// VarStringCachePrefix describes the prefix of cache.
54+
VarStringCachePrefix string
5355
)
5456

5557
var errNotMatched = errors.New("sql not matched")
5658

5759
// MysqlDDL generates model code from ddl
5860
func MysqlDDL(_ *cobra.Command, _ []string) error {
5961
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
62+
if VarBoolCache && len(VarStringCachePrefix) == 0 {
63+
return errors.New("cache prefix is empty")
64+
}
6065
src := VarStringSrc
6166
dir := VarStringDir
6267
cache := VarBoolCache
@@ -89,13 +94,17 @@ func MysqlDDL(_ *cobra.Command, _ []string) error {
8994
database: database,
9095
strict: VarBoolStrict,
9196
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
97+
prefix: VarStringCachePrefix,
9298
}
9399
return fromDDL(arg)
94100
}
95101

96102
// MySqlDataSource generates model code from datasource
97103
func MySqlDataSource(_ *cobra.Command, _ []string) error {
98104
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
105+
if VarBoolCache && len(VarStringCachePrefix) == 0 {
106+
return errors.New("cache prefix is empty")
107+
}
99108
url := strings.TrimSpace(VarStringURL)
100109
dir := strings.TrimSpace(VarStringDir)
101110
cache := VarBoolCache
@@ -130,6 +139,7 @@ func MySqlDataSource(_ *cobra.Command, _ []string) error {
130139
idea: idea,
131140
strict: VarBoolStrict,
132141
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
142+
prefix: VarStringCachePrefix,
133143
}
134144
return fromMysqlDataSource(arg)
135145
}
@@ -225,6 +235,7 @@ type ddlArg struct {
225235
database string
226236
strict bool
227237
ignoreColumns []string
238+
prefix string
228239
}
229240

230241
func fromDDL(arg ddlArg) error {
@@ -243,7 +254,7 @@ func fromDDL(arg ddlArg) error {
243254
return errNotMatched
244255
}
245256

246-
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
257+
generator, err := gen.NewDefaultGenerator(arg.prefix, arg.dir, arg.cfg,
247258
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
248259
if err != nil {
249260
return err
@@ -266,6 +277,7 @@ type dataSourceArg struct {
266277
cache, idea bool
267278
strict bool
268279
ignoreColumns []string
280+
prefix string
269281
}
270282

271283
func fromMysqlDataSource(arg dataSourceArg) error {
@@ -318,7 +330,7 @@ func fromMysqlDataSource(arg dataSourceArg) error {
318330
return errors.New("no tables matched")
319331
}
320332

321-
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
333+
generator, err := gen.NewDefaultGenerator(arg.prefix, arg.dir, arg.cfg,
322334
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
323335
if err != nil {
324336
return err
@@ -369,7 +381,7 @@ func fromPostgreSqlDataSource(url string, pattern pattern, dir, schema string, c
369381
return errors.New("no tables matched")
370382
}
371383

372-
generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns))
384+
generator, err := gen.NewDefaultGenerator("", dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns))
373385
if err != nil {
374386
return err
375387
}

tools/goctl/model/sql/example/makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
# generate model with cache from ddl
44
fromDDLWithCache:
55
goctl template clean
6-
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache
6+
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache --prefix gozero
77

88
fromDDLWithCacheAndIgnoreColumns:
99
goctl template clean
10-
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at'
10+
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at' --prefix gozero
1111

1212
fromDDLWithCacheAndDb:
1313
goctl template clean
14-
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache
14+
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache --prefix gozero
1515

1616
fromDDLWithoutCache:
1717
goctl template clean;
1818
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/nocache"
1919

2020

2121
# generate model with cache from data source
22-
user=root
23-
password=password
24-
datasource=127.0.0.1:3306
25-
database=gozero
22+
user=app_user
23+
password=PLO75FbcfmFYRuQEGmygZ9PyQCQbmgeD5
24+
datasource=k8s-istiosys-unifydev-2cdcebd306-b64e09be84220820.elb.ap-southeast-1.amazonaws.com:3306
25+
database=db_fiat
2626

2727
fromDataSource:
2828
goctl template clean
29-
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style gozero
29+
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="biz_switch" -dir ./model/cache -c -style gozero --prefix gozero

tools/goctl/model/sql/gen/gen.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type (
3030
cfg *config.Config
3131
isPostgreSql bool
3232
ignoreColumns []string
33+
prefix string
3334
}
3435

3536
// Option defines a function with argument defaultGenerator
@@ -56,7 +57,7 @@ type (
5657
)
5758

5859
// NewDefaultGenerator creates an instance for defaultGenerator
59-
func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) {
60+
func NewDefaultGenerator(prefix, dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) {
6061
if dir == "" {
6162
dir = pwd
6263
}
@@ -72,7 +73,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
7273
return nil, err
7374
}
7475

75-
generator := &defaultGenerator{dir: dir, cfg: cfg, pkg: pkg}
76+
generator := &defaultGenerator{dir: dir, cfg: cfg, pkg: pkg, prefix: prefix}
7677
var optionList []Option
7778
optionList = append(optionList, newDefaultOption())
7879
optionList = append(optionList, opt...)
@@ -260,7 +261,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
260261
return "", fmt.Errorf("table %s: missing primary key", in.Name.Source())
261262
}
262263

263-
primaryKey, uniqueKey := genCacheKeys(in)
264+
primaryKey, uniqueKey := genCacheKeys(g.prefix, in)
264265

265266
var table Table
266267
table.Table = in

tools/goctl/model/sql/gen/gen_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCacheModel(t *testing.T) {
3434
dir := filepath.Join(pathx.MustTempDir(), "./testmodel")
3535
cacheDir := filepath.Join(dir, "cache")
3636
noCacheDir := filepath.Join(dir, "nocache")
37-
g, err := NewDefaultGenerator(cacheDir, &config.Config{
37+
g, err := NewDefaultGenerator("cache", cacheDir, &config.Config{
3838
NamingFormat: "GoZero",
3939
})
4040
assert.Nil(t, err)
@@ -45,7 +45,7 @@ func TestCacheModel(t *testing.T) {
4545
_, err := os.Stat(filepath.Join(cacheDir, "TestUserModel.go"))
4646
return err == nil
4747
}())
48-
g, err = NewDefaultGenerator(noCacheDir, &config.Config{
48+
g, err = NewDefaultGenerator("cache", noCacheDir, &config.Config{
4949
NamingFormat: "gozero",
5050
})
5151
assert.Nil(t, err)
@@ -72,7 +72,7 @@ func TestNamingModel(t *testing.T) {
7272
defer func() {
7373
_ = os.RemoveAll(dir)
7474
}()
75-
g, err := NewDefaultGenerator(camelDir, &config.Config{
75+
g, err := NewDefaultGenerator("cache", camelDir, &config.Config{
7676
NamingFormat: "GoZero",
7777
})
7878
assert.Nil(t, err)
@@ -83,7 +83,7 @@ func TestNamingModel(t *testing.T) {
8383
_, err := os.Stat(filepath.Join(camelDir, "TestUserModel.go"))
8484
return err == nil
8585
}())
86-
g, err = NewDefaultGenerator(snakeDir, &config.Config{
86+
g, err = NewDefaultGenerator("cache", snakeDir, &config.Config{
8787
NamingFormat: "go_zero",
8888
})
8989
assert.Nil(t, err)
@@ -110,7 +110,7 @@ func TestFolderName(t *testing.T) {
110110
defer func() {
111111
_ = os.RemoveAll(dir)
112112
}()
113-
g, err := NewDefaultGenerator(camelDir, &config.Config{
113+
g, err := NewDefaultGenerator("cache", camelDir, &config.Config{
114114
NamingFormat: "GoZero",
115115
})
116116
assert.Nil(t, err)
@@ -125,7 +125,7 @@ func TestFolderName(t *testing.T) {
125125
}())
126126
assert.Equal(t, pkg, g.pkg)
127127

128-
g, err = NewDefaultGenerator(snakeDir, &config.Config{
128+
g, err = NewDefaultGenerator("cache", snakeDir, &config.Config{
129129
NamingFormat: "go_zero",
130130
})
131131
assert.Nil(t, err)
@@ -180,7 +180,7 @@ func Test_genPublicModel(t *testing.T) {
180180
err = os.WriteFile(modelFilename, []byte(source), 0o777)
181181
require.NoError(t, err)
182182

183-
g, err := NewDefaultGenerator(modelDir, &config.Config{
183+
g, err := NewDefaultGenerator("cache", modelDir, &config.Config{
184184
NamingFormat: config.DefaultFormat,
185185
})
186186
require.NoError(t, err)

tools/goctl/model/sql/gen/keys.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ type Key struct {
3737
// Join describes an alias of string slice
3838
type Join []string
3939

40-
func genCacheKeys(table parser.Table) (Key, []Key) {
40+
func genCacheKeys(prefix string, table parser.Table) (Key, []Key) {
4141
var primaryKey Key
4242
var uniqueKey []Key
43-
primaryKey = genCacheKey(table.Db, table.Name, []*parser.Field{&table.PrimaryKey.Field})
43+
primaryKey = genCacheKey(prefix, table.Db, table.Name, []*parser.Field{&table.PrimaryKey.Field})
4444
for _, each := range table.UniqueIndex {
45-
uniqueKey = append(uniqueKey, genCacheKey(table.Db, table.Name, each))
45+
uniqueKey = append(uniqueKey, genCacheKey(prefix, table.Db, table.Name, each))
4646
}
4747
sort.Slice(uniqueKey, func(i, j int) bool {
4848
return uniqueKey[i].VarLeft < uniqueKey[j].VarLeft
@@ -51,7 +51,7 @@ func genCacheKeys(table parser.Table) (Key, []Key) {
5151
return primaryKey, uniqueKey
5252
}
5353

54-
func genCacheKey(db, table stringx.String, in []*parser.Field) Key {
54+
func genCacheKey(prefix string, db, table stringx.String, in []*parser.Field) Key {
5555
var (
5656
varLeftJoin, varRightJoin, fieldNameJoin Join
5757
varLeft, varRight, varExpression string
@@ -62,12 +62,12 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key {
6262

6363
dbName, tableName := util.SafeString(db.Source()), util.SafeString(table.Source())
6464
if len(dbName) > 0 {
65-
varLeftJoin = append(varLeftJoin, "cache", dbName, tableName)
66-
varRightJoin = append(varRightJoin, "cache", dbName, tableName)
65+
varLeftJoin = append(varLeftJoin, prefix, dbName, tableName)
66+
varRightJoin = append(varRightJoin, prefix, dbName, tableName)
6767
keyLeftJoin = append(keyLeftJoin, dbName, tableName)
6868
} else {
69-
varLeftJoin = append(varLeftJoin, "cache", tableName)
70-
varRightJoin = append(varRightJoin, "cache", tableName)
69+
varLeftJoin = append(varLeftJoin, prefix, tableName)
70+
varRightJoin = append(varRightJoin, prefix, tableName)
7171
keyLeftJoin = append(keyLeftJoin, tableName)
7272
}
7373

tools/goctl/model/sql/gen/keys_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestGenCacheKeys(t *testing.T) {
3434
Comment: "姓名",
3535
SeqInIndex: 2,
3636
}
37-
primariCacheKey, uniqueCacheKey := genCacheKeys(parser.Table{
37+
primariCacheKey, uniqueCacheKey := genCacheKeys("cache", parser.Table{
3838
Name: stringx.From("user"),
3939
Db: stringx.From("go_zero"),
4040
PrimaryKey: parser.Primary{
@@ -129,7 +129,7 @@ func TestGenCacheKeys(t *testing.T) {
129129
}())
130130
})
131131
t.Run("no database name", func(t *testing.T) {
132-
primariCacheKey, _ = genCacheKeys(parser.Table{
132+
primariCacheKey, _ = genCacheKeys("cache", parser.Table{
133133
Name: stringx.From("user"),
134134
Db: stringx.From(""),
135135
PrimaryKey: parser.Primary{

tools/goctl/util/ctx/gomod_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,54 +101,54 @@ func Test_getRealModule(t *testing.T) {
101101
{
102102
name: "go work duplicate prefix",
103103
args: args{
104-
workDir: "D:\\code\\company\\core-ee\\service",
104+
workDir: "/code/company/core-ee/service",
105105
execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
106106
return `
107107
{
108108
"Path": "gitee.com/unitedrhino/core",
109-
"Dir": "D:\\code\\company\\core",
110-
"GoMod": "D:\\code\\company\\core\\go.mod",
109+
"Dir": "/code/company/core",
110+
"GoMod": "/code/company/core/go.mod",
111111
"GoVersion": "1.21.4"
112112
}
113113
{
114114
"Path": "gitee.com/unitedrhino/core-ee",
115-
"Dir": "D:\\code\\company\\core-ee",
116-
"GoMod": "D:\\code\\company\\core-ee\\go.mod",
115+
"Dir": "/code/company/core-ee",
116+
"GoMod": "/code/company/core-ee/go.mod",
117117
"GoVersion": "1.21.4"
118118
}`, nil
119119
},
120120
},
121121
want: &Module{
122122
Path: "gitee.com/unitedrhino/core-ee",
123-
Dir: "D:\\code\\company\\core-ee",
124-
GoMod: "D:\\code\\company\\core-ee\\go.mod",
123+
Dir: "/code/company/core-ee",
124+
GoMod: "/code/company/core-ee/go.mod",
125125
GoVersion: "1.21.4",
126126
},
127127
},
128128
{
129129
name: "go work duplicate prefix2",
130130
args: args{
131-
workDir: "D:\\code\\company\\core-ee",
131+
workDir: "/code/company/core-ee",
132132
execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
133133
return `
134134
{
135135
"Path": "gitee.com/unitedrhino/core",
136-
"Dir": "D:\\code\\company\\core",
137-
"GoMod": "D:\\code\\company\\core\\go.mod",
136+
"Dir": "/code/company/core",
137+
"GoMod": "/code/company/core/go.mod",
138138
"GoVersion": "1.21.4"
139139
}
140140
{
141141
"Path": "gitee.com/unitedrhino/core-ee",
142-
"Dir": "D:\\code\\company\\core-ee",
143-
"GoMod": "D:\\code\\company\\core-ee\\go.mod",
142+
"Dir": "/code/company/core-ee",
143+
"GoMod": "/code/company/core-ee/go.mod",
144144
"GoVersion": "1.21.4"
145145
}`, nil
146146
},
147147
},
148148
want: &Module{
149149
Path: "gitee.com/unitedrhino/core-ee",
150-
Dir: "D:\\code\\company\\core-ee",
151-
GoMod: "D:\\code\\company\\core-ee\\go.mod",
150+
Dir: "/code/company/core-ee",
151+
GoMod: "/code/company/core-ee/go.mod",
152152
GoVersion: "1.21.4",
153153
},
154154
},

0 commit comments

Comments
 (0)