Added database prefix of cache key. (#835)

This commit is contained in:
fangjianwei
2021-07-22 11:29:09 +08:00
committed by GitHub
parent 75952308f9
commit 476026e393
10 changed files with 60 additions and 49 deletions

View File

@@ -24,6 +24,7 @@ const (
flagURL = "url"
flagTable = "table"
flagStyle = "style"
flagDatabase = "database"
)
var errNotMatched = errors.New("sql not matched")
@@ -35,12 +36,13 @@ func MysqlDDL(ctx *cli.Context) error {
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
style := ctx.String(flagStyle)
database := ctx.String(flagDatabase)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
return fromDDl(src, dir, cfg, cache, idea)
return fromDDl(src, dir, cfg, cache, idea, database)
}
// MyDataSource generates model code from datasource
@@ -59,7 +61,7 @@ func MyDataSource(ctx *cli.Context) error {
return fromDataSource(url, pattern, dir, cfg, cache, idea)
}
func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
func fromDDl(src, dir string, cfg *config.Config, cache, idea bool, database string) error {
log := console.NewConsole(idea)
src = strings.TrimSpace(src)
if len(src) == 0 {
@@ -81,7 +83,7 @@ func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
}
for _, file := range files {
err = generator.StartFromDDL(file, cache)
err = generator.StartFromDDL(file, cache, database)
if err != nil {
return err
}

View File

@@ -24,12 +24,12 @@ func TestFromDDl(t *testing.T) {
err := gen.Clean()
assert.Nil(t, err)
err = fromDDl("./user.sql", t.TempDir(), cfg, true, false)
err = fromDDl("./user.sql", t.TempDir(), cfg, true, false, "go_zero")
assert.Equal(t, errNotMatched, err)
// case dir is not exists
unknownDir := filepath.Join(t.TempDir(), "test", "user.sql")
err = fromDDl(unknownDir, t.TempDir(), cfg, true, false)
err = fromDDl(unknownDir, t.TempDir(), cfg, true, false, "go_zero")
assert.True(t, func() bool {
switch err.(type) {
case *os.PathError:
@@ -40,7 +40,7 @@ func TestFromDDl(t *testing.T) {
}())
// case empty src
err = fromDDl("", t.TempDir(), cfg, true, false)
err = fromDDl("", t.TempDir(), cfg, true, false, "go_zero")
if err != nil {
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
}
@@ -70,7 +70,7 @@ func TestFromDDl(t *testing.T) {
_, err = os.Stat(user2Sql)
assert.Nil(t, err)
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, cfg, true, false)
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, cfg, true, false, "go_zero")
assert.Nil(t, err)
_, err = os.Stat(filepath.Join(tempDir, "usermodel.go"))