mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/gen: allow spaces in enum fields
This commit is contained in:
committed by
Ariel Mashraki
parent
5262810d8c
commit
4c2faf2282
@@ -471,9 +471,15 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
c.Type = field.TypeJSON
|
||||
case "enum":
|
||||
c.Type = field.TypeEnum
|
||||
c.Enums = make([]string, len(parts)-1)
|
||||
for i, e := range parts[1:] {
|
||||
c.Enums[i] = strings.Trim(e, "'")
|
||||
// Parse the enum values according to the MySQL format.
|
||||
// github.com/mysql/mysql-server/blob/8.0/sql/field.cc#Field_enum::sql_type
|
||||
values := strings.TrimSuffix(strings.TrimPrefix(c.typ, "enum("), ")")
|
||||
if values == "" {
|
||||
return fmt.Errorf("mysql: unexpected enum type: %q", c.typ)
|
||||
}
|
||||
parts := strings.Split(values, "','")
|
||||
for i := range parts {
|
||||
c.Enums = append(c.Enums, strings.Trim(parts[i], "'"))
|
||||
}
|
||||
case "char":
|
||||
c.Type = field.TypeOther
|
||||
|
||||
@@ -307,8 +307,9 @@ func TestMySQL_Create(t *testing.T) {
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "name", Type: field.TypeString, Nullable: true},
|
||||
{Name: "enums1", Type: field.TypeEnum, Enums: []string{"a", "b"}}, // add enum.
|
||||
{Name: "enums2", Type: field.TypeEnum, Enums: []string{"a"}}, // remove enum.
|
||||
{Name: "enums1", Type: field.TypeEnum, Enums: []string{"a", "b"}}, // add enum.
|
||||
{Name: "enums2", Type: field.TypeEnum, Enums: []string{"a"}}, // remove enum.
|
||||
{Name: "enums3", Type: field.TypeEnum, Enums: []string{"a", "b c"}}, // no changes.
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
@@ -324,7 +325,8 @@ func TestMySQL_Create(t *testing.T) {
|
||||
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "", nil, nil).
|
||||
AddRow("name", "varchar(255)", "YES", "YES", "NULL", "", "", "", nil, nil).
|
||||
AddRow("enums1", "enum('a')", "YES", "NO", "NULL", "", "", "", nil, nil).
|
||||
AddRow("enums2", "enum('b', 'a')", "NO", "YES", "NULL", "", "", "", nil, nil))
|
||||
AddRow("enums2", "enum('b', 'a')", "NO", "YES", "NULL", "", "", "", nil, nil).
|
||||
AddRow("enums3", "enum('a', 'b c')", "NO", "YES", "NULL", "", "", "", nil, nil))
|
||||
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `sub_part`, `non_unique`, `seq_in_index` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ? ORDER BY `index_name`, `seq_in_index`")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "sub_part", "non_unique", "seq_in_index"}).
|
||||
|
||||
Reference in New Issue
Block a user