-
Notifications
You must be signed in to change notification settings - Fork 3
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,postgres] Change Debezium conversion for timestamp types #317
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,18 +67,37 @@ func (DateConverter) Convert(value any) (any, error) { | |
return int32(timeValue.Unix() / (60 * 60 * 24)), nil | ||
} | ||
|
||
type TimestampConverter struct{} | ||
type MicroTimestampConverter struct{} | ||
|
||
func (TimestampConverter) ToField(name string) debezium.Field { | ||
func (MicroTimestampConverter) ToField(name string) debezium.Field { | ||
// Represents the number of microseconds since the epoch, and does not include timezone information. | ||
return debezium.Field{ | ||
FieldName: name, | ||
// NOTE: We are returning string here because we want the right layout to be used by our Typing library | ||
FieldName: name, | ||
Type: "int64", | ||
DebeziumType: string(debezium.MicroTimestamp), | ||
} | ||
} | ||
|
||
func (MicroTimestampConverter) Convert(value any) (any, error) { | ||
timeValue, ok := value.(time.Time) | ||
if !ok { | ||
return nil, fmt.Errorf("expected time.Time got %T with value: %v", value, value) | ||
} | ||
return timeValue.UnixMicro(), nil | ||
} | ||
|
||
type ZonedTimestampConverter struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically renamed |
||
|
||
func (ZonedTimestampConverter) ToField(name string) debezium.Field { | ||
// A string representation of a timestamp with timezone information, where the timezone is GMT. | ||
return debezium.Field{ | ||
FieldName: name, | ||
Type: "string", | ||
DebeziumType: string(debezium.Timestamp), | ||
DebeziumType: string(debezium.DateTimeWithTimezone), | ||
} | ||
} | ||
|
||
func (TimestampConverter) Convert(value any) (any, error) { | ||
func (ZonedTimestampConverter) Convert(value any) (any, error) { | ||
timeValue, ok := value.(time.Time) | ||
if !ok { | ||
return nil, fmt.Errorf("expected time.Time got %T with value: %v", value, value) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,8 +121,35 @@ func TestDateConverter_Convert(t *testing.T) { | |
} | ||
} | ||
|
||
func TestTimestampConverter_Convert(t *testing.T) { | ||
converter := TimestampConverter{} | ||
func TestMicroTimestampConverter_Convert(t *testing.T) { | ||
converter := MicroTimestampConverter{} | ||
{ | ||
// Invalid type | ||
_, err := converter.Convert(1234) | ||
assert.ErrorContains(t, err, "expected time.Time got int with value: 1234") | ||
} | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to worry about dates > 9999 or < 0 for MicroTimestamps since it uses an int64 and won't have JSON encoding issues. |
||
// Date > 9999 | ||
value, err := converter.Convert(time.Date(9_9999, 2, 3, 4, 5, 0, 0, time.UTC)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, value, int64(3093499310700000000)) | ||
} | ||
{ | ||
// Date < 0 | ||
value, err := converter.Convert(time.Date(-1, 2, 3, 4, 5, 0, 0, time.UTC)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these tests, can we have it also run against Transfer to ensure Transfer is able to parse it correctly? Like run it against this function: https://github.com/artie-labs/transfer/blob/52a52c9cbe92e506c7d4a24757440979d28ef8a8/lib/debezium/types.go#L72 |
||
assert.NoError(t, err) | ||
assert.Equal(t, int64(-62195889300000000), value) | ||
} | ||
{ | ||
// time.Time | ||
value, err := converter.Convert(time.Date(2001, 2, 3, 4, 5, 0, 0, time.UTC)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, int64(981173100000000), value) | ||
} | ||
} | ||
|
||
func TestZonedTimestampConverter_Convert(t *testing.T) { | ||
converter := ZonedTimestampConverter{} | ||
{ | ||
// Invalid type | ||
_, err := converter.Convert(1234) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a new
MicroTimestampConverter
.