Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jul 7, 2024
1 parent cd051ea commit 6f4c889
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/postgres/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ func ParseValue(colKind schema.DataType, value any) (any, error) {
}

return nil, fmt.Errorf("value: %v not of string type for Numeric or VariableNumeric", value)
case schema.Time, schema.TimeWithTimeZone:
case schema.TimeWithTimeZone:
stringValue, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected string got %T with value: %v", value, value)
}

return stringValue, nil
case schema.Time:
stringValue, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected string got %T with value: %v", value, value)
Expand Down
5 changes: 0 additions & 5 deletions lib/postgres/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ func (s scanAdapter) ParsePrimaryKeyValueForOverrides(columnName string, value s
func castColumn(col schema.Column) string {
colName := pgx.Identifier{col.Name}.Sanitize()
switch col.Type {
case schema.TimeWithTimeZone:
// If we don't convert `time with time zone` to UTC we end up with strings like `10:23:54-02`
// And pgtype.Time doesn't parse the offset properly.
// See https://github.com/jackc/pgx/issues/1940
return fmt.Sprintf(`%s AT TIME ZONE 'UTC' AS %q`, colName, col.Name)
case schema.Array:
return fmt.Sprintf(`ARRAY_TO_JSON(%s)::TEXT as %q`, colName, col.Name)
default:
Expand Down
4 changes: 3 additions & 1 deletion sources/postgres/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func valueConverterForType(dataType schema.DataType, opts *schema.Opts) (convert
return converters.BytesPassthrough{}, nil
case schema.Text, schema.UserDefinedText:
return converters.StringPassthrough{}, nil
case schema.Time, schema.TimeWithTimeZone:
case schema.TimeWithTimeZone:
return TimeWithTimezoneConverter{}, nil
case schema.Time:
return PgTimeConverter{}, nil
case schema.Date:
return converters.DateConverter{}, nil
Expand Down
28 changes: 28 additions & 0 deletions sources/postgres/adapter/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)

type TimeWithTimezoneConverter struct{}

func (TimeWithTimezoneConverter) ToField(name string) debezium.Field {
return debezium.Field{
FieldName: name,
Type: debezium.String,
DebeziumType: debezium.TimeWithTimezone,
}
}

func (TimeWithTimezoneConverter) Convert(value any) (any, error) {
stringValue, ok := value.(string)
if !ok {
return nil, fmt.Errorf("expected string got %T with value: %v", value, value)
}

inputLayout := "15:04:05.000000-07"
timeValue, err := time.Parse(inputLayout, stringValue)
if err != nil {
return nil, fmt.Errorf("failed to parse time value %q: %w", stringValue, err)
}

// We need to parse this value into `time.Time`
// Then convert it back into a string where the timezone is GMT to match Debezium.
outputLayout := "15:04:05.000000Z"
return timeValue.UTC().Format(outputLayout), nil
}

type PgTimeConverter struct{}

func (PgTimeConverter) ToField(name string) debezium.Field {
Expand Down

0 comments on commit 6f4c889

Please sign in to comment.