ent/schema: remove string field charset / collation settings

Summary: don't expose starage specific features in ent schema

Reviewed By: a8m

Differential Revision: D17111724

fbshipit-source-id: fca9e624b272c0db3fed14c511fa6cb07816a100
This commit is contained in:
Alex Snast
2019-08-29 07:33:41 -07:00
committed by Facebook Github Bot
parent 4ae7526a65
commit b71ee6820b
36 changed files with 84 additions and 977 deletions

View File

@@ -279,9 +279,6 @@ func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
fallthrough
// change nullability of a column.
case c1.Nullable != c2.Nullable:
fallthrough
// modify character encoding.
case c1.Charset != "" && c1.Charset != c2.Charset || c1.Collation != "" && c1.Collation != c2.Collation:
change.column.modify = append(change.column.modify, c1)
}
}

View File

@@ -51,7 +51,7 @@ func TestMySQL_Create(t *testing.T) {
},
Columns: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString, Nullable: true, Charset: "utf8"},
{Name: "name", Type: field.TypeString, Nullable: true},
{Name: "age", Type: field.TypeInt},
},
},
@@ -63,7 +63,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) CHARACTER SET utf8 NULL, `age` bigint 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, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},
@@ -412,7 +412,7 @@ func TestMySQL_Create(t *testing.T) {
Columns: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "age", Type: field.TypeInt},
{Name: "name", Type: field.TypeString, Nullable: true, Charset: "utf8", Collation: "utf8_general_ci"},
{Name: "name", Type: field.TypeString, Nullable: true},
},
PrimaryKey: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
@@ -436,7 +436,7 @@ func TestMySQL_Create(t *testing.T) {
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
AddRow("PRIMARY", "id", "0", "1"))
mock.ExpectExec(escape("ALTER TABLE `users` MODIFY COLUMN `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL")).
mock.ExpectExec(escape("ALTER TABLE `users` MODIFY COLUMN `name` varchar(255) NULL")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},

View File

@@ -180,8 +180,6 @@ type Column struct {
Increment bool // auto increment attribute.
Nullable bool // null or not null attribute.
Default interface{} // default value.
Charset string // column character set.
Collation string // column collation.
indexes Indexes // linked indexes.
}
@@ -197,16 +195,10 @@ func (c *Column) PrimaryKey() bool { return c.Key == PrimaryKey }
// The syntax/order is: datatype [Charset] [Unique|Increment] [Collation] [Nullable].
func (c *Column) MySQL(version string) *sql.ColumnBuilder {
b := sql.Column(c.Name).Type(c.MySQLType(version)).Attr(c.Attr)
if c.Charset != "" {
b.Attr("CHARACTER SET " + c.Charset)
}
c.unique(b)
if c.Increment {
b.Attr("AUTO_INCREMENT")
}
if c.Collation != "" {
b.Attr("COLLATE " + c.Collation)
}
c.nullable(b)
c.defaultValue(b)
return b
@@ -301,17 +293,13 @@ func (c *Column) SQLiteType() (t string) {
// ScanMySQL scans the information from MySQL column description.
func (c *Column) ScanMySQL(rows *sql.Rows) error {
var (
charset sql.NullString
collate sql.NullString
nullable sql.NullString
defaults sql.NullString
)
if err := rows.Scan(&c.Name, &c.typ, &nullable, &c.Key, &defaults, &c.Attr, &charset, &collate); err != nil {
if err := rows.Scan(&c.Name, &c.typ, &nullable, &c.Key, &defaults, &c.Attr, &sql.NullString{}, &sql.NullString{}); err != nil {
return fmt.Errorf("scanning column description: %v", err)
}
c.Unique = c.UniqueKey()
c.Charset = charset.String
c.Collation = collate.String
if nullable.Valid {
c.Nullable = nullable.String == "YES"
}