dialect/sql/schema: ignore mysql default values for type type (#561)

This commit is contained in:
Ariel Mashraki
2020-06-21 23:16:05 +03:00
committed by GitHub
parent 93c78aeda6
commit 3e7481c3a0
2 changed files with 9 additions and 3 deletions

View File

@@ -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

View File

@@ -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"}).