@@ -2,6 +2,7 @@ package migration
2
2
3
3
import (
4
4
"database/sql"
5
+ "fmt"
5
6
"os"
6
7
"testing"
7
8
"time"
@@ -245,3 +246,48 @@ func TestGeneratedColumns(t *testing.T) {
245
246
err = migration .Run ()
246
247
assert .NoError (t , err )
247
248
}
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
+ }
0 commit comments