schema/field: add an option to configure the database type (#484)

This commit is contained in:
Ariel Mashraki
2020-05-11 15:07:33 +03:00
committed by GitHub
parent 68db86be76
commit cfee55e514
25 changed files with 1126 additions and 26 deletions

View File

@@ -218,7 +218,7 @@ func (d *MySQL) cType(c *Column) (t string) {
t = "longtext"
}
case field.TypeFloat32, field.TypeFloat64:
t = "double"
t = c.scanTypeOr("double")
case field.TypeTime:
t = c.scanTypeOr("timestamp")
// In MySQL, timestamp columns are `NOT NULL by default, and assigning NULL
@@ -354,7 +354,7 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
default:
c.Type = field.TypeInt8
}
case "double":
case "numeric", "decimal", "double":
c.Type = field.TypeFloat64
case "time", "timestamp", "date", "datetime":
c.Type = field.TypeTime

View File

@@ -161,6 +161,7 @@ func TestMySQL_Create(t *testing.T) {
{Name: "small_unsigned", Type: field.TypeUint16},
{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)"}},
},
PrimaryKey: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
@@ -184,7 +185,8 @@ func TestMySQL_Create(t *testing.T) {
AddRow("small", "smallint", "NO", "YES", "NULL", "", "", "").
AddRow("small_unsigned", "smallint unsigned", "NO", "YES", "NULL", "", "", "").
AddRow("big", "bigint", "NO", "YES", "NULL", "", "", "").
AddRow("big_unsigned", "bigint unsigned", "NO", "YES", "NULL", "", "", ""))
AddRow("big_unsigned", "bigint unsigned", "NO", "YES", "NULL", "", "", "").
AddRow("decimal", "decimal(6,2)", "NO", "YES", "NULL", "", "", ""))
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"}).

View File

@@ -222,7 +222,7 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
c.Type = field.TypeInt64
case "real":
c.Type = field.TypeFloat32
case "double precision":
case "numeric", "decimal", "double precision":
c.Type = field.TypeFloat64
case "text":
c.Type = field.TypeString
@@ -278,9 +278,9 @@ func (d *Postgres) cType(c *Column) (t string) {
case field.TypeInt, field.TypeUint, field.TypeInt64, field.TypeUint64:
t = "bigint"
case field.TypeFloat32:
t = "real"
t = c.scanTypeOr("real")
case field.TypeFloat64:
t = "double precision"
t = c.scanTypeOr("double precision")
case field.TypeBytes:
t = "bytea"
case field.TypeJSON: