Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes write table for date type #33

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dumper/mysqlnativedump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"log/slog"
"net"
"strings"
"time"

"github.com/go-sql-driver/mysql"
"github.com/liweiyi88/onedump/config"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions dumper/mysqlnativedump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"),
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions testutils/onedump_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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\": \"[email protected]\", \"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\": \"[email protected]\", \"isActive\": true}');

INSERT INTO `users` (`id`, `name`) VALUES (1, 'julian');

Expand Down
Loading