Repository: ariga/atlas
Pull request #386 (2022-01-09 14:12:48)

sql/mysql: bug fixes in enum types


Patch:
diff --git a/sql/mysql/convert.go b/sql/mysql/convert.go
index 8ce90c87..1f36d58e 100644
--- a/sql/mysql/convert.go
+++ b/sql/mysql/convert.go
@@ -192,6 +192,7 @@ func ParseType(raw string) (schema.Type, error) {
 		}
 		if t == TypeEnum {
 			return &schema.EnumType{
+				T:      TypeEnum,
 				Values: values,
 			}, nil
 		}
diff --git a/sql/mysql/diff.go b/sql/mysql/diff.go
index cc9e89cf..bf725a75 100644
--- a/sql/mysql/diff.go
+++ b/sql/mysql/diff.go
@@ -268,7 +268,7 @@ func (d *diff) defaultChanged(from, to *schema.Column) (bool, error) {
 		return false, nil
 	case *schema.IntegerType:
 		return !d.equalsIntValues(d1, d2), nil
-	case *schema.StringType:
+	case *schema.EnumType, *SetType, *schema.StringType:
 		return !equalsStringValues(d1, d2), nil
 	case *schema.TimeType:
 		x1 := strings.ToLower(strings.Trim(d1, "' ()"))


Test patch:
diff --git a/internal/integration/mysql_test.go b/internal/integration/mysql_test.go
index 3499af41..bb25bf18 100644
--- a/internal/integration/mysql_test.go
+++ b/internal/integration/mysql_test.go
@@ -166,7 +166,7 @@ func TestMySQL_AddColumns(t *testing.T) {
 			&schema.Column{Name: "r", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "int"}}},
 			&schema.Column{Name: "s", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}}},
 			&schema.Column{Name: "t", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "smallint"}}},
-			&schema.Column{Name: "u", Type: &schema.ColumnType{Type: &schema.EnumType{Values: []string{"a", "b", "c"}}}},
+			&schema.Column{Name: "u", Type: &schema.ColumnType{Type: &schema.EnumType{T: "enum", Values: []string{"a", "b", "c"}}}},
 			&schema.Column{Name: "v", Type: &schema.ColumnType{Type: &schema.StringType{T: "char(36)"}}},
 			&schema.Column{Name: "x", Type: &schema.ColumnType{Type: &schema.SpatialType{T: "line"}}},
 			&schema.Column{Name: "y", Type: &schema.ColumnType{Type: &schema.SpatialType{T: "point"}}},
@@ -831,7 +831,7 @@ create table atlas_types_sanity
 					},
 					{
 						Name: "tEnum",
-						Type: &schema.ColumnType{Type: &schema.EnumType{Values: []string{"a", "b"}},
+						Type: &schema.ColumnType{Type: &schema.EnumType{T: "enum", Values: []string{"a", "b"}},
 							Raw: "enum('a','b')", Null: true},
 						Attrs: []schema.Attr{
 							&schema.Charset{V: "latin1"},
diff --git a/sql/mysql/diff_test.go b/sql/mysql/diff_test.go
index ae6f427d..1f2a7055 100644
--- a/sql/mysql/diff_test.go
+++ b/sql/mysql/diff_test.go
@@ -32,6 +32,11 @@ func TestDiff_TableDiff(t *testing.T) {
 			from: &schema.Table{Name: "users", Schema: &schema.Schema{Name: "public"}},
 			to:   &schema.Table{Name: "users"},
 		},
+		{
+			name: "no changes",
+			from: &schema.Table{Name: "users", Schema: &schema.Schema{Name: "public"}, Columns: []*schema.Column{{Name: "enum", Default: &schema.RawExpr{X: "'A'"}, Type: &schema.ColumnType{Type: &schema.EnumType{Values: []string{"A"}}}}}},
+			to:   &schema.Table{Name: "users", Columns: []*schema.Column{{Name: "enum", Default: &schema.RawExpr{X: `"A"`}, Type: &schema.ColumnType{Type: &schema.EnumType{Values: []string{"A"}}}}}},
+		},
 		{
 			name: "change primary key",
 			from: func() *schema.Table {
diff --git a/sql/mysql/inspect_test.go b/sql/mysql/inspect_test.go
index bf50e03c..c733cd75 100644
--- a/sql/mysql/inspect_test.go
+++ b/sql/mysql/inspect_test.go
@@ -333,8 +333,8 @@ func TestDriver_InspectTable(t *testing.T) {
 				require.NoError(err)
 				require.Equal("users", t.Name)
 				require.EqualValues([]*schema.Column{
-					{Name: "c1", Type: &schema.ColumnType{Raw: "enum('a','b')", Type: &schema.EnumType{Values: []string{"a", "b"}}}, Attrs: []schema.Attr{&schema.Charset{V: "latin1"}, &schema.Collation{V: "latin1_swedish_ci"}}},
-					{Name: "c2", Type: &schema.ColumnType{Raw: "enum('c','d')", Type: &schema.EnumType{Values: []string{"c", "d"}}}, Default: &schema.Literal{V: `"d"`}, Attrs: []schema.Attr{&schema.Charset{V: "latin1"}, &schema.Collation{V: "latin1_swedish_ci"}}},
+					{Name: "c1", Type: &schema.ColumnType{Raw: "enum('a','b')", Type: &schema.EnumType{T: "enum", Values: []string{"a", "b"}}}, Attrs: []schema.Attr{&schema.Charset{V: "latin1"}, &schema.Collation{V: "latin1_swedish_ci"}}},
+					{Name: "c2", Type: &schema.ColumnType{Raw: "enum('c','d')", Type: &schema.EnumType{T: "enum", Values: []string{"c", "d"}}}, Default: &schema.Literal{V: `"d"`}, Attrs: []schema.Attr{&schema.Charset{V: "latin1"}, &schema.Collation{V: "latin1_swedish_ci"}}},
 				}, t.Columns)
 			},
 		},
diff --git a/sql/mysql/sqlspec_test.go b/sql/mysql/sqlspec_test.go
index 5962fd49..284e6b2e 100644
--- a/sql/mysql/sqlspec_test.go
+++ b/sql/mysql/sqlspec_test.go
@@ -361,7 +361,7 @@ func TestTypes(t *testing.T) {
 		},
 		{
 			typeExpr: `enum("on","off")`,
-			expected: &schema.EnumType{Values: []string{"on", "off"}},
+			expected: &schema.EnumType{T: TypeEnum, Values: []string{"on", "off"}},
 		},
 		{
 			typeExpr: "bit(10)",
@@ -477,7 +477,7 @@ func TestTypes(t *testing.T) {
 		},
 		{
 			typeExpr: `enum("a","b")`,
-			expected: &schema.EnumType{Values: []string{"a", "b"}},
+			expected: &schema.EnumType{T: "enum", Values: []string{"a", "b"}},
 		},
 		{
 			typeExpr: `set("a","b")`,