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

MySQL datetime guardrail #428

merged 5 commits into from
Jul 3, 2024

Conversation

Tang8330
Copy link
Collaborator

@Tang8330 Tang8330 commented Jul 2, 2024

If strict mode isn't enabled, MySQL may allow invalid datetime / timestamps.

@Tang8330 Tang8330 marked this pull request as ready for review July 2, 2024 20:45
@Tang8330 Tang8330 requested a review from nathan-artie July 2, 2024 20:45
@Tang8330 Tang8330 added the bug Something isn't working label Jul 2, 2024
// 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 strings.HasSuffix(stringValue, "-00-00 00:00:00") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reading the MySQL docs it seems like it's also possible for the date to be non-zero and the month zero, or vice versa, so maybe what we want to do is lop off just the date, split by hyphens, and then check if any of the three segments is only zeros, something like:

func hasNonStrictModeDate(d string) bool {
	if len(d) < 10 {
		return false
	}
	parts := strings.Split(d[:10], "-")
	if len(parts) != 3 {
		return false
	}
	for _, part := range parts {
		value, err := strconv.Atoi(part)
		if err != nil {
			return false
		}
		if value == 0 {
			return true
		}
	}
	return false
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

date to be non-zero and the month zero

Oh god.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah makes sense

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not to mention whether it's possible to have a zero day/month but a non-zero h/m/s. 😬

@@ -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.

@@ -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.

@Tang8330 Tang8330 merged commit f2af92b into master Jul 3, 2024
5 checks passed
@Tang8330 Tang8330 deleted the guardrail-around-mysql branch July 3, 2024 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants