Repository: ariga/atlas
Pull request #216 (2021-11-11 19:50:48)

sql/mysql/sqlspec: fix type conversion


Patch:
diff --git a/sql/mysql/sqlspec.go b/sql/mysql/sqlspec.go
index 9e486945..2f1107cb 100644
--- a/sql/mysql/sqlspec.go
+++ b/sql/mysql/sqlspec.go
@@ -301,16 +301,16 @@ func binarySpec(t *schema.BinaryType) (*sqlspec.Column, error) {
 		size := specutil.LitAttr("size", strconv.Itoa(t.Size))
 		return specutil.NewCol("", "binary", size), nil
 	}
-	return nil, errors.New("mysql: schema binary failed to convert")
+	return nil, fmt.Errorf("mysql: schema binary failed to convert %q", t.T)
 }
 
 func stringSpec(t *schema.StringType) (*sqlspec.Column, error) {
 	switch t.T {
-	case tVarchar, tMediumText, tLongText:
+	case tVarchar, tMediumText, tLongText, tTinyText, tText, tChar:
 		s := strconv.Itoa(t.Size)
 		return specutil.NewCol("", "string", specutil.LitAttr("size", s)), nil
 	}
-	return nil, errors.New("mysql: schema string failed to convert")
+	return nil, fmt.Errorf("mysql: schema string failed to convert %q", t.T)
 }
 
 func integerSpec(t *schema.IntegerType) (*sqlspec.Column, error) {
@@ -322,13 +322,17 @@ func integerSpec(t *schema.IntegerType) (*sqlspec.Column, error) {
 		return &sqlspec.Column{TypeName: "int"}, nil
 	case tTinyInt:
 		return &sqlspec.Column{TypeName: "int8"}, nil
+	case tMediumInt:
+		return &sqlspec.Column{TypeName: tMediumInt}, nil
+	case tSmallInt:
+		return &sqlspec.Column{TypeName: tSmallInt}, nil
 	case tBigInt:
 		if t.Unsigned {
 			return specutil.NewCol("", "uint64"), nil
 		}
 		return &sqlspec.Column{TypeName: "int64"}, nil
 	}
-	return nil, errors.New("mysql: schema integer failed to convert")
+	return nil, fmt.Errorf("mysql: schema integer failed to convert %q", t.T)
 }
 
 func enumSpec(t *schema.EnumType) (*sqlspec.Column, error) {
diff --git a/sql/postgres/sqlspec.go b/sql/postgres/sqlspec.go
index a7c74d7f..f0b343f1 100644
--- a/sql/postgres/sqlspec.go
+++ b/sql/postgres/sqlspec.go
@@ -274,7 +274,7 @@ func stringSpec(t *schema.StringType) (*sqlspec.Column, error) {
 		s := strconv.Itoa(t.Size)
 		return specutil.NewCol("", "string", specutil.LitAttr("size", s)), nil
 	default:
-		return nil, errors.New("schema string failed to convert")
+		return nil, fmt.Errorf("schema string failed to convert type %T", t)
 	}
 }
 


Test patch:
diff --git a/sql/mysql/sqlspec_test.go b/sql/mysql/sqlspec_test.go
index d23141fc..2fe77dba 100644
--- a/sql/mysql/sqlspec_test.go
+++ b/sql/mysql/sqlspec_test.go
@@ -331,6 +331,20 @@ func TestMarshalSpecColumnType(t *testing.T) {
 			},
 			expected: specutil.NewCol("column", "int8"),
 		},
+		{
+			schem: &schema.IntegerType{
+				T:        tMediumInt,
+				Unsigned: false,
+			},
+			expected: specutil.NewCol("column", tMediumInt),
+		},
+		{
+			schem: &schema.IntegerType{
+				T:        tSmallInt,
+				Unsigned: false,
+			},
+			expected: specutil.NewCol("column", tSmallInt),
+		},
 		{
 			schem: &schema.IntegerType{
 				T:        tBigInt,
@@ -352,6 +366,27 @@ func TestMarshalSpecColumnType(t *testing.T) {
 			},
 			expected: specutil.NewCol("column", "string", specutil.LitAttr("size", "255")),
 		},
+		{
+			schem: &schema.StringType{
+				T:    tTinyText,
+				Size: 255,
+			},
+			expected: specutil.NewCol("column", "string", specutil.LitAttr("size", "255")),
+		},
+		{
+			schem: &schema.StringType{
+				T:    tText,
+				Size: 255,
+			},
+			expected: specutil.NewCol("column", "string", specutil.LitAttr("size", "255")),
+		},
+		{
+			schem: &schema.StringType{
+				T:    tChar,
+				Size: 255,
+			},
+			expected: specutil.NewCol("column", "string", specutil.LitAttr("size", "255")),
+		},
 		{
 			schem: &schema.StringType{
 				T:    tMediumText,