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
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/stores/postgres"
|
"github.com/zeromicro/go-zero/core/stores/postgres"
|
||||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
"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/gen"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/model"
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/model"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/util"
|
"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
|
// MysqlDDL generates model code from ddl
|
||||||
func MysqlDDL(ctx *cli.Context) error {
|
func MysqlDDL(ctx *cli.Context) error {
|
||||||
|
migrationnotes.BeforeCommands(ctx)
|
||||||
src := ctx.String(flagSrc)
|
src := ctx.String(flagSrc)
|
||||||
dir := ctx.String(flagDir)
|
dir := ctx.String(flagDir)
|
||||||
cache := ctx.Bool(flagCache)
|
cache := ctx.Bool(flagCache)
|
||||||
@@ -66,6 +68,7 @@ func MysqlDDL(ctx *cli.Context) error {
|
|||||||
|
|
||||||
// MySqlDataSource generates model code from datasource
|
// MySqlDataSource generates model code from datasource
|
||||||
func MySqlDataSource(ctx *cli.Context) error {
|
func MySqlDataSource(ctx *cli.Context) error {
|
||||||
|
migrationnotes.BeforeCommands(ctx)
|
||||||
url := strings.TrimSpace(ctx.String(flagURL))
|
url := strings.TrimSpace(ctx.String(flagURL))
|
||||||
dir := strings.TrimSpace(ctx.String(flagDir))
|
dir := strings.TrimSpace(ctx.String(flagDir))
|
||||||
cache := ctx.Bool(flagCache)
|
cache := ctx.Bool(flagCache)
|
||||||
@@ -95,6 +98,7 @@ func MySqlDataSource(ctx *cli.Context) error {
|
|||||||
|
|
||||||
// PostgreSqlDataSource generates model code from datasource
|
// PostgreSqlDataSource generates model code from datasource
|
||||||
func PostgreSqlDataSource(ctx *cli.Context) error {
|
func PostgreSqlDataSource(ctx *cli.Context) error {
|
||||||
|
migrationnotes.BeforeCommands(ctx)
|
||||||
url := strings.TrimSpace(ctx.String(flagURL))
|
url := strings.TrimSpace(ctx.String(flagURL))
|
||||||
dir := strings.TrimSpace(ctx.String(flagDir))
|
dir := strings.TrimSpace(ctx.String(flagDir))
|
||||||
cache := ctx.Bool(flagCache)
|
cache := ctx.Bool(flagCache)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
57
tools/goctl/model/sql/command/migrationnotes/v1.3.4.go
Normal file
57
tools/goctl/model/sql/command/migrationnotes/v1.3.4.go
Normal file
@@ -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
|
||||||
|
|
||||||
|
}
|
||||||
75
tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go
Normal file
75
tools/goctl/model/sql/command/migrationnotes/v1.3.4_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user