sql/schema: support custom char type for field.Other

Fixed #1664
This commit is contained in:
Ariel Mashraki
2021-06-29 16:22:59 +03:00
committed by Ariel Mashraki
parent 7d55d70750
commit 7ffdce4cef
19 changed files with 1160 additions and 32 deletions

View File

@@ -389,6 +389,9 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
if nullable.Valid {
c.Nullable = nullable.String == "YES"
}
if c.typ == "" {
return fmt.Errorf("missing type information for column %q", c.Name)
}
parts, size, unsigned, err := parseColumn(c.typ)
if err != nil {
return err
@@ -458,11 +461,11 @@ func (d *MySQL) scanColumn(c *Column, rows *sql.Rows) error {
c.Enums[i] = strings.Trim(e, "'")
}
case "char":
c.Type = field.TypeOther
// UUID field has length of 36 characters (32 alphanumeric characters and 4 hyphens).
if size != 36 {
return fmt.Errorf("unknown char(%d) type (not a uuid)", size)
if size == 36 {
c.Type = field.TypeUUID
}
c.Type = field.TypeUUID
case "point", "geometry", "linestring", "polygon":
c.Type = field.TypeOther
default:
@@ -652,7 +655,9 @@ func parseColumn(typ string) (parts []string, size int64, unsigned bool, err err
size, err = strconv.ParseInt(parts[1], 10, 0)
}
case "varbinary", "varchar", "char", "binary":
size, err = strconv.ParseInt(parts[1], 10, 64)
if len(parts) > 1 {
size, err = strconv.ParseInt(parts[1], 10, 64)
}
}
if err != nil {
return parts, size, unsigned, fmt.Errorf("converting %s size to int: %w", parts[0], err)

View File

@@ -282,7 +282,10 @@ func (d *SQLite) scanColumn(c *Column, rows *sql.Rows) error {
if pk.Int64 > 0 {
c.Key = PrimaryKey
}
parts, _, _, err := parseColumn(c.typ)
if c.typ == "" {
return fmt.Errorf("missing type information for column %q", c.Name)
}
parts, size, _, err := parseColumn(c.typ)
if err != nil {
return err
}
@@ -302,8 +305,8 @@ func (d *SQLite) scanColumn(c *Column, rows *sql.Rows) error {
c.Type = field.TypeJSON
case "uuid":
c.Type = field.TypeUUID
case "varchar", "text":
c.Size = DefaultStringLen
case "varchar", "char", "text":
c.Size = size
c.Type = field.TypeString
case "decimal", "numeric":
c.Type = field.TypeOther
@@ -339,5 +342,6 @@ func (d *SQLite) tables() sql.Querier {
// needsConversion reports if column "old" needs to be converted
// (by table altering) to column "new".
func (d *SQLite) needsConversion(old, new *Column) bool {
return d.cType(old) != d.cType(new)
c1, c2 := d.cType(old), d.cType(new)
return c1 != c2 && old.typ != c2
}