From 240132e7c7b592eeb27901b429b242db2083c213 Mon Sep 17 00:00:00 2001 From: spectatorMrZ Date: Fri, 7 Jan 2022 10:45:26 +0800 Subject: [PATCH] Fix pg model generation without tag (#1407) 1. fix pg model struct haven't tag 2. add pg command test from datasource --- .../model/sql/command/command_pg_test.go | 45 +++++++++++++++++++ tools/goctl/model/sql/parser/parser.go | 1 + 2 files changed, 46 insertions(+) create mode 100644 tools/goctl/model/sql/command/command_pg_test.go diff --git a/tools/goctl/model/sql/command/command_pg_test.go b/tools/goctl/model/sql/command/command_pg_test.go new file mode 100644 index 00000000..a66943dc --- /dev/null +++ b/tools/goctl/model/sql/command/command_pg_test.go @@ -0,0 +1,45 @@ +package command + +import ( + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/tools/goctl/config" + "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" + "github.com/tal-tech/go-zero/tools/goctl/util" + "path/filepath" + "testing" +) + +// generate test table sql +/* +CREATE TABLE "public"."users" ( + "id" serial NOT NULL, + "account" varchar(20) COLLATE "pg_catalog"."default" NOT NULL, + "avatar" text COLLATE "pg_catalog"."default", + "nick_name" varchar(60) COLLATE "pg_catalog"."default", + "register_time" timestamp(6) NOT NULL, + "update_time" timestamp(6), + "password" varchar(255) COLLATE "pg_catalog"."default", + "email" varchar(100) COLLATE "pg_catalog"."default", + "reset_key" varchar(10) COLLATE "pg_catalog"."default", + "active" bool NOT NULL DEFAULT true, + CONSTRAINT "users_pk" PRIMARY KEY ("id") +) +; + +ALTER TABLE "public"."users" + OWNER TO "postgres"; +*/ + +func TestFromDatasource(t *testing.T) { + err := gen.Clean() + assert.Nil(t, err) + + url := "postgres://postgres:postgres@127.0.0.1:5432/demo?sslmode=disable" + + pattern := "users" // table name + + cfg, err := config.NewConfig("") + tempDir := filepath.Join(util.MustTempDir(), "test") + err = fromPostgreSqlDataSource(url, pattern, tempDir, "public", cfg, false, false) + assert.Nil(t, err) +} diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index 28f69a43..6f27daf4 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -360,6 +360,7 @@ func getTableFields(table *model.Table) (map[string]*Field, error) { } field := &Field{ + NameOriginal: each.Name, Name: stringx.From(each.Name), DataType: dt, Comment: each.Comment,