Skip to content

Commit 89c4f16

Browse files
committed
binary type conversion
1 parent e708608 commit 89c4f16

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

pkg/migration/migration_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package migration
22

33
import (
44
"database/sql"
5+
"fmt"
56
"os"
67
"testing"
78
"time"
@@ -245,3 +246,48 @@ func TestGeneratedColumns(t *testing.T) {
245246
err = migration.Run()
246247
assert.NoError(t, err)
247248
}
249+
250+
type testcase struct {
251+
OldType string
252+
NewType string
253+
}
254+
255+
// TestBinaryChecksum tests that we can alter a binary column and still get a checksum match.
256+
// It works fine from varbinary(50)->varbinary(100), but not from binary(50)->binary(100),
257+
// without an intermediate cast.
258+
func TestBinaryChecksum(t *testing.T) {
259+
tests := []testcase{
260+
{"binary(50)", "varbinary(100)"},
261+
{"binary(50)", "binary(100)"},
262+
{"varbinary(100)", "varbinary(50)"},
263+
{"varbinary(100)", "binary(50)"},
264+
{"blob", "tinyblob"},
265+
{"tinyblob", "blob"},
266+
{"mediumblob", "tinyblob"},
267+
{"longblob", "mediumblob"},
268+
{"binary(100)", "blob"},
269+
{"blob", "binary(100)"},
270+
}
271+
for _, test := range tests {
272+
testutils.RunSQL(t, `DROP TABLE IF EXISTS t1varbin, _t1varbin_new`)
273+
table := fmt.Sprintf(`CREATE TABLE t1varbin (
274+
id int not null primary key auto_increment,
275+
b %s not null
276+
)`, test.OldType)
277+
testutils.RunSQL(t, table)
278+
testutils.RunSQL(t, `insert into t1varbin values (null, 'abcdefg')`)
279+
migration := &Migration{}
280+
cfg, err := mysql.ParseDSN(testutils.DSN())
281+
assert.NoError(t, err)
282+
migration.Host = cfg.Addr
283+
migration.Username = cfg.User
284+
migration.Password = cfg.Passwd
285+
migration.Database = cfg.DBName
286+
migration.Threads = 1
287+
migration.Checksum = true
288+
migration.Table = "t1varbin"
289+
migration.Alter = fmt.Sprintf("CHANGE b b %s not null", test.NewType) //nolint: dupword
290+
err = migration.Run()
291+
assert.NoError(t, err)
292+
}
293+
}

pkg/table/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ func castableTp(tp string) string {
3636
return "datetime"
3737
case "varchar", "enum", "set", "text", "mediumtext", "longtext":
3838
return "char"
39-
case "tinyblob", "blob", "mediumblob", "longblob", "varbinary", "binary":
39+
case "tinyblob", "blob", "mediumblob", "longblob", "varbinary":
4040
return "binary"
41+
case "binary":
42+
return "binary(0)" // weirdly only binary needs special handling; blob etc is fine.
4143
case "float", "double": // required for MySQL 5.7
4244
return "char"
4345
case "json":

pkg/table/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestCastableTp(t *testing.T) {
5252
{"longblob", "binary"},
5353
{"varbinary", "binary"},
5454
{"char(100)", "char"},
55-
{"binary(100)", "binary"},
55+
{"binary(100)", "binary(0)"},
5656
{"datetime", "datetime"},
5757
{"datetime(6)", "datetime"},
5858
{"year", "char"},

0 commit comments

Comments
 (0)