From 036d803fbbac271b700afc589a790aab5e9cdcf4 Mon Sep 17 00:00:00 2001 From: Fyn <53678344+fynxiu@users.noreply.github.com> Date: Mon, 18 Apr 2022 14:42:13 +0800 Subject: [PATCH] docs(goctl): goctl 1.3.4 migration note (#1780) * docs(goctl): goctl 1.3.4 migration note * adds a simple lang check * adds migration notes * chore: remove i18n * chore: remove todo --- tools/goctl/model/sql/command/command.go | 4 + .../command/migrationnotes/migrationnotes.go | 27 +++++++ .../sql/command/migrationnotes/v1.3.4.go | 57 ++++++++++++++ .../sql/command/migrationnotes/v1.3.4_test.go | 75 +++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 tools/goctl/model/sql/command/migrationnotes/migrationnotes.go create mode 100644 tools/goctl/model/sql/command/migrationnotes/v1.3.4.go create mode 100644 tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index 6e484c6d..326be929 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -11,6 +11,7 @@ import ( "github.com/zeromicro/go-zero/core/stores/postgres" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/tools/goctl/config" + "github.com/zeromicro/go-zero/tools/goctl/model/sql/command/migrationnotes" "github.com/zeromicro/go-zero/tools/goctl/model/sql/gen" "github.com/zeromicro/go-zero/tools/goctl/model/sql/model" "github.com/zeromicro/go-zero/tools/goctl/model/sql/util" @@ -38,6 +39,7 @@ var errNotMatched = errors.New("sql not matched") // MysqlDDL generates model code from ddl func MysqlDDL(ctx *cli.Context) error { + migrationnotes.BeforeCommands(ctx) src := ctx.String(flagSrc) dir := ctx.String(flagDir) cache := ctx.Bool(flagCache) @@ -66,6 +68,7 @@ func MysqlDDL(ctx *cli.Context) error { // MySqlDataSource generates model code from datasource func MySqlDataSource(ctx *cli.Context) error { + migrationnotes.BeforeCommands(ctx) url := strings.TrimSpace(ctx.String(flagURL)) dir := strings.TrimSpace(ctx.String(flagDir)) cache := ctx.Bool(flagCache) @@ -95,6 +98,7 @@ func MySqlDataSource(ctx *cli.Context) error { // PostgreSqlDataSource generates model code from datasource func PostgreSqlDataSource(ctx *cli.Context) error { + migrationnotes.BeforeCommands(ctx) url := strings.TrimSpace(ctx.String(flagURL)) dir := strings.TrimSpace(ctx.String(flagDir)) cache := ctx.Bool(flagCache) diff --git a/tools/goctl/model/sql/command/migrationnotes/migrationnotes.go b/tools/goctl/model/sql/command/migrationnotes/migrationnotes.go new file mode 100644 index 00000000..d8baa62e --- /dev/null +++ b/tools/goctl/model/sql/command/migrationnotes/migrationnotes.go @@ -0,0 +1,27 @@ +package migrationnotes + +import ( + "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/config" + "github.com/zeromicro/go-zero/tools/goctl/util/format" +) + +// BeforeCommands run before comamnd run to show some migration notes +func BeforeCommands(ctx *cli.Context) error { + if err := migrateBefore1_3_4(ctx); err != nil { + return err + } + return nil +} + +func getModelSuffix(style string) (string, error) { + cfg, err := config.NewConfig(style) + if err != nil { + return "", err + } + baseSuffix, err := format.FileNamingFormat(cfg.NamingFormat, "_model") + if err != nil { + return "", err + } + return baseSuffix + ".go", nil +} diff --git a/tools/goctl/model/sql/command/migrationnotes/v1.3.4.go b/tools/goctl/model/sql/command/migrationnotes/v1.3.4.go new file mode 100644 index 00000000..2a99e142 --- /dev/null +++ b/tools/goctl/model/sql/command/migrationnotes/v1.3.4.go @@ -0,0 +1,57 @@ +package migrationnotes + +import ( + "fmt" + "io/ioutil" + "strings" + + "github.com/urfave/cli" +) + +func migrateBefore1_3_4(ctx *cli.Context) error { + dir := ctx.String("dir") + style := ctx.String("style") + ok, err := needShow1_3_4(dir, style) + if err != nil { + return err + } + if !ok { + return nil + } + + fmt.Println(`It seems like that your goctl has just been upgraded to version 1.3.4 or later, which refactored the code of the model module. The original XXXmodel.go has been split into XXXmodel_gen.go (read-only) and XXXmodel.go. You just need to follow these steps to complete the migration: +1. back up the original XXXmodel.go (make sure the file name is no longer in the current directory) +2. re-run the generate command (a new XXXmodel.go will be created) +3. populate XXXmodel.go with the code that is not generated by goctl according to the comments in XXXmodel_gen.go`) + + return nil +} + +func needShow1_3_4(dir, style string) (bool, error) { + files, err := ioutil.ReadDir(dir) + if err != nil { + return false, nil + } + // Returns false when the directory contains a file with the suffix "_gen.go" + // In addition, it returns true if it contains a model file extension. + // In other case, false is returned. + for _, f := range files { + if f.IsDir() { + continue + } + if strings.HasSuffix(f.Name(), "_gen.go") { + return false, nil + } + } + modelSuffix, err := getModelSuffix(style) + if err != nil { + return false, err + } + for _, f := range files { + if !f.IsDir() && strings.HasSuffix(f.Name(), modelSuffix) { + return true, nil + } + } + return false, nil + +} diff --git a/tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go b/tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go new file mode 100644 index 00000000..4d77bbfc --- /dev/null +++ b/tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go @@ -0,0 +1,75 @@ +package migrationnotes + +import ( + "io/fs" + "io/ioutil" + "os" + "path" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_needShow1_3_4(t *testing.T) { + root, err := os.MkdirTemp("", "goctl-model") + require.NoError(t, err) + defer os.RemoveAll(root) + + dir1 := path.Join(root, "dir1") + require.NoError(t, os.Mkdir(dir1, fs.ModePerm)) + ioutil.WriteFile(filepath.Join(dir1, "foo_gen.go"), nil, fs.ModePerm) + + dir2 := path.Join(root, "dir2") + require.NoError(t, os.Mkdir(dir2, fs.ModePerm)) + ioutil.WriteFile(filepath.Join(dir2, "foomodel.go"), nil, fs.ModePerm) + + dir3 := path.Join(root, "dir3") + require.NoError(t, os.Mkdir(dir3, fs.ModePerm)) + ioutil.WriteFile(filepath.Join(dir3, "irrelevant.go"), nil, fs.ModePerm) + + type args struct { + dir string + style string + } + tests := []struct { + name string + args args + want bool + wantErr bool + }{ + { + name: "dir that contains *_gen.go should return false", + args: args{ + dir: dir1, + }, + want: false, + }, + { + name: "dir that contains *model.go without *_gen.go should return true", + args: args{ + dir: dir2, + }, + want: true, + }, + { + name: "dir that only contains irrelevant files should return false", + args: args{ + dir: dir3, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := needShow1_3_4(tt.args.dir, tt.args.style) + if (err != nil) != tt.wantErr { + t.Errorf("needShow1_3_4() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("needShow1_3_4() = %v, want %v", got, tt.want) + } + }) + } +}