mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql/schema: mysql support for uuid type
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/176 Reviewed By: alexsn Differential Revision: D18615992 fbshipit-source-id: 2c86a661e8d75165470b9b2d351cb973147290d8
This commit is contained in:
committed by
Facebook Github Bot
parent
f7dac21972
commit
703a6dd039
@@ -190,6 +190,8 @@ func (d *MySQL) cType(c *Column) (t string) {
|
||||
}
|
||||
sort.Strings(values)
|
||||
t = fmt.Sprintf("enum(%s)", strings.Join(values, ", "))
|
||||
case field.TypeUUID:
|
||||
t = "char(36) binary"
|
||||
default:
|
||||
panic(fmt.Sprintf("unsupported type %q for column %q", c.Type.String(), c.Name))
|
||||
}
|
||||
@@ -306,6 +308,18 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
for i, e := range parts[1:] {
|
||||
c.Enums[i] = strings.Trim(e, "'")
|
||||
}
|
||||
case "char":
|
||||
size, err := strconv.ParseInt(parts[1], 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting char size to int: %v", err)
|
||||
}
|
||||
// UUID field has length of 36 characters (32 alphanumeric characters and 4 hyphens).
|
||||
if size != 36 {
|
||||
return fmt.Errorf("unknown char(%d) type (not a uuid)", size)
|
||||
}
|
||||
c.Type = field.TypeUUID
|
||||
default:
|
||||
return fmt.Errorf("unknown column type %q for version %q", parts[0], d.version)
|
||||
}
|
||||
if defaults.Valid {
|
||||
return c.ScanDefault(defaults.String)
|
||||
|
||||
@@ -56,6 +56,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
{Name: "age", Type: field.TypeInt},
|
||||
{Name: "doc", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "enums", Type: field.TypeEnum, Enums: []string{"a", "b"}},
|
||||
{Name: "uuid", Type: field.TypeUUID, Nullable: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -66,7 +67,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
mock.ExpectQuery(escape("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
|
||||
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4")).
|
||||
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
@@ -166,6 +167,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "name", Type: field.TypeString, Nullable: true},
|
||||
{Name: "text", Type: field.TypeString, Nullable: true, Size: math.MaxInt32},
|
||||
{Name: "uuid", Type: field.TypeUUID, Nullable: true},
|
||||
{Name: "age", Type: field.TypeInt},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
@@ -185,7 +187,8 @@ func TestMySQL_Create(t *testing.T) {
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "column_type", "is_nullable", "column_key", "column_default", "extra", "character_set_name", "collation_name"}).
|
||||
AddRow("id", "bigint(20)", "NO", "PRI", "NULL", "auto_increment", "", "").
|
||||
AddRow("name", "varchar(255)", "YES", "YES", "NULL", "", "", "").
|
||||
AddRow("text", "longtext", "YES", "YES", "NULL", "", "", ""))
|
||||
AddRow("text", "longtext", "YES", "YES", "NULL", "", "", "").
|
||||
AddRow("uuid", "char(36)", "YES", "YES", "NULL", "", "", "utf8mb4_bin"))
|
||||
mock.ExpectQuery(escape("SELECT `index_name`, `column_name`, `non_unique`, `seq_in_index` FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
|
||||
Reference in New Issue
Block a user