diff --git a/dumper/mysqlnativedump.go b/dumper/mysqlnativedump.go index d9d737d..85ef122 100644 --- a/dumper/mysqlnativedump.go +++ b/dumper/mysqlnativedump.go @@ -11,7 +11,6 @@ import ( "log/slog" "net" "strings" - "time" "github.com/go-sql-driver/mysql" "github.com/liweiyi88/onedump/config" @@ -184,11 +183,13 @@ func (m *MysqlNativeDump) writeTableContent(buf *bufio.Writer, table string) err case "DECIMAL", "DEC": sb.WriteString(fmt.Sprintf("%s", value)) case "DATE": - v, ok := value.(time.Time) + v, ok := value.([]uint8) + if !ok { - return fmt.Errorf("could not parse DATE type, expect time.Time, got %T", value) + return fmt.Errorf("could not parse DATE type, expect []uint8, got %T", value) } - sb.WriteString(fmt.Sprintf("'%s'", v.Format("2006-01-02"))) + + sb.WriteString(fmt.Sprintf("'%s'", v)) case "DATETIME": v, ok := value.([]byte) if !ok { diff --git a/dumper/mysqlnativedump_test.go b/dumper/mysqlnativedump_test.go index 65b93ed..111a747 100644 --- a/dumper/mysqlnativedump_test.go +++ b/dumper/mysqlnativedump_test.go @@ -89,8 +89,8 @@ func TestWriteTableContentOK(t *testing.T) { rows := mock. NewRows([]string{"null", "tinyint", "tinyint_bytes", "float", "float_bytes", "decimal", "date", "datetime", "timestamp", "time", "year", "char", "binary", "varbinary", "bit", "tinyblob", "bool", "bool", "json"}). - AddRow(nil, 1, []byte("1"), 1.1, []byte("1.2"), 1.2, time.Now(), []byte("2024-08-09 00:00:00"), []byte("1723161093"), []byte("00:00:00"), 2024, "char", []uint8{1}, "1", []uint8{1}, "tinyblob", true, false, []uint8{1}). - AddRow(nil, 1, []byte("1"), 1.1, []byte("1.2"), 1.2, time.Now(), []byte("2024-08-09 00:00:00"), []byte("1723161093"), []byte("00:00:00"), 2024, "char", []uint8{1}, "1", []uint8{1}, "tinyblob", true, false, []uint8{1}) + AddRow(nil, 1, []byte("1"), 1.1, []byte("1.2"), 1.2, []uint8{50, 48, 50, 52, 45, 48, 54, 45, 49, 55}, []byte("2024-08-09 00:00:00"), []byte("1723161093"), []byte("00:00:00"), 2024, "char", []uint8{1}, "1", []uint8{1}, "tinyblob", true, false, []uint8{1}). + AddRow(nil, 1, []byte("1"), 1.1, []byte("1.2"), 1.2, []uint8{50, 48, 50, 52, 45, 48, 54, 45, 49, 55}, []byte("2024-08-09 00:00:00"), []byte("1723161093"), []byte("00:00:00"), 2024, "char", []uint8{1}, "1", []uint8{1}, "tinyblob", true, false, []uint8{1}) val := reflect.ValueOf(rows).Elem() field := val.FieldByName("def") @@ -102,7 +102,7 @@ func TestWriteTableContentOK(t *testing.T) { sqlmock.NewColumn("new").OfType("FLOAT", 1.1), sqlmock.NewColumn("new").OfType("FLOAT", 1.1), sqlmock.NewColumn("new").OfType("DECIMAL", 1.2), - sqlmock.NewColumn("new").OfType("DATE", time.Now()), + sqlmock.NewColumn("new").OfType("DATE", []uint8{50, 48, 50, 52, 45, 48, 54, 45, 49, 55}), sqlmock.NewColumn("new").OfType("DATETIME", "2024-08-09 00:00:00"), sqlmock.NewColumn("new").OfType("TIMESTAMP", "1723161093"), sqlmock.NewColumn("new").OfType("TIME", "00:00:00"), @@ -194,7 +194,7 @@ func TestWriteTableContentInvalidColumnType(t *testing.T) { sqlmock.NewColumn("new").OfType("UNSUPPORT", "12"), }) - want := []string{"could not parse DATE type, expect time.Time, got string", + want := []string{"could not parse DATE type, expect []uint8, got string", "could not parse DATETIME type, expect []byte, got string", "could not parse TIMESTAMP type, expect []byte, got string", "could not parse TIME type, expect []byte, got string", diff --git a/testutils/onedump_mysql.sql b/testutils/onedump_mysql.sql index 4aeaf6f..e346d80 100644 --- a/testutils/onedump_mysql.sql +++ b/testutils/onedump_mysql.sql @@ -47,6 +47,7 @@ CREATE TABLE `onedump` ( `double_precision` double DEFAULT NULL, `decimal` decimal(10,2) DEFAULT NULL, `dec` decimal(10,2) DEFAULT NULL, + `date` date default null, `datetime` datetime DEFAULT NULL, `timestamp` timestamp NULL DEFAULT NULL, `time` time DEFAULT NULL, @@ -60,8 +61,8 @@ CREATE TABLE `users` ( `name` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; -INSERT INTO `onedump` (`char`, `varchar`, `binary`, `varbinary`, `tinyblob`, `tinytext`, `text`, `blob`, `mediumtext`, `mediumblob`, `longtext`, `longblob`, `enum`, `set`, `bit`, `tinyint`, `bool`, `boolean`, `smallint`, `mediumint`, `int`, `bigint`, `float`, `double`, `double_precision`, `decimal`, `dec`, `datetime`, `timestamp`, `time`, `year`, `json`) VALUES -('a', 'abc', '7\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', '7', 'tinyblob', 'tinytext', 'text', 'blob', 'mediumtext', 'mediumblob', 'longtext', 'longblob', '1', '2', b'1', 1, 1, 1, 1, 12, 12, 12, 12, 12.00, 12, 12.00, 12.00, '2024-06-17 16:50:54', '2024-06-17 16:50:58', '16:51:03', '2024', '{\"age\": 25, \"name\": \"Alice\", \"email\": \"alice@example.com\", \"isActive\": true}'); +INSERT INTO `onedump` (`char`, `varchar`, `binary`, `varbinary`, `tinyblob`, `tinytext`, `text`, `blob`, `mediumtext`, `mediumblob`, `longtext`, `longblob`, `enum`, `set`, `bit`, `tinyint`, `bool`, `boolean`, `smallint`, `mediumint`, `int`, `bigint`, `float`, `double`, `double_precision`, `decimal`, `dec`, `date`, `datetime`, `timestamp`, `time`, `year`, `json`) VALUES +('a', 'abc', '7\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', '7', 'tinyblob', 'tinytext', 'text', 'blob', 'mediumtext', 'mediumblob', 'longtext', 'longblob', '1', '2', b'1', 1, 1, 1, 1, 12, 12, 12, 12, 12.00, 12, 12.00, 12.00, '2024-06-17', '2024-06-17 16:50:54', '2024-06-17 16:50:58', '16:51:03', '2024', '{\"age\": 25, \"name\": \"Alice\", \"email\": \"alice@example.com\", \"isActive\": true}'); INSERT INTO `users` (`id`, `name`) VALUES (1, 'julian');