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

MySQL datetime guardrail #428

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 29 additions & 4 deletions lib/mysql/schema/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/binary"
"fmt"
"math"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -82,13 +84,12 @@ func ConvertValue(value any, colType DataType) (any, error) {
return nil, fmt.Errorf("expected []byte got %T for value: %v", value, value)
}

if string(bytesValue) == "0000-00-00 00:00:00" {
// MySQL supports '0000-00-00 00:00:00' for datetime columns.
// We are returning `nil` here because this will fail most Time parsers.
stringValue := string(bytesValue)
if hasNonStrictModeDate(stringValue) {
return nil, nil
}

timeValue, err := time.Parse(DateTimeFormat, string(bytesValue))
timeValue, err := time.Parse(DateTimeFormat, stringValue)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,3 +185,27 @@ func ConvertValues(values []any, cols []Column) error {
}
return nil
}

// hasNonStrictModeDate - if strict mode is not enabled, we can end up having invalid datetimes
func hasNonStrictModeDate(d string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, I know I suggested this name, but maybe we could call this hasNonStrictModeInvalidDate to be a little clearer.

if len(d) < 10 {
return false
}

parts := strings.Split(d[:10], "-")
if len(parts) != 3 {
return false
}

// Year, month, date cannot be non-zero
for _, part := range parts {
value, err := strconv.Atoi(part)
if err != nil {
return false
}
if value == 0 {
return true
}
}
return false
}
10 changes: 10 additions & 0 deletions lib/mysql/schema/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,13 @@ func TestConvertValues(t *testing.T) {
assert.Equal(t, []any{int64(1234), "hello world", true}, values)
}
}

func TestHasNonStrictModeDate(t *testing.T) {
assert.False(t, hasNonStrictModeDate("2021-01-02"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a test for an empty string, a junk string, and a string with just two hyphens, all of which should return false.

assert.False(t, hasNonStrictModeDate("2021-01-02 03:04:05"))

assert.True(t, hasNonStrictModeDate("2009-00-00"))
assert.True(t, hasNonStrictModeDate("0000-00-00"))
assert.True(t, hasNonStrictModeDate("0000-00-00 00:00:00"))
assert.True(t, hasNonStrictModeDate("2009-00-00 00:00:00"))
}