sql/schema: more precise blob definition

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/29

Reviewed By: alexsn

Differential Revision: D17284406

fbshipit-source-id: 84c2ffb50b8f016ad361f1420c5352c7969cbc77
This commit is contained in:
Ariel Mashraki
2019-09-10 12:38:53 -07:00
committed by Facebook Github Bot
parent 2cc229eeb2
commit 7dfe3c174c
23 changed files with 427 additions and 12 deletions

View File

@@ -225,6 +225,43 @@ func TestMySQL_Create(t *testing.T) {
mock.ExpectCommit()
},
},
{
name: "add blob columns",
tables: []*Table{
{
Name: "users",
Columns: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "tiny", Type: field.TypeBytes, Size: 100},
{Name: "blob", Type: field.TypeBytes, Size: 1e3},
{Name: "medium", Type: field.TypeBytes, Size: 1e5},
{Name: "long", Type: field.TypeBytes, Size: 1e8},
},
PrimaryKey: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
},
},
},
before: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectQuery(escape("SHOW VARIABLES LIKE 'version'")).
WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}).AddRow("version", "5.7.23"))
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(1))
mock.ExpectQuery(escape("SELECT `column_name`, `column_type`, `is_nullable`, `column_key`, `column_default`, `extra`, `character_set_name`, `collation_name` FROM INFORMATION_SCHEMA.COLUMNS WHERE `TABLE_SCHEMA` = (SELECT DATABASE()) AND `TABLE_NAME` = ?")).
WithArgs("users").
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", "", ""))
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"}).
AddRow("PRIMARY", "id", "0", "1"))
mock.ExpectExec(escape("ALTER TABLE `users` ADD COLUMN `tiny` tinyblob NOT NULL, ADD COLUMN `blob` blob NOT NULL, ADD COLUMN `medium` mediumblob NOT NULL, ADD COLUMN `long` longblob NOT NULL")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},
},
{
name: "add float column with default value to table",
tables: []*Table{

View File

@@ -6,6 +6,7 @@ package schema
import (
"fmt"
"math"
"strconv"
"strings"
@@ -238,13 +239,26 @@ func (c *Column) MySQLType(version string) (t string) {
case field.TypeUint, field.TypeUint64:
t = "bigint unsigned"
case field.TypeBytes:
t = "blob"
size := math.MaxUint16
if c.Size > 0 {
size = c.Size
}
switch {
case size <= math.MaxUint8:
t = "tinyblob"
case size <= math.MaxUint16:
t = "blob"
case size < 1<<24:
t = "mediumblob"
case size <= math.MaxUint32:
t = "longblob"
}
case field.TypeString:
size := c.Size
if size == 0 {
size = c.defaultSize(version)
}
if size < 1<<16 {
if size <= math.MaxUint16 {
t = fmt.Sprintf("varchar(%d)", size)
} else {
t = "longtext"
@@ -335,7 +349,17 @@ func (c *Column) ScanMySQL(rows *sql.Rows) error {
c.Type = field.TypeFloat64
case "timestamp":
c.Type = field.TypeTime
case "tinyblob":
c.Size = math.MaxUint8
c.Type = field.TypeBytes
case "blob":
c.Size = math.MaxUint16
c.Type = field.TypeBytes
case "mediumblob":
c.Size = 1<<24 - 1
c.Type = field.TypeBytes
case "longblob":
c.Size = math.MaxUint32
c.Type = field.TypeBytes
case "varchar":
c.Type = field.TypeString