mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql/schema: ignore mysql default values for type type (#561)
This commit is contained in:
@@ -166,7 +166,8 @@ func (d *MySQL) tBuilder(t *Table) *sql.TableBuilder {
|
||||
// cType returns the MySQL string type for the given column.
|
||||
func (d *MySQL) cType(c *Column) (t string) {
|
||||
if c.SchemaType != nil && c.SchemaType[dialect.MySQL] != "" {
|
||||
return c.SchemaType[dialect.MySQL]
|
||||
// MySQL returns the column type lower cased.
|
||||
return strings.ToLower(c.SchemaType[dialect.MySQL])
|
||||
}
|
||||
switch c.Type {
|
||||
case field.TypeBool:
|
||||
@@ -223,7 +224,7 @@ func (d *MySQL) cType(c *Column) (t string) {
|
||||
t = c.scanTypeOr("timestamp")
|
||||
// In MySQL, timestamp columns are `NOT NULL by default, and assigning NULL
|
||||
// assigns the current_timestamp(). We avoid this if not set otherwise.
|
||||
c.Nullable = true
|
||||
c.Nullable = c.Attr == ""
|
||||
case field.TypeEnum:
|
||||
values := make([]string, len(c.Enums))
|
||||
for i, e := range c.Enums {
|
||||
@@ -358,6 +359,9 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
c.Type = field.TypeFloat64
|
||||
case "time", "timestamp", "date", "datetime":
|
||||
c.Type = field.TypeTime
|
||||
// The mapping from schema defaults to database
|
||||
// defaults is not supported for TypeTime fields.
|
||||
defaults = sql.NullString{}
|
||||
case "tinyblob":
|
||||
c.Size = math.MaxUint8
|
||||
c.Type = field.TypeBytes
|
||||
|
||||
@@ -162,6 +162,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
{Name: "big", Type: field.TypeInt64},
|
||||
{Name: "big_unsigned", Type: field.TypeUint64},
|
||||
{Name: "decimal", Type: field.TypeFloat64, SchemaType: map[string]string{dialect.MySQL: "decimal(6,2)"}},
|
||||
{Name: "timestamp", Type: field.TypeTime, SchemaType: map[string]string{dialect.MySQL: "TIMESTAMP"}},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
@@ -186,7 +187,8 @@ func TestMySQL_Create(t *testing.T) {
|
||||
AddRow("small_unsigned", "smallint unsigned", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("big", "bigint", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("big_unsigned", "bigint unsigned", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("decimal", "decimal(6,2)", "NO", "YES", "NULL", "", "", ""))
|
||||
AddRow("decimal", "decimal(6,2)", "NO", "YES", "NULL", "", "", "").
|
||||
AddRow("timestamp", "timestamp", "NO", "NO", "CURRENT_TIMESTAMP", "DEFAULT_GENERATED on update CURRENT_TIMESTAMP", "", ""))
|
||||
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` = ? ORDER BY `index_name`, `seq_in_index`")).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "non_unique", "seq_in_index"}).
|
||||
|
||||
Reference in New Issue
Block a user