snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
55
examples/migration/ent/schema/card.go
Normal file
55
examples/migration/ent/schema/card.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// Card holds the schema definition for the Card entity.
|
||||
type Card struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Card.
|
||||
func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("type").
|
||||
Default("unknown"),
|
||||
field.String("number_hash"),
|
||||
field.String("cvv_hash"),
|
||||
field.Time("expires_at").
|
||||
Optional(),
|
||||
field.Int("owner_id").
|
||||
Default(0),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Card.
|
||||
func (Card) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Ref("cards").
|
||||
Unique().
|
||||
Required().
|
||||
Field("owner_id"),
|
||||
edge.To("payments", Payment.Type),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the Card.
|
||||
func (Card) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
// Named check constraints are compared by their name.
|
||||
// Thus, the definition does not need to be normalized.
|
||||
entsql.Checks(map[string]string{
|
||||
"number_hash_length": "(LENGTH(`number_hash`) = 16)",
|
||||
}),
|
||||
}
|
||||
}
|
||||
63
examples/migration/ent/schema/payment.go
Normal file
63
examples/migration/ent/schema/payment.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// Payment holds the schema definition for the Payment entity.
|
||||
type Payment struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Payment.
|
||||
func (Payment) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("card_id"),
|
||||
field.Float("amount").
|
||||
Positive(),
|
||||
field.Enum("currency").
|
||||
Values("USD", "EUR", "VND", "ILS"),
|
||||
field.Time("time"),
|
||||
field.String("description"),
|
||||
field.Enum("status").
|
||||
Values("pending", "completed", "failed"),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Payment.
|
||||
func (Payment) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("card", Card.Type).
|
||||
Ref("payments").
|
||||
Unique().
|
||||
Required().
|
||||
Field("card_id"),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Payment.
|
||||
func (Payment) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("status", "time"),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the Payment.
|
||||
func (Payment) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
// Named check constraints are compared by their name.
|
||||
// Thus, the definition does not need to be normalized.
|
||||
entsql.Checks(map[string]string{
|
||||
"amount_positive": "(`amount` > 0)",
|
||||
}),
|
||||
}
|
||||
}
|
||||
58
examples/migration/ent/schema/pet.go
Normal file
58
examples/migration/ent/schema/pet.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Pet holds the schema definition for the Pet entity.
|
||||
type Pet struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Pet.
|
||||
func (Pet) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.UUID("id", uuid.Nil).
|
||||
Default(uuid.New),
|
||||
field.String("name"),
|
||||
field.Float("age"),
|
||||
field.Float("weight"),
|
||||
field.UUID("best_friend_id", uuid.Nil).
|
||||
Annotations(
|
||||
entsql.Default(uuid.Nil.String()),
|
||||
),
|
||||
field.Int("owner_id").
|
||||
Default(0),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Pet.
|
||||
func (Pet) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("best_friend", Pet.Type).
|
||||
Unique().
|
||||
Required().
|
||||
Field("best_friend_id"),
|
||||
edge.To("owner", User.Type).
|
||||
Unique().
|
||||
Required().
|
||||
Field("owner_id"),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Pet.
|
||||
func (Pet) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("name", "owner_id").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
100
examples/migration/ent/schema/session.go
Normal file
100
examples/migration/ent/schema/session.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Session holds the schema definition for the Session entity.
|
||||
type Session struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Session.
|
||||
func (Session) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.UUID("id", uuid.Nil).
|
||||
Default(uuid.New),
|
||||
field.Bool("active").
|
||||
Default(false),
|
||||
field.Time("issued_at"),
|
||||
field.Time("expires_at").
|
||||
Optional(),
|
||||
field.String("token").
|
||||
Optional(),
|
||||
field.JSON("method", map[string]any{}).
|
||||
Optional(),
|
||||
field.UUID("device_id", uuid.Nil).
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Session.
|
||||
func (Session) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("device", SessionDevice.Type).
|
||||
Ref("sessions").
|
||||
Unique().
|
||||
Field("device_id"),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Session.
|
||||
func (Session) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("active", "expires_at"),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the Session.
|
||||
func (Session) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
// Named check constraints are compared by their name.
|
||||
// Thus, the definition does not need to be normalized.
|
||||
entsql.Checks(map[string]string{
|
||||
"token_length": "(LENGTH(`token`) = 64)",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// SessionDevice holds the schema definition for the SessionDevice entity.
|
||||
type SessionDevice struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the SessionDevice.
|
||||
func (SessionDevice) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.UUID("id", uuid.Nil).
|
||||
Default(uuid.New),
|
||||
field.String("ip_address").
|
||||
MaxLen(50),
|
||||
field.String("user_agent").
|
||||
MaxLen(512),
|
||||
field.String("location").
|
||||
MaxLen(512),
|
||||
field.Time("created_at"),
|
||||
field.Time("updated_at").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the SessionDevice.
|
||||
func (SessionDevice) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("sessions", Session.Type),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the SessionDevice.
|
||||
func (SessionDevice) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("ip_address", "user_agent"),
|
||||
index.Fields("location", "updated_at"),
|
||||
}
|
||||
}
|
||||
52
examples/migration/ent/schema/user.go
Normal file
52
examples/migration/ent/schema/user.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Float("age"),
|
||||
field.String("first_name"),
|
||||
field.String("last_name"),
|
||||
field.Strings("tags").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the User.
|
||||
func (User) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
// In case schema.ModeInspect is used without a dev-database, unnamed check constraints
|
||||
// should be normalized (i.e. identical to their definition in the database). In this
|
||||
// case, it is entsql.Check("(`age` > 0)"). See: https://atlasgo.io/concepts/dev-database.
|
||||
entsql.Check("age > 0"),
|
||||
|
||||
// Named check constraints are compared by their name.
|
||||
// Thus, the definition does not need to be normalized.
|
||||
entsql.Checks(map[string]string{
|
||||
"first_last_not_empty": "(`first_name` <> '' AND `last_name` <> '')",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the User.
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("cards", Card.Type),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user