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] Adding all the LOB types + refactoring tests #488

Merged
merged 2 commits into from
Sep 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/mysql/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func parseColumnDataType(originalS string) (DataType, *Opts, error) {
return Binary, nil, nil
case "varbinary":
return Varbinary, nil, nil
case "blob":
case "blob", "tinyblob", "mediumblob", "longblob":
return Blob, nil, nil
case "text":
return Text, nil, nil
Expand Down
132 changes: 69 additions & 63 deletions lib/mysql/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,86 @@ func TestQuoteIdentifier(t *testing.T) {
}

func TestParseColumnDataType(t *testing.T) {
testCases := []struct {
input string
expectedType DataType
expectedOpts *Opts
expectedErr string
}{
{
input: "int",
expectedType: Int,
},
{
input: "tinyint(1)",
expectedType: Boolean,
},
{
input: "varchar(255)",
expectedType: Varchar,
expectedOpts: &Opts{Size: ptr.ToInt(255)},
},
{
// Invalid
{
input: "decimal(5,2)",
expectedType: Decimal,
expectedOpts: &Opts{
Precision: ptr.ToInt(5),
Scale: ptr2.ToUint16(2),
},
},
_, _, err := parseColumnDataType("int(10 unsigned")
assert.ErrorContains(t, err, `malformed data type: "int(10 unsigned"`)
}
{
input: "int(10) unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("foo")
assert.ErrorContains(t, err, `unknown data type: "foo"`)
}
{
input: "tinyint unsigned",
expectedType: SmallInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("varchar(")
assert.ErrorContains(t, err, `malformed data type: "varchar("`)
}
}
{
// Integers
{
input: "smallint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int
dataType, _, err := parseColumnDataType("int")
assert.NoError(t, err)
assert.Equal(t, Int, dataType)
}
{
input: "mediumint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int unsigned
dataType, _, err := parseColumnDataType("int unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
// int(10) unsigned
dataType, _, err := parseColumnDataType("int(10) unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int(10 unsigned",
expectedErr: `malformed data type: "int(10 unsigned"`,
},
// tinyint
dataType, _, err := parseColumnDataType("tinyint")
assert.NoError(t, err)
assert.Equal(t, TinyInt, dataType)
}
{
input: "foo",
expectedErr: `unknown data type: "foo"`,
},
// tinyint unsigned
dataType, _, err := parseColumnDataType("tinyint unsigned")
assert.NoError(t, err)
assert.Equal(t, SmallInt, dataType)
}
{
input: "varchar(",
expectedErr: `malformed data type: "varchar("`,
},
// mediumint unsigned
dataType, _, err := parseColumnDataType("mediumint unsigned")
assert.NoError(t, err)
assert.Equal(t, Int, dataType)
}
}

for _, testCase := range testCases {
colType, opts, err := parseColumnDataType(testCase.input)
if testCase.expectedErr == "" {
{
// tinyint(1) or boolean
dataType, _, err := parseColumnDataType("tinyint(1)")
assert.NoError(t, err)
assert.Equal(t, Boolean, dataType)
}
{
// String
dataType, opts, err := parseColumnDataType("varchar(255)")
assert.NoError(t, err)
assert.Equal(t, Varchar, dataType)
assert.Equal(t, &Opts{Size: ptr.ToInt(255)}, opts)
}
{
// Decimal
dataType, opts, err := parseColumnDataType("decimal(5,2)")
assert.NoError(t, err)
assert.Equal(t, Decimal, dataType)
assert.Equal(t, &Opts{Precision: ptr.ToInt(5), Scale: ptr2.ToUint16(2)}, opts)
}
{
// Blob
for _, blob := range []string{"blob", "tinyblob", "mediumblob", "longblob"} {
dataType, _, err := parseColumnDataType(blob)
assert.NoError(t, err)
assert.Equal(t, testCase.expectedType, colType, testCase.input)
assert.Equal(t, testCase.expectedOpts, opts, testCase.input)
} else {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.input)
assert.Equal(t, Blob, dataType, blob)
}
}
}
Expand Down
Loading