Skip to content

Commit fb92a14

Browse files
authored
feature:build undo log by insert target SQL (#333)
* insert undo log * add insert undo test * fix map loop * OPT code style & lint & add ut * fix imports * fix conflict & adapter some modify * fix some bug & add ut
1 parent 48bf289 commit fb92a14

File tree

4 files changed

+1738
-0
lines changed

4 files changed

+1738
-0
lines changed

pkg/datasource/sql/types/meta.go

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package types
1919

2020
import (
2121
"reflect"
22+
23+
"github.com/pkg/errors"
2224
)
2325

2426
// ColumnMeta
@@ -117,3 +119,31 @@ func (m TableMeta) GetPrimaryKeyOnlyName() []string {
117119
}
118120
return keys
119121
}
122+
123+
// GetPrimaryKeyType get PK database type
124+
func (m TableMeta) GetPrimaryKeyType() (int32, error) {
125+
for _, index := range m.Indexs {
126+
if index.IType == IndexTypePrimaryKey {
127+
for i := range index.Columns {
128+
return index.Columns[i].DatabaseType, nil
129+
}
130+
}
131+
}
132+
return 0, errors.New("get primary key type error")
133+
}
134+
135+
// GetPrimaryKeyTypeStrMap get all PK type to map
136+
func (m TableMeta) GetPrimaryKeyTypeStrMap() (map[string]string, error) {
137+
pkMap := make(map[string]string)
138+
for _, index := range m.Indexs {
139+
if index.IType == IndexTypePrimaryKey {
140+
for i := range index.Columns {
141+
pkMap[index.ColumnName] = index.Columns[i].DatabaseTypeString
142+
}
143+
}
144+
}
145+
if len(pkMap) == 0 {
146+
return nil, errors.New("get primary key type error")
147+
}
148+
return pkMap, nil
149+
}

pkg/datasource/sql/types/meta_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package types
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestTableMeta_GetPrimaryKeyTypeStrMap(t *testing.T) {
27+
type fields struct {
28+
TableName string
29+
Columns map[string]ColumnMeta
30+
Indexs map[string]IndexMeta
31+
ColumnNames []string
32+
}
33+
34+
tests := []struct {
35+
name string
36+
fields fields
37+
want map[string]string
38+
}{
39+
{name: "test-1", fields: fields{TableName: "test", Indexs: map[string]IndexMeta{
40+
"id": {
41+
Name: "id",
42+
ColumnName: "id",
43+
IType: IndexTypePrimaryKey,
44+
Columns: []ColumnMeta{
45+
{
46+
ColumnName: "id",
47+
DatabaseTypeString: "BIGINT",
48+
},
49+
},
50+
},
51+
}}, want: map[string]string{
52+
"id": "BIGINT",
53+
}},
54+
}
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
m := TableMeta{
58+
TableName: tt.fields.TableName,
59+
Columns: tt.fields.Columns,
60+
Indexs: tt.fields.Indexs,
61+
ColumnNames: tt.fields.ColumnNames,
62+
}
63+
got, err := m.GetPrimaryKeyTypeStrMap()
64+
assert.Nil(t, err)
65+
assert.Equal(t, tt.want, got)
66+
})
67+
}
68+
}
69+
70+
func TestTableMeta_GetPrimaryKeyType(t *testing.T) {
71+
type fields struct {
72+
TableName string
73+
Columns map[string]ColumnMeta
74+
Indexs map[string]IndexMeta
75+
ColumnNames []string
76+
}
77+
tests := []struct {
78+
name string
79+
fields fields
80+
want int32
81+
}{
82+
{name: "test-1", fields: fields{TableName: "test", Indexs: map[string]IndexMeta{
83+
"id": {
84+
Name: "id",
85+
ColumnName: "id",
86+
IType: IndexTypePrimaryKey,
87+
Columns: []ColumnMeta{
88+
{
89+
ColumnName: "id",
90+
DatabaseTypeString: "BIGINT",
91+
DatabaseType: GetSqlDataType("BIGINT"),
92+
},
93+
},
94+
},
95+
}}, want: GetSqlDataType("BIGINT")},
96+
}
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
m := TableMeta{
100+
TableName: tt.fields.TableName,
101+
Columns: tt.fields.Columns,
102+
Indexs: tt.fields.Indexs,
103+
ColumnNames: tt.fields.ColumnNames,
104+
}
105+
got, err := m.GetPrimaryKeyType()
106+
assert.Nil(t, err)
107+
assert.Equalf(t, tt.want, got, "GetPrimaryKeyType()")
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)