Skip to content

Commit c82efd9

Browse files
authored
schemacmp: support decode field types from Table (#420)
* support decode field types from table
1 parent 0297393 commit c82efd9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pkg/schemacmp/table.go

+10
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ func Encode(ti *model.TableInfo) Table {
356356
return Table{value: encodeTableInfoToLattice(ti)}
357357
}
358358

359+
func DecodeColumnFieldTypes(t Table) map[string]*types.FieldType {
360+
table := t.value.Unwrap().([]interface{})
361+
columnMaps := table[tableInfoTupleIndexColumns].(map[string]interface{})
362+
cols := make(map[string]*types.FieldType, len(columnMaps))
363+
for key, value := range columnMaps {
364+
cols[key] = value.([]interface{})[columnInfoTupleIndexFieldTypes].(*types.FieldType)
365+
}
366+
return cols
367+
}
368+
359369
func (t Table) Restore(ctx *format.RestoreCtx, tableName string) {
360370
restoreTableInfoFromUnwrapped(ctx, t.value.Unwrap().([]interface{}), tableName)
361371
}

pkg/schemacmp/table_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ func (s *tableSchema) toTableInfo(createTableStmt string) (*model.TableInfo, err
5858
return ddl.MockTableInfo(s.sctx, createStmtNode, 1)
5959
}
6060

61+
func (s *tableSchema) checkDecodeFieldTypes(c *C, info *model.TableInfo, t Table) {
62+
fieldTyps := DecodeColumnFieldTypes(t)
63+
c.Assert(fieldTyps, HasLen, len(info.Columns))
64+
for _, col := range info.Columns {
65+
typ, ok := fieldTyps[col.Name.O]
66+
c.Assert(ok, IsTrue)
67+
c.Assert(col.FieldType, DeepEquals, *typ)
68+
}
69+
}
70+
6171
func (s *tableSchema) TestJoinSchemas(c *C) {
6272
testCases := []struct {
6373
name string
@@ -405,6 +415,8 @@ func (s *tableSchema) TestJoinSchemas(c *C) {
405415

406416
a := Encode(tia)
407417
b := Encode(tib)
418+
s.checkDecodeFieldTypes(c, tia, a)
419+
s.checkDecodeFieldTypes(c, tib, b)
408420
var j Table
409421
if len(tc.joinErr) == 0 {
410422
tij, err := s.toTableInfo(tc.join)

0 commit comments

Comments
 (0)