-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change column to read from information_schema * reactor generate mode from datasource * reactor generate mode from datasource * add primary key check logic * resolve rebase conflicts * add naming style * add filename test case * resolve rebase conflicts * reactor test * add test case * change shell script to makefile * update rpc new * update gen_test.go * format code * format code * update test * generates alias
- Loading branch information
Keson
authored
Nov 18, 2020
1 parent
71083b5
commit 24fb29a
Showing
55 changed files
with
678 additions
and
1,167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package command | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" | ||
"github.com/tal-tech/go-zero/tools/goctl/util" | ||
) | ||
|
||
var sql = "-- 用户表 --\nCREATE TABLE `user` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n UNIQUE KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n\n" | ||
|
||
func TestFromDDl(t *testing.T) { | ||
err := fromDDl("./user.sql", t.TempDir(), gen.NamingCamel, true, false) | ||
assert.Equal(t, errNotMatched, err) | ||
|
||
// case dir is not exists | ||
unknownDir := filepath.Join(t.TempDir(), "test", "user.sql") | ||
err = fromDDl(unknownDir, t.TempDir(), gen.NamingCamel, true, false) | ||
assert.True(t, func() bool { | ||
switch err.(type) { | ||
case *os.PathError: | ||
return true | ||
default: | ||
return false | ||
} | ||
}()) | ||
|
||
// case empty src | ||
err = fromDDl("", t.TempDir(), gen.NamingCamel, true, false) | ||
if err != nil { | ||
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error()) | ||
} | ||
|
||
// case unknown naming style | ||
tmp := filepath.Join(t.TempDir(), "user.sql") | ||
err = fromDDl(tmp, t.TempDir(), "lower1", true, false) | ||
if err != nil { | ||
assert.Equal(t, "unexpected naming style: lower1", err.Error()) | ||
} | ||
|
||
tempDir := filepath.Join(t.TempDir(), "test") | ||
err = util.MkdirIfNotExist(tempDir) | ||
if err != nil { | ||
return | ||
} | ||
|
||
user1Sql := filepath.Join(tempDir, "user1.sql") | ||
user2Sql := filepath.Join(tempDir, "user2.sql") | ||
|
||
err = ioutil.WriteFile(user1Sql, []byte(sql), os.ModePerm) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = ioutil.WriteFile(user2Sql, []byte(sql), os.ModePerm) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = os.Stat(user1Sql) | ||
assert.Nil(t, err) | ||
|
||
_, err = os.Stat(user2Sql) | ||
assert.Nil(t, err) | ||
|
||
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, gen.NamingLower, true, false) | ||
assert.Nil(t, err) | ||
|
||
_, err = os.Stat(filepath.Join(tempDir, "usermodel.go")) | ||
assert.Nil(t, err) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
# generate model with cache from ddl | ||
fromDDL: | ||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/user" -c | ||
|
||
|
||
# generate model with cache from data source | ||
user=root | ||
password=password | ||
datasource=127.0.0.1:3306 | ||
database=gozero | ||
|
||
fromDataSource: | ||
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style camel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.