Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion cmd/internal/server/handlers/schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,20 @@ func parseEnumOrSetValues(mType string) ValueMap {
// Convert columnType to fivetran type
func getFivetranDataType(mType string, treatTinyIntAsBoolean bool) (fivetransdk.DataType, *fivetransdk.DecimalParams) {
mysqlType := strings.ToLower(mType)
// only interested in first part of the schema output to avoid issues with NOT NULL
parts := strings.Fields(mysqlType)

//empty schema condition
if len(parts) == 0 {
return fivetransdk.DataType_UNSPECIFIED, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice, can we add a test case for this?

}

//get first entry from non zero length slice of column type definition
baseType := parts[0]

//modified conditional to include baseType to only accept tinyint(1)
if strings.HasPrefix(mysqlType, "tinyint") {
if treatTinyIntAsBoolean && mysqlType == "tinyint(1)" {
if treatTinyIntAsBoolean && baseType == "tinyint(1)" {
return fivetransdk.DataType_BOOLEAN, nil
}

Expand Down
27 changes: 27 additions & 0 deletions cmd/internal/server/handlers/schema_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,30 @@ func TestCanDetectDecimalPrecision(t *testing.T) {
})
}
}

//Adding an additional unit test for the tinyint(1) as bool handling since the orginals were not wide enough
Copy link
Collaborator

Choose a reason for hiding this comment

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

can remove this comment and the next line


func TestGetFivetranDataType_TinyintBooleanHandling(t *testing.T) {
tests := []struct {
mysqlType string
treatTinyIntAsBoolean bool
expected fivetransdk.DataType
}{
{"tinyint(1)", true, fivetransdk.DataType_BOOLEAN},
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there any reason these test cases can't fit as cases into the existing TestSchema_CanPickRightFivetranType test rather than defining a new test?

{"tinyint(1) NOT NULL", true, fivetransdk.DataType_BOOLEAN},
{"tinyint(1) unsigned", true, fivetransdk.DataType_BOOLEAN},
{"tinyint(2)", true, fivetransdk.DataType_INT},
{"tinyint", true, fivetransdk.DataType_INT},
{"TINYINT(1)", true, fivetransdk.DataType_BOOLEAN},
{"tinyint(1)", false, fivetransdk.DataType_INT},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%s_bool_%t", tt.mysqlType, tt.treatTinyIntAsBoolean), func(t *testing.T) {
got, _ := getFivetranDataType(tt.mysqlType, tt.treatTinyIntAsBoolean)
if got != tt.expected {
t.Errorf("For input '%s', expected %s but got %s", tt.mysqlType, tt.expected, got)
}
})
}
}