Skip to content

Commit 43873cc

Browse files
authored
Fixing varbinary (varbinary(XX)) schema migration for mysql to map to correct spanner type with length BYTES(XX) (#1164)
* Fixing varbinary (varbinary(XX)) schema migration for mysql to map to correct spanner type with length BYTES(XX) * Only changing varbinary type * Updating integration test
1 parent 1af53bd commit 43873cc

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

sources/mysql/mysqldump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestProcessMySQLDump_Scalar(t *testing.T) {
4040
ty string
4141
expected ddl.Type
4242
}{
43-
{"varbinary(100)", ddl.Type{Name: ddl.Bytes, Len: ddl.MaxLength}},
43+
{"varbinary(100)", ddl.Type{Name: ddl.Bytes, Len: int64(100)}},
4444
{"bigint", ddl.Type{Name: ddl.Int64}},
4545
{"bool", ddl.Type{Name: ddl.Bool}},
4646
{"boolean", ddl.Type{Name: ddl.Bool}},

sources/mysql/toddl.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,23 @@ func toSpannerTypeInternal(srcType schema.Type, spType string) (ddl.Type, []inte
188188
default:
189189
return ddl.Type{Name: ddl.JSON}, nil
190190
}
191-
case "binary", "varbinary":
191+
case "binary":
192192
switch spType {
193193
case ddl.String:
194194
return ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, nil
195195
default:
196196
return ddl.Type{Name: ddl.Bytes, Len: ddl.MaxLength}, nil
197197
}
198+
case "varbinary":
199+
switch spType {
200+
case ddl.String:
201+
return ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, nil
202+
default:
203+
if len(srcType.Mods) > 0 {
204+
return ddl.Type{Name: ddl.Bytes, Len: srcType.Mods[0]}, nil
205+
}
206+
return ddl.Type{Name: ddl.Bytes, Len: ddl.MaxLength}, nil
207+
}
198208
case "tinyblob", "mediumblob", "blob", "longblob":
199209
switch spType {
200210
case ddl.String:

sources/mysql/toddl_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ func TestToSpannerTypeInternal(t *testing.T) {
103103
if errCheck != nil {
104104
t.Errorf("Error in binary to default conversion")
105105
}
106+
spType, errCheck := toSpannerTypeInternal(schema.Type{"varbinary", []int64{10}, []int64{}}, "BYTES")
107+
if errCheck != nil {
108+
t.Errorf("Error in binary to default conversion")
109+
}
110+
assert.Equal(t, "BYTES", spType.Name)
111+
assert.Equal(t, int64(10), spType.Len)
112+
assert.Equal(t, false, spType.IsArray)
106113
_, errCheck = toSpannerTypeInternal(schema.Type{"blob", []int64{1, 2, 3}, []int64{1, 2, 3}}, "STRING")
107114
if errCheck != nil {
108115
t.Errorf("Error in blob to string conversion")

0 commit comments

Comments
 (0)