dialect/sql/schema: add optional schema-type to column spec (#461)

This commit is contained in:
Ariel Mashraki
2020-05-04 20:16:43 +03:00
committed by GitHub
parent b456251154
commit 51cdda421f
7 changed files with 34 additions and 17 deletions

View File

@@ -159,6 +159,9 @@ 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]
}
switch c.Type {
case field.TypeBool:
t = "boolean"

View File

@@ -11,6 +11,7 @@ import (
"strings"
"testing"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/schema/field"
@@ -55,13 +56,15 @@ func TestMySQL_Create(t *testing.T) {
{Name: "doc", Type: field.TypeJSON, Nullable: true},
{Name: "enums", Type: field.TypeEnum, Enums: []string{"a", "b"}},
{Name: "uuid", Type: field.TypeUUID, Nullable: true},
{Name: "datetime", Type: field.TypeTime, SchemaType: map[string]string{dialect.MySQL: "datetime"}, Nullable: true},
{Name: "decimal", Type: field.TypeFloat32, SchemaType: map[string]string{dialect.MySQL: "decimal(6,2)"}},
},
},
},
before: func(mock mysqlMock) {
mock.start("5.7.8")
mock.tableExists("users", false)
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4")).
mock.ExpectExec(escape("CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(255) NULL, `age` bigint NOT NULL, `doc` json NULL, `enums` enum('a', 'b') NOT NULL, `uuid` char(36) binary NULL, `datetime` datetime NULL, `decimal` decimal(6,2) NOT NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},

View File

@@ -256,6 +256,9 @@ func (d *Postgres) tBuilder(t *Table) *sql.TableBuilder {
// cType returns the PostgreSQL string type for this column.
func (d *Postgres) cType(c *Column) (t string) {
if c.SchemaType != nil && c.SchemaType[dialect.Postgres] != "" {
return c.SchemaType[dialect.Postgres]
}
switch c.Type {
case field.TypeBool:
t = "boolean"

View File

@@ -11,10 +11,11 @@ import (
"strings"
"testing"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/schema/field"
"github.com/DATA-DOG/go-sqlmock"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/stretchr/testify/require"
)
@@ -62,13 +63,14 @@ func TestPostgres_Create(t *testing.T) {
{Name: "doc", Type: field.TypeJSON, Nullable: true},
{Name: "enums", Type: field.TypeEnum, Enums: []string{"a", "b"}},
{Name: "uuid", Type: field.TypeUUID},
{Name: "price", Type: field.TypeFloat64, SchemaType: map[string]string{dialect.Postgres: "numeric(5,2)"}},
},
},
},
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", false)
mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL, "name" varchar NULL, "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL, "uuid" uuid NOT NULL, PRIMARY KEY("id"))`)).
mock.ExpectExec(escape(`CREATE TABLE IF NOT EXISTS "users"("id" bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL, "name" varchar NULL, "age" bigint NOT NULL, "doc" jsonb NULL, "enums" varchar NOT NULL, "uuid" uuid NOT NULL, "price" numeric(5,2) NOT NULL, PRIMARY KEY("id"))`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},

View File

@@ -137,19 +137,20 @@ func (t *Table) fk(symbol string) (*ForeignKey, bool) {
// Column schema definition for SQL dialects.
type Column struct {
Name string // column name.
Type field.Type // column type.
typ string // row column type (used for Rows.Scan).
Attr string // extra attributes.
Size int64 // max size parameter for string, blob, etc.
Key string // key definition (PRI, UNI or MUL).
Unique bool // column with unique constraint.
Increment bool // auto increment attribute.
Nullable bool // null or not null attribute.
Default interface{} // default value.
Enums []string // enum values.
indexes Indexes // linked indexes.
foreign *ForeignKey // linked foreign-key.
Name string // column name.
Type field.Type // column type.
SchemaType map[string]string // optional schema type per dialect.
Attr string // extra attributes.
Size int64 // max size parameter for string, blob, etc.
Key string // key definition (PRI, UNI or MUL).
Unique bool // column with unique constraint.
Increment bool // auto increment attribute.
Nullable bool // null or not null attribute.
Default interface{} // default value.
Enums []string // enum values.
typ string // row column type (used for Rows.Scan).
indexes Indexes // linked indexes.
foreign *ForeignKey // linked foreign-key.
}
// UniqueKey returns boolean indicates if this column is a unique key.

View File

@@ -89,6 +89,9 @@ func (d *SQLite) tBuilder(t *Table) *sql.TableBuilder {
// cType returns the SQLite string type for the given column.
func (*SQLite) cType(c *Column) (t string) {
if c.SchemaType != nil && c.SchemaType[dialect.SQLite] != "" {
return c.SchemaType[dialect.SQLite]
}
switch c.Type {
case field.TypeBool:
t = "bool"

View File

@@ -10,6 +10,7 @@ import (
"math"
"testing"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/schema/field"
@@ -63,13 +64,14 @@ func TestSQLite_Create(t *testing.T) {
{Name: "age", Type: field.TypeInt},
{Name: "doc", Type: field.TypeJSON, Nullable: true},
{Name: "uuid", Type: field.TypeUUID, Nullable: true},
{Name: "decimal", Type: field.TypeFloat32, SchemaType: map[string]string{dialect.SQLite: "decimal(6,2)"}},
},
},
},
before: func(mock sqliteMock) {
mock.start()
mock.tableExists("users", false)
mock.ExpectExec(escape("CREATE TABLE `users`(`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `name` varchar(255) NULL, `age` integer NOT NULL, `doc` json NULL, `uuid` uuid NULL)")).
mock.ExpectExec(escape("CREATE TABLE `users`(`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `name` varchar(255) NULL, `age` integer NOT NULL, `doc` json NULL, `uuid` uuid NULL, `decimal` decimal(6,2) NOT NULL)")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},