From ab96196cfbca0c8ce529e500a737f89acf261b08 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 10 Sep 2024 08:13:22 -0700 Subject: [PATCH] Adding all the lob types. --- lib/mysql/schema/schema.go | 2 +- lib/mysql/schema/schema_test.go | 126 ++++++++++++++++---------------- 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/lib/mysql/schema/schema.go b/lib/mysql/schema/schema.go index 12ccc68e..215199c1 100644 --- a/lib/mysql/schema/schema.go +++ b/lib/mysql/schema/schema.go @@ -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 diff --git a/lib/mysql/schema/schema_test.go b/lib/mysql/schema/schema_test.go index f399a205..0c48be88 100644 --- a/lib/mysql/schema/schema_test.go +++ b/lib/mysql/schema/schema_test.go @@ -15,82 +15,80 @@ 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("`, - }, - } - - for _, testCase := range testCases { - colType, opts, err := parseColumnDataType(testCase.input) - if testCase.expectedErr == "" { + // mediumint unsigned + dataType, _, err := parseColumnDataType("mediumint unsigned") 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, Int, dataType) } } + { + // 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) + } } func TestBuildPkValuesQuery(t *testing.T) {