Skip to content

Commit

Permalink
binary type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo committed Nov 14, 2024
1 parent e708608 commit 89c4f16
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
46 changes: 46 additions & 0 deletions pkg/migration/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migration

import (
"database/sql"
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -245,3 +246,48 @@ func TestGeneratedColumns(t *testing.T) {
err = migration.Run()
assert.NoError(t, err)
}

type testcase struct {
OldType string
NewType string
}

// TestBinaryChecksum tests that we can alter a binary column and still get a checksum match.
// It works fine from varbinary(50)->varbinary(100), but not from binary(50)->binary(100),
// without an intermediate cast.
func TestBinaryChecksum(t *testing.T) {
tests := []testcase{
{"binary(50)", "varbinary(100)"},
{"binary(50)", "binary(100)"},
{"varbinary(100)", "varbinary(50)"},
{"varbinary(100)", "binary(50)"},
{"blob", "tinyblob"},
{"tinyblob", "blob"},
{"mediumblob", "tinyblob"},
{"longblob", "mediumblob"},
{"binary(100)", "blob"},
{"blob", "binary(100)"},
}
for _, test := range tests {
testutils.RunSQL(t, `DROP TABLE IF EXISTS t1varbin, _t1varbin_new`)
table := fmt.Sprintf(`CREATE TABLE t1varbin (
id int not null primary key auto_increment,
b %s not null
)`, test.OldType)
testutils.RunSQL(t, table)
testutils.RunSQL(t, `insert into t1varbin values (null, 'abcdefg')`)
migration := &Migration{}
cfg, err := mysql.ParseDSN(testutils.DSN())
assert.NoError(t, err)
migration.Host = cfg.Addr
migration.Username = cfg.User
migration.Password = cfg.Passwd
migration.Database = cfg.DBName
migration.Threads = 1
migration.Checksum = true
migration.Table = "t1varbin"
migration.Alter = fmt.Sprintf("CHANGE b b %s not null", test.NewType) //nolint: dupword
err = migration.Run()
assert.NoError(t, err)
}
}
4 changes: 3 additions & 1 deletion pkg/table/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func castableTp(tp string) string {
return "datetime"
case "varchar", "enum", "set", "text", "mediumtext", "longtext":
return "char"
case "tinyblob", "blob", "mediumblob", "longblob", "varbinary", "binary":
case "tinyblob", "blob", "mediumblob", "longblob", "varbinary":
return "binary"
case "binary":
return "binary(0)" // weirdly only binary needs special handling; blob etc is fine.
case "float", "double": // required for MySQL 5.7
return "char"
case "json":
Expand Down
2 changes: 1 addition & 1 deletion pkg/table/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCastableTp(t *testing.T) {
{"longblob", "binary"},
{"varbinary", "binary"},
{"char(100)", "char"},
{"binary(100)", "binary"},
{"binary(100)", "binary(0)"},
{"datetime", "datetime"},
{"datetime(6)", "datetime"},
{"year", "char"},
Expand Down

0 comments on commit 89c4f16

Please sign in to comment.