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

@@ -242,7 +242,7 @@ func (b *stringBuilder) Immutable() *stringBuilder {
}
// Comment sets the comment of the field.
func (b *stringBuilder) Comment(c string) *stringBuilder {
func (b *stringBuilder) Comment(string) *stringBuilder {
return b
}
@@ -259,6 +259,20 @@ func (b *stringBuilder) StorageKey(key string) *stringBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for string.
//
// field.String("name").
// SchemaType(map[string]string{
// dialect.MySQL: "text",
// dialect.Postgres: "varchar",
// })
//
func (b *stringBuilder) SchemaType(types map[string]string) *stringBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *stringBuilder) Descriptor() *Descriptor {
return b.desc
@@ -335,6 +349,20 @@ func (b *timeBuilder) Descriptor() *Descriptor {
return b.desc
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for time.
//
// field.Time("created_at").
// SchemaType(map[string]string{
// dialect.MySQL: "datetime",
// dialect.Postgres: "time with time zone",
// })
//
func (b *timeBuilder) SchemaType(types map[string]string) *timeBuilder {
b.desc.SchemaType = types
return b
}
// boolBuilder is the builder for boolean fields.
type boolBuilder struct {
desc *Descriptor
@@ -451,6 +479,20 @@ func (b *bytesBuilder) Descriptor() *Descriptor {
return b.desc
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for bytes.
//
// field.Bytes("blob").
// SchemaType(map[string]string{
// dialect.MySQL: "tinyblob",
// dialect.SQLite: "tinyblob",
// })
//
func (b *bytesBuilder) SchemaType(types map[string]string) *bytesBuilder {
b.desc.SchemaType = types
return b
}
// jsonBuilder is the builder for json fields.
type jsonBuilder struct {
desc *Descriptor
@@ -487,6 +529,20 @@ func (b *jsonBuilder) StructTag(s string) *jsonBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for json.
//
// field.JSON("json").
// SchemaType(map[string]string{
// dialect.MySQL: "json",
// dialect.Postgres: "jsonb",
// })
//
func (b *jsonBuilder) SchemaType(types map[string]string) *jsonBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *jsonBuilder) Descriptor() *Descriptor {
return b.desc
@@ -547,6 +603,19 @@ func (b *enumBuilder) StructTag(s string) *enumBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for enum.
//
// field.Enum("enum").
// SchemaType(map[string]string{
// dialect.Postgres: "EnumType",
// })
//
func (b *enumBuilder) SchemaType(types map[string]string) *enumBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *enumBuilder) Descriptor() *Descriptor {
return b.desc
@@ -600,6 +669,19 @@ func (b *uuidBuilder) Default(fn interface{}) *uuidBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uuid.
//
// field.UUID("id", uuid.New()).
// SchemaType(map[string]string{
// dialect.Postgres: "CustomUUID",
// })
//
func (b *uuidBuilder) SchemaType(types map[string]string) *uuidBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uuidBuilder) Descriptor() *Descriptor {
return b.desc

View File

@@ -10,6 +10,7 @@ import (
"testing"
"time"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/schema/field"
"github.com/google/uuid"
@@ -17,24 +18,36 @@ import (
)
func TestInt(t *testing.T) {
f := field.Int("age").Positive()
fd := f.Descriptor()
fd := field.Int("age").
Positive().
Descriptor()
assert.Equal(t, "age", fd.Name)
assert.Equal(t, field.TypeInt, fd.Info.Type)
assert.Len(t, fd.Validators, 1)
f = field.Int("age").Default(10).Min(10).Max(20)
fd = f.Descriptor()
fd = field.Int("age").
Default(10).
Min(10).
Max(20).
Descriptor()
assert.NotNil(t, fd.Default)
assert.Equal(t, 10, fd.Default)
assert.Len(t, fd.Validators, 2)
f = field.Int("age").Range(20, 40).Nillable()
fd = f.Descriptor()
fd = field.Int("age").
Range(20, 40).
Nillable().
SchemaType(map[string]string{
dialect.SQLite: "numeric",
dialect.Postgres: "int_type",
}).
Descriptor()
assert.Nil(t, fd.Default)
assert.True(t, fd.Nillable)
assert.False(t, fd.Immutable)
assert.Len(t, fd.Validators, 1)
assert.Equal(t, "numeric", fd.SchemaType[dialect.SQLite])
assert.Equal(t, "int_type", fd.SchemaType[dialect.Postgres])
assert.Equal(t, field.TypeInt8, field.Int8("age").Descriptor().Info.Type)
assert.Equal(t, field.TypeInt16, field.Int16("age").Descriptor().Info.Type)

View File

@@ -9,7 +9,7 @@ import "errors"
//go:generate go run gen/gen.go
{{ range $_, $t := $.Ints }}
{{ range $t := $.Ints }}
{{ $title := title $t.String }}
// {{ $title }} returns a new Field with type {{ $t }}.
func {{ $title }}(name string) *{{ $t }}Builder { return &{{ $t }}Builder{&Descriptor{
@@ -147,6 +147,19 @@ func (b *{{ $builder }}) StorageKey(key string) *{{ $builder }} {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for {{ $t.String }}.
//
// field.{{ title $t.String }}("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *{{ $builder }}) SchemaType(types map[string]string) *{{ $builder }} {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
return b.desc
@@ -154,7 +167,7 @@ func (b *{{ $builder }}) Descriptor() *Descriptor {
{{ end }}
{{ range $_, $t := $.Floats }}
{{ range $t := $.Floats }}
{{ $builder := printf "%sBuilder" $t }}
// {{ $builder }} is the builder for float fields.
@@ -262,6 +275,20 @@ func (b *{{ $builder }}) StorageKey(key string) *{{ $builder }} {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for {{ $t.String }}.
//
// field.{{ title $t.String }}("amount").
// SchemaType(map[string]string{
// dialect.MySQL: "decimal(5, 2)",
// dialect.Postgres: "numeric(5, 2)",
// })
//
func (b *{{ $builder }}) SchemaType(types map[string]string) *{{ $builder }} {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
return b.desc

View File

@@ -213,6 +213,19 @@ func (b *intBuilder) StorageKey(key string) *intBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for int.
//
// field.Int("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *intBuilder) SchemaType(types map[string]string) *intBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *intBuilder) Descriptor() *Descriptor {
return b.desc
@@ -317,6 +330,19 @@ func (b *uintBuilder) StorageKey(key string) *uintBuilder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uint.
//
// field.Uint("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *uintBuilder) SchemaType(types map[string]string) *uintBuilder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uintBuilder) Descriptor() *Descriptor {
return b.desc
@@ -431,6 +457,19 @@ func (b *int8Builder) StorageKey(key string) *int8Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for int8.
//
// field.Int8("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *int8Builder) SchemaType(types map[string]string) *int8Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int8Builder) Descriptor() *Descriptor {
return b.desc
@@ -545,6 +584,19 @@ func (b *int16Builder) StorageKey(key string) *int16Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for int16.
//
// field.Int16("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *int16Builder) SchemaType(types map[string]string) *int16Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int16Builder) Descriptor() *Descriptor {
return b.desc
@@ -659,6 +711,19 @@ func (b *int32Builder) StorageKey(key string) *int32Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for int32.
//
// field.Int32("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *int32Builder) SchemaType(types map[string]string) *int32Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int32Builder) Descriptor() *Descriptor {
return b.desc
@@ -773,6 +838,19 @@ func (b *int64Builder) StorageKey(key string) *int64Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for int64.
//
// field.Int64("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *int64Builder) SchemaType(types map[string]string) *int64Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int64Builder) Descriptor() *Descriptor {
return b.desc
@@ -877,6 +955,19 @@ func (b *uint8Builder) StorageKey(key string) *uint8Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uint8.
//
// field.Uint8("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *uint8Builder) SchemaType(types map[string]string) *uint8Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint8Builder) Descriptor() *Descriptor {
return b.desc
@@ -981,6 +1072,19 @@ func (b *uint16Builder) StorageKey(key string) *uint16Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uint16.
//
// field.Uint16("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *uint16Builder) SchemaType(types map[string]string) *uint16Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint16Builder) Descriptor() *Descriptor {
return b.desc
@@ -1085,6 +1189,19 @@ func (b *uint32Builder) StorageKey(key string) *uint32Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uint32.
//
// field.Uint32("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *uint32Builder) SchemaType(types map[string]string) *uint32Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint32Builder) Descriptor() *Descriptor {
return b.desc
@@ -1189,6 +1306,19 @@ func (b *uint64Builder) StorageKey(key string) *uint64Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for uint64.
//
// field.Uint64("oid").
// SchemaType(map[string]string{
// dialect.Postgres: "CustomType",
// })
//
func (b *uint64Builder) SchemaType(types map[string]string) *uint64Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1298,6 +1428,20 @@ func (b *float64Builder) StorageKey(key string) *float64Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for float64.
//
// field.Float64("amount").
// SchemaType(map[string]string{
// dialect.MySQL: "decimal(5, 2)",
// dialect.Postgres: "numeric(5, 2)",
// })
//
func (b *float64Builder) SchemaType(types map[string]string) *float64Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1407,6 +1551,20 @@ func (b *float32Builder) StorageKey(key string) *float32Builder {
return b
}
// SchemaType overrides the default database type with a custom
// schema type (per dialect) for float32.
//
// field.Float32("amount").
// SchemaType(map[string]string{
// dialect.MySQL: "decimal(5, 2)",
// dialect.Postgres: "numeric(5, 2)",
// })
//
func (b *float32Builder) SchemaType(types map[string]string) *float32Builder {
b.desc.SchemaType = types
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float32Builder) Descriptor() *Descriptor {
return b.desc