Skip to content

Commit

Permalink
workaround github.com/go-sql-driver/mysql/pull/1424 not released
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Chinn committed Aug 30, 2023
1 parent 31781fc commit 2301e09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
19 changes: 18 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,28 @@ func (table *table) Init() error {

table.values = make([]interface{}, len(tt))
for i, tp := range tt {
table.values[i] = reflect.New(tp.ScanType()).Interface()
table.values[i] = reflect.New(reflectColumnType(tp)).Interface()
}
return nil
}

func reflectColumnType(tp *sql.ColumnType) reflect.Type {
// workaround https://github.com/go-sql-driver/mysql/pull/1424 till it's released
nullable, _ := tp.Nullable()
switch tp.DatabaseTypeName() {
case "TINYBLOB", "MEDIUMBLOB", "LONGBLOB", "BLOB",
"VARBINARY", "BINARY", "BIT", "GEOMETRY":
return reflect.TypeOf([]byte{})
case "TINYTEXT", "MEDIUMTEXT", "LONGTEXT", "TEXT",
"VARCHAR", "CHAR", "DECIMAL", "ENUM", "SET", "JSON", "TIME":
if nullable {
return reflect.TypeOf(sql.NullString{})
}
return reflect.TypeOf("")
}
return tp.ScanType()
}

func (table *table) Next() bool {
if table.rows == nil {
if err := table.Init(); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions mysqldump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ CREATE TABLE 'Test_Table' (
~NullFloat64~ DOUBLE,
~bool~ TINYINT(1) NOT NULL,
~NullBool~ TINYINT(1),
~time~ TIME NOT NULL,
~NullTime~ TIME,
~time~ DATETIME NOT NULL,
~NullTime~ DATETIME,
~varbinary~ VARBINARY,
~rawbytes~ BLOB,
PRIMARY KEY (~id~)
Expand Down Expand Up @@ -128,8 +128,8 @@ func mockColumnRows() *sqlmock.Rows {
AddRow("NullFloat64", "DOUBLE", "YES", "", nil, "").
AddRow("bool", "BOOL", "NO", "", nil, "").
AddRow("NullBool", "BOOL", "YES", "", nil, "").
AddRow("time", "TIME", "NO", "", nil, "").
AddRow("NullTime", "TIME", "YES", "", nil, "").
AddRow("time", "DATETIME", "NO", "", nil, "").
AddRow("NullTime", "DATETIME", "YES", "", nil, "").
AddRow("varbinary", "VARBINARY", "YES", "", nil, "").
AddRow("rawbytes", "BLOB", "YES", "", nil, "")
}
Expand Down Expand Up @@ -185,10 +185,10 @@ func c(name string, v interface{}) *sqlmock.Column {
nullable = true
t = "BOOL"
case time.Time:
t = "TIME"
t = "DATETIME"
case sql.NullTime:
nullable = true
t = "TIME"
t = "DATETIME"
case []byte:
nullable = true
t = "VARBINARY"
Expand Down Expand Up @@ -243,8 +243,8 @@ func RunDump(t testing.TB, data *mysqldump.Data) {
~NullFloat64~ DOUBLE,
~bool~ TINYINT(1) NOT NULL,
~NullBool~ TINYINT(1),
~time~ TIME NOT NULL,
~NullTime~ TIME,
~time~ DATETIME NOT NULL,
~NullTime~ DATETIME,
~varbinary~ VARBINARY,
~rawbytes~ BLOB,
PRIMARY KEY (~id~)
Expand Down Expand Up @@ -421,8 +421,8 @@ func TestNoLockOk(t *testing.T) {
~NullFloat64~ DOUBLE,
~bool~ TINYINT(1) NOT NULL,
~NullBool~ TINYINT(1),
~time~ TIME NOT NULL,
~NullTime~ TIME,
~time~ DATETIME NOT NULL,
~NullTime~ DATETIME,
~varbinary~ VARBINARY,
~rawbytes~ BLOB,
PRIMARY KEY (~id~)
Expand Down

0 comments on commit 2301e09

Please sign in to comment.