ent/field: add update_default option time field

Reviewed By: alexsn

Differential Revision: D17070907

fbshipit-source-id: 63c9ce75c58e524044c38f9461cb04e8e45c8017
This commit is contained in:
Ariel Mashraki
2019-08-27 06:53:44 -07:00
committed by Facebook Github Bot
parent bd07c86b60
commit 772b8a33f8
30 changed files with 376 additions and 100 deletions

File diff suppressed because one or more lines are too long

View File

@@ -25,17 +25,18 @@ type Schema struct {
// Field represents an ent.Field that was loaded from a complied user package.
type Field struct {
Name string `json:"name,omitempty"`
Type field.Type `json:"type,omitempty"`
Tag string `json:"tag,omitempty"`
Size *int `json:"size,omitempty"`
Charset *string `json:"charset,omitempty"`
Unique bool `json:"unique,omitempty"`
Nillable bool `json:"nillable,omitempty"`
Optional bool `json:"optional,omitempty"`
Default bool `json:"default,omitempty"`
Immutable bool `json:"immutable,omitempty"`
Validators int `json:"validators,omitempty"`
Name string `json:"name,omitempty"`
Type field.Type `json:"type,omitempty"`
Tag string `json:"tag,omitempty"`
Size *int `json:"size,omitempty"`
Charset *string `json:"charset,omitempty"`
Unique bool `json:"unique,omitempty"`
Nillable bool `json:"nillable,omitempty"`
Optional bool `json:"optional,omitempty"`
Default bool `json:"default,omitempty"`
UpdateDefault bool `json:"update_default,omitempty"`
Immutable bool `json:"immutable,omitempty"`
Validators int `json:"validators,omitempty"`
}
// Edge represents an ent.Edge that was loaded from a complied user package.
@@ -85,15 +86,16 @@ func MarshalSchema(schema ent.Interface) (b []byte, err error) {
for _, f := range fields {
fd := f.Descriptor()
sf := &Field{
Name: fd.Name,
Type: fd.Type,
Tag: fd.Tag,
Unique: fd.Unique,
Default: fd.Default != nil,
Nillable: fd.Nillable,
Optional: fd.Optional,
Immutable: fd.Immutable,
Validators: len(fd.Validators),
Name: fd.Name,
Type: fd.Type,
Tag: fd.Tag,
Unique: fd.Unique,
Nillable: fd.Nillable,
Optional: fd.Optional,
Immutable: fd.Immutable,
Validators: len(fd.Validators),
Default: fd.Default != nil,
UpdateDefault: fd.UpdateDefault != nil,
}
if fd.Size != 0 {
sf.Size = &fd.Size

View File

@@ -8,6 +8,7 @@ import (
"encoding/json"
"math"
"testing"
"time"
"github.com/facebookincubator/ent"
"github.com/facebookincubator/ent/schema/edge"
@@ -138,6 +139,8 @@ func (WithDefaults) Fields() []ent.Field {
Default("foo"),
field.Bool("string").
Default(true),
field.Time("updated_at").
UpdateDefault(time.Now),
}
}
@@ -163,4 +166,6 @@ func TestMarshalDefaults(t *testing.T) {
require.True(t, schema.Fields[1].Default)
require.True(t, schema.Fields[2].Default)
require.True(t, schema.Fields[3].Default)
require.False(t, schema.Fields[4].Default)
require.True(t, schema.Fields[4].UpdateDefault)
}