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

@@ -21,6 +21,7 @@ type (
// Table describes a mysql table
Table struct {
Name stringx.String
Db stringx.String
PrimaryKey Primary
UniqueIndex map[string][]*Field
Fields []*Field
@@ -46,7 +47,7 @@ type (
)
// Parse parses ddl into golang structure
func Parse(filename string) ([]*Table, error) {
func Parse(filename string, database string) ([]*Table, error) {
p := parser.NewParser()
tables, err := p.From(filename)
if err != nil {
@@ -145,6 +146,7 @@ func Parse(filename string) ([]*Table, error) {
list = append(list, &Table{
Name: stringx.From(e.Name),
Db: stringx.From(database),
PrimaryKey: primaryKey,
UniqueIndex: uniqueIndex,
Fields: fields,
@@ -243,6 +245,7 @@ func ConvertDataType(table *model.Table) (*Table, error) {
var reply Table
reply.UniqueIndex = map[string][]*Field{}
reply.Name = stringx.From(table.Table)
reply.Db = stringx.From(table.Db)
seqInIndex := 0
if table.PrimaryKey.Index != nil {
seqInIndex = table.PrimaryKey.Index.SeqInIndex

View File

@@ -15,7 +15,7 @@ func TestParsePlainText(t *testing.T) {
err := ioutil.WriteFile(sqlFile, []byte("plain text"), 0o777)
assert.Nil(t, err)
_, err = Parse(sqlFile)
_, err = Parse(sqlFile, "go_zero")
assert.NotNil(t, err)
}
@@ -24,7 +24,7 @@ func TestParseSelect(t *testing.T) {
err := ioutil.WriteFile(sqlFile, []byte("select * from user"), 0o777)
assert.Nil(t, err)
tables, err := Parse(sqlFile)
tables, err := Parse(sqlFile, "go_zero")
assert.Nil(t, err)
assert.Equal(t, 0, len(tables))
}
@@ -34,7 +34,7 @@ func TestParseCreateTable(t *testing.T) {
err := ioutil.WriteFile(sqlFile, []byte("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"), 0o777)
assert.Nil(t, err)
tables, err := Parse(sqlFile)
tables, err := Parse(sqlFile, "go_zero")
assert.Equal(t, 1, len(tables))
table := tables[0]
assert.Nil(t, err)