From 95cd7fee33f1fd928f9f926ae38128cb19547233 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 27 Aug 2024 10:58:31 -0700 Subject: [PATCH] [Debezium] Adding String / UUID passthrough converter (#854) --- clients/shared/default_value.go | 3 +-- lib/debezium/converters/string.go | 18 ++++++++++++++++++ lib/debezium/converters/string_test.go | 21 +++++++++++++++++++++ lib/debezium/schema.go | 2 ++ lib/debezium/schema_test.go | 5 +++++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 lib/debezium/converters/string.go create mode 100644 lib/debezium/converters/string_test.go diff --git a/clients/shared/default_value.go b/clients/shared/default_value.go index 8883c78cc..41ab38cab 100644 --- a/clients/shared/default_value.go +++ b/clients/shared/default_value.go @@ -80,8 +80,7 @@ func BackfillColumn(dwh destination.DataWarehouse, column columns.Column, tableI slog.String("table", tableID.FullyQualifiedName()), ) - _, err = dwh.Exec(query) - if err != nil { + if _, err = dwh.Exec(query); err != nil { return fmt.Errorf("failed to backfill, err: %w, query: %v", err, query) } diff --git a/lib/debezium/converters/string.go b/lib/debezium/converters/string.go new file mode 100644 index 000000000..2d591a78d --- /dev/null +++ b/lib/debezium/converters/string.go @@ -0,0 +1,18 @@ +package converters + +import "github.com/artie-labs/transfer/lib/typing" + +type StringPassthrough struct{} + +func (StringPassthrough) Convert(value any) (any, error) { + castedValue, err := typing.AssertType[string](value) + if err != nil { + return nil, err + } + + return castedValue, nil +} + +func (StringPassthrough) ToKindDetails() typing.KindDetails { + return typing.String +} diff --git a/lib/debezium/converters/string_test.go b/lib/debezium/converters/string_test.go new file mode 100644 index 000000000..7ac624670 --- /dev/null +++ b/lib/debezium/converters/string_test.go @@ -0,0 +1,21 @@ +package converters + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStringPassthrough_Convert(t *testing.T) { + { + // Non string + _, err := StringPassthrough{}.Convert(1) + assert.ErrorContains(t, err, "expected type string, got int") + } + { + // String + value, err := StringPassthrough{}.Convert("test") + assert.Nil(t, err) + assert.Equal(t, "test", value) + } +} diff --git a/lib/debezium/schema.go b/lib/debezium/schema.go index 2453f6066..542efd4ca 100644 --- a/lib/debezium/schema.go +++ b/lib/debezium/schema.go @@ -82,6 +82,8 @@ func (f Field) GetScaleAndPrecision() (int32, *int32, error) { func (f Field) ToValueConverter() converters.ValueConverter { switch f.DebeziumType { + case UUID: + return converters.StringPassthrough{} case DateTimeWithTimezone: return converters.DateTimeWithTimezone{} case TimeWithTimezone: diff --git a/lib/debezium/schema_test.go b/lib/debezium/schema_test.go index e7c74f462..939def9d5 100644 --- a/lib/debezium/schema_test.go +++ b/lib/debezium/schema_test.go @@ -271,6 +271,11 @@ func TestField_ToKindDetails(t *testing.T) { }, expectedKindDetails: typing.Struct, }, + { + name: "UUID", + field: Field{DebeziumType: UUID, Type: String}, + expectedKindDetails: typing.String, + }, } for _, tc := range tcs {