1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/index"
"github.com/facebook/ent/schema/mixin"
)
type Mixin struct {
mixin.Schema
}
func (m Mixin) Fields() []ent.Field {
return []ent.Field{
field.String("mixed_string").
Default("default"),
field.Enum("mixed_enum").
Values("on", "off").
Default("on"),
}
}
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
Mixin{},
}
}
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
StorageKey("oid"),
// changing the type of the field.
field.Int("age"),
// extending name field to longtext.
field.Text("name"),
// changing nickname from unique no non-unique.
field.String("nickname").
MaxLen(255),
// adding new columns (must be either optional, or with a default value).
field.String("phone").
Default("unknown"),
field.Bytes("buffer").
Optional().
DefaultFunc(func() []byte { return []byte("null") }),
// adding new column with supported default value
// in the database side, will append this value to
// all existing rows.
field.String("title").
Default("SWE"),
// change column name and reference it to the
// previous one ("renamed").
field.String("new_name").
Optional().
StorageKey("renamed"),
// extending the blob size.
field.Bytes("blob").
Optional().
MaxLen(1000),
// adding enum to the `state` column.
field.Enum("state").
Optional().
Values("logged_in", "logged_out", "online"),
// convert string to enum.
field.Enum("status").
Optional().
Values("done", "pending"),
// remove the max-length constraint from varchar.
field.String("workplace").
Optional(),
// deleting the `address` column.
}
}
func (User) Edges() []ent.Edge {
return []ent.Edge{
// Edge(children<-M2O->parent) to be dropped.
// Edge(spouse<-O2O->spouse) to be dropped.
edge.To("car", Car.Type),
// New edges to added.
edge.To("pets", Pet.Type).
StorageKey(edge.Column("owner_id")).
Unique(),
edge.To("friends", User.Type).
StorageKey(edge.Table("friends"), edge.Columns("user", "friend")),
}
}
func (User) Indexes() []ent.Index {
return []ent.Index{
// deleting old indexes (name, address),
// and defining a new one.
index.Fields("phone", "age").
Unique(),
}
}
type Car struct {
ent.Schema
}
func (Car) Edges() []ent.Edge {
return []ent.Edge{
// Car now can have more than 1 owner (not unique anymore).
edge.From("owner", User.Type).
Ref("car").
Unique(),
}
}
// Group schema.
type Group struct{ ent.Schema }
// Pet schema.
type Pet struct {
ent.Schema
}
func (Pet) Edges() []ent.Edge {
return []ent.Edge{
edge.From("owner", User.Type).
Ref("pets").
Unique(),
}
}
|