mirror of
https://github.com/ent/ent.git
synced 2026-03-05 19:35:23 +03:00
examples/migration: add column with default value (#3960)
This commit is contained in:
@@ -22,6 +22,8 @@ type Card struct {
|
||||
config `json:"-"`
|
||||
// ID of the ent.
|
||||
ID int `json:"id,omitempty"`
|
||||
// Type holds the value of the "type" field.
|
||||
Type string `json:"type,omitempty"`
|
||||
// NumberHash holds the value of the "number_hash" field.
|
||||
NumberHash string `json:"number_hash,omitempty"`
|
||||
// CvvHash holds the value of the "cvv_hash" field.
|
||||
@@ -74,7 +76,7 @@ func (*Card) scanValues(columns []string) ([]any, error) {
|
||||
switch columns[i] {
|
||||
case card.FieldID, card.FieldOwnerID:
|
||||
values[i] = new(sql.NullInt64)
|
||||
case card.FieldNumberHash, card.FieldCvvHash:
|
||||
case card.FieldType, card.FieldNumberHash, card.FieldCvvHash:
|
||||
values[i] = new(sql.NullString)
|
||||
case card.FieldExpiresAt:
|
||||
values[i] = new(sql.NullTime)
|
||||
@@ -99,6 +101,12 @@ func (c *Card) assignValues(columns []string, values []any) error {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
c.ID = int(value.Int64)
|
||||
case card.FieldType:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field type", values[i])
|
||||
} else if value.Valid {
|
||||
c.Type = value.String
|
||||
}
|
||||
case card.FieldNumberHash:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field number_hash", values[i])
|
||||
@@ -169,6 +177,9 @@ func (c *Card) String() string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString("Card(")
|
||||
builder.WriteString(fmt.Sprintf("id=%v, ", c.ID))
|
||||
builder.WriteString("type=")
|
||||
builder.WriteString(c.Type)
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("number_hash=")
|
||||
builder.WriteString(c.NumberHash)
|
||||
builder.WriteString(", ")
|
||||
|
||||
@@ -16,6 +16,8 @@ const (
|
||||
Label = "card"
|
||||
// FieldID holds the string denoting the id field in the database.
|
||||
FieldID = "id"
|
||||
// FieldType holds the string denoting the type field in the database.
|
||||
FieldType = "type"
|
||||
// FieldNumberHash holds the string denoting the number_hash field in the database.
|
||||
FieldNumberHash = "number_hash"
|
||||
// FieldCvvHash holds the string denoting the cvv_hash field in the database.
|
||||
@@ -49,6 +51,7 @@ const (
|
||||
// Columns holds all SQL columns for card fields.
|
||||
var Columns = []string{
|
||||
FieldID,
|
||||
FieldType,
|
||||
FieldNumberHash,
|
||||
FieldCvvHash,
|
||||
FieldExpiresAt,
|
||||
@@ -66,6 +69,8 @@ func ValidColumn(column string) bool {
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultType holds the default value on creation for the "type" field.
|
||||
DefaultType string
|
||||
// DefaultOwnerID holds the default value on creation for the "owner_id" field.
|
||||
DefaultOwnerID int
|
||||
)
|
||||
@@ -78,6 +83,11 @@ func ByID(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldID, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByType orders the results by the type field.
|
||||
func ByType(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldType, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByNumberHash orders the results by the number_hash field.
|
||||
func ByNumberHash(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldNumberHash, opts...).ToFunc()
|
||||
|
||||
@@ -59,6 +59,11 @@ func IDLTE(id int) predicate.Card {
|
||||
return predicate.Card(sql.FieldLTE(FieldID, id))
|
||||
}
|
||||
|
||||
// Type applies equality check predicate on the "type" field. It's identical to TypeEQ.
|
||||
func Type(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldEQ(FieldType, v))
|
||||
}
|
||||
|
||||
// NumberHash applies equality check predicate on the "number_hash" field. It's identical to NumberHashEQ.
|
||||
func NumberHash(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldEQ(FieldNumberHash, v))
|
||||
@@ -79,6 +84,71 @@ func OwnerID(v int) predicate.Card {
|
||||
return predicate.Card(sql.FieldEQ(FieldOwnerID, v))
|
||||
}
|
||||
|
||||
// TypeEQ applies the EQ predicate on the "type" field.
|
||||
func TypeEQ(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldEQ(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeNEQ applies the NEQ predicate on the "type" field.
|
||||
func TypeNEQ(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldNEQ(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeIn applies the In predicate on the "type" field.
|
||||
func TypeIn(vs ...string) predicate.Card {
|
||||
return predicate.Card(sql.FieldIn(FieldType, vs...))
|
||||
}
|
||||
|
||||
// TypeNotIn applies the NotIn predicate on the "type" field.
|
||||
func TypeNotIn(vs ...string) predicate.Card {
|
||||
return predicate.Card(sql.FieldNotIn(FieldType, vs...))
|
||||
}
|
||||
|
||||
// TypeGT applies the GT predicate on the "type" field.
|
||||
func TypeGT(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldGT(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeGTE applies the GTE predicate on the "type" field.
|
||||
func TypeGTE(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldGTE(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeLT applies the LT predicate on the "type" field.
|
||||
func TypeLT(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldLT(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeLTE applies the LTE predicate on the "type" field.
|
||||
func TypeLTE(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldLTE(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeContains applies the Contains predicate on the "type" field.
|
||||
func TypeContains(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldContains(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeHasPrefix applies the HasPrefix predicate on the "type" field.
|
||||
func TypeHasPrefix(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldHasPrefix(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeHasSuffix applies the HasSuffix predicate on the "type" field.
|
||||
func TypeHasSuffix(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldHasSuffix(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeEqualFold applies the EqualFold predicate on the "type" field.
|
||||
func TypeEqualFold(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldEqualFold(FieldType, v))
|
||||
}
|
||||
|
||||
// TypeContainsFold applies the ContainsFold predicate on the "type" field.
|
||||
func TypeContainsFold(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldContainsFold(FieldType, v))
|
||||
}
|
||||
|
||||
// NumberHashEQ applies the EQ predicate on the "number_hash" field.
|
||||
func NumberHashEQ(v string) predicate.Card {
|
||||
return predicate.Card(sql.FieldEQ(FieldNumberHash, v))
|
||||
|
||||
@@ -26,6 +26,20 @@ type CardCreate struct {
|
||||
hooks []Hook
|
||||
}
|
||||
|
||||
// SetType sets the "type" field.
|
||||
func (cc *CardCreate) SetType(s string) *CardCreate {
|
||||
cc.mutation.SetType(s)
|
||||
return cc
|
||||
}
|
||||
|
||||
// SetNillableType sets the "type" field if the given value is not nil.
|
||||
func (cc *CardCreate) SetNillableType(s *string) *CardCreate {
|
||||
if s != nil {
|
||||
cc.SetType(*s)
|
||||
}
|
||||
return cc
|
||||
}
|
||||
|
||||
// SetNumberHash sets the "number_hash" field.
|
||||
func (cc *CardCreate) SetNumberHash(s string) *CardCreate {
|
||||
cc.mutation.SetNumberHash(s)
|
||||
@@ -121,6 +135,10 @@ func (cc *CardCreate) ExecX(ctx context.Context) {
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (cc *CardCreate) defaults() {
|
||||
if _, ok := cc.mutation.GetType(); !ok {
|
||||
v := card.DefaultType
|
||||
cc.mutation.SetType(v)
|
||||
}
|
||||
if _, ok := cc.mutation.OwnerID(); !ok {
|
||||
v := card.DefaultOwnerID
|
||||
cc.mutation.SetOwnerID(v)
|
||||
@@ -129,6 +147,9 @@ func (cc *CardCreate) defaults() {
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (cc *CardCreate) check() error {
|
||||
if _, ok := cc.mutation.GetType(); !ok {
|
||||
return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Card.type"`)}
|
||||
}
|
||||
if _, ok := cc.mutation.NumberHash(); !ok {
|
||||
return &ValidationError{Name: "number_hash", err: errors.New(`ent: missing required field "Card.number_hash"`)}
|
||||
}
|
||||
@@ -167,6 +188,10 @@ func (cc *CardCreate) createSpec() (*Card, *sqlgraph.CreateSpec) {
|
||||
_node = &Card{config: cc.config}
|
||||
_spec = sqlgraph.NewCreateSpec(card.Table, sqlgraph.NewFieldSpec(card.FieldID, field.TypeInt))
|
||||
)
|
||||
if value, ok := cc.mutation.GetType(); ok {
|
||||
_spec.SetField(card.FieldType, field.TypeString, value)
|
||||
_node.Type = value
|
||||
}
|
||||
if value, ok := cc.mutation.NumberHash(); ok {
|
||||
_spec.SetField(card.FieldNumberHash, field.TypeString, value)
|
||||
_node.NumberHash = value
|
||||
|
||||
@@ -338,12 +338,12 @@ func (cq *CardQuery) WithPayments(opts ...func(*PaymentQuery)) *CardQuery {
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// NumberHash string `json:"number_hash,omitempty"`
|
||||
// Type string `json:"type,omitempty"`
|
||||
// Count int `json:"count,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.Card.Query().
|
||||
// GroupBy(card.FieldNumberHash).
|
||||
// GroupBy(card.FieldType).
|
||||
// Aggregate(ent.Count()).
|
||||
// Scan(ctx, &v)
|
||||
func (cq *CardQuery) GroupBy(field string, fields ...string) *CardGroupBy {
|
||||
@@ -361,11 +361,11 @@ func (cq *CardQuery) GroupBy(field string, fields ...string) *CardGroupBy {
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// NumberHash string `json:"number_hash,omitempty"`
|
||||
// Type string `json:"type,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.Card.Query().
|
||||
// Select(card.FieldNumberHash).
|
||||
// Select(card.FieldType).
|
||||
// Scan(ctx, &v)
|
||||
func (cq *CardQuery) Select(fields ...string) *CardSelect {
|
||||
cq.ctx.Fields = append(cq.ctx.Fields, fields...)
|
||||
|
||||
@@ -34,6 +34,20 @@ func (cu *CardUpdate) Where(ps ...predicate.Card) *CardUpdate {
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetType sets the "type" field.
|
||||
func (cu *CardUpdate) SetType(s string) *CardUpdate {
|
||||
cu.mutation.SetType(s)
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetNillableType sets the "type" field if the given value is not nil.
|
||||
func (cu *CardUpdate) SetNillableType(s *string) *CardUpdate {
|
||||
if s != nil {
|
||||
cu.SetType(*s)
|
||||
}
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetNumberHash sets the "number_hash" field.
|
||||
func (cu *CardUpdate) SetNumberHash(s string) *CardUpdate {
|
||||
cu.mutation.SetNumberHash(s)
|
||||
@@ -195,6 +209,9 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := cu.mutation.GetType(); ok {
|
||||
_spec.SetField(card.FieldType, field.TypeString, value)
|
||||
}
|
||||
if value, ok := cu.mutation.NumberHash(); ok {
|
||||
_spec.SetField(card.FieldNumberHash, field.TypeString, value)
|
||||
}
|
||||
@@ -301,6 +318,20 @@ type CardUpdateOne struct {
|
||||
mutation *CardMutation
|
||||
}
|
||||
|
||||
// SetType sets the "type" field.
|
||||
func (cuo *CardUpdateOne) SetType(s string) *CardUpdateOne {
|
||||
cuo.mutation.SetType(s)
|
||||
return cuo
|
||||
}
|
||||
|
||||
// SetNillableType sets the "type" field if the given value is not nil.
|
||||
func (cuo *CardUpdateOne) SetNillableType(s *string) *CardUpdateOne {
|
||||
if s != nil {
|
||||
cuo.SetType(*s)
|
||||
}
|
||||
return cuo
|
||||
}
|
||||
|
||||
// SetNumberHash sets the "number_hash" field.
|
||||
func (cuo *CardUpdateOne) SetNumberHash(s string) *CardUpdateOne {
|
||||
cuo.mutation.SetNumberHash(s)
|
||||
@@ -492,6 +523,9 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := cuo.mutation.GetType(); ok {
|
||||
_spec.SetField(card.FieldType, field.TypeString, value)
|
||||
}
|
||||
if value, ok := cuo.mutation.NumberHash(); ok {
|
||||
_spec.SetField(card.FieldNumberHash, field.TypeString, value)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Modify "cards" table
|
||||
ALTER TABLE `cards` ADD COLUMN `type` varchar(255) NOT NULL DEFAULT "unknown";
|
||||
@@ -1,4 +1,4 @@
|
||||
h1:oAG06Nq+huZ4PRykXyrDFIR7Xc1AJ9G1g24ctJ2uY+A=
|
||||
h1:vhKIBaVqxvUHieIcRzt0ZNPm4Bw8GLBZVObuChJfSWk=
|
||||
20221114082343_create_users.sql h1:EeOPKbOeYUHO830P1MCXKUsxT3wLk/hSgcpT9GzlGZQ=
|
||||
20221114090322_add_age.sql h1:weRCTqS5cqKyrvIjGXk7Bn24YUXo0nhLNWD0EZBcaoQ=
|
||||
20221114101516_add_name.sql h1:leQgI2JN3URZyujlNzX3gI53MSiTA02MKI/G9cnMu6I=
|
||||
@@ -11,3 +11,4 @@ h1:oAG06Nq+huZ4PRykXyrDFIR7Xc1AJ9G1g24ctJ2uY+A=
|
||||
20231231102849_add_currency.sql h1:Dzm4fDibgEV841whj7Y1TMeat6Qt5d7kQojBXNIh2nY=
|
||||
20231231120006_add_session.sql h1:n725mYZBuwUU2Nl8XdUFDgn2Dbm2kpVwAASEtWOqQuo=
|
||||
20231231123114_hash_card.sql h1:ZsZUe2+vJ6echLKnQxBhXyVoBJja2IAY7rX/P5UZ0iE=
|
||||
20240229200634_add_card_type.sql h1:y6kam4J45wzdgUh7eU9TTgbs8GwKuqNWyfqaFEtNgbY=
|
||||
|
||||
@@ -16,6 +16,7 @@ var (
|
||||
// CardsColumns holds the columns for the "cards" table.
|
||||
CardsColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "type", Type: field.TypeString, Default: "unknown"},
|
||||
{Name: "number_hash", Type: field.TypeString},
|
||||
{Name: "cvv_hash", Type: field.TypeString},
|
||||
{Name: "expires_at", Type: field.TypeTime, Nullable: true},
|
||||
@@ -29,7 +30,7 @@ var (
|
||||
ForeignKeys: []*schema.ForeignKey{
|
||||
{
|
||||
Symbol: "cards_users_cards",
|
||||
Columns: []*schema.Column{CardsColumns[4]},
|
||||
Columns: []*schema.Column{CardsColumns[5]},
|
||||
RefColumns: []*schema.Column{UsersColumns[0]},
|
||||
OnDelete: schema.NoAction,
|
||||
},
|
||||
|
||||
@@ -48,6 +48,7 @@ type CardMutation struct {
|
||||
op Op
|
||||
typ string
|
||||
id *int
|
||||
_type *string
|
||||
number_hash *string
|
||||
cvv_hash *string
|
||||
expires_at *time.Time
|
||||
@@ -160,6 +161,42 @@ func (m *CardMutation) IDs(ctx context.Context) ([]int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// SetType sets the "type" field.
|
||||
func (m *CardMutation) SetType(s string) {
|
||||
m._type = &s
|
||||
}
|
||||
|
||||
// GetType returns the value of the "type" field in the mutation.
|
||||
func (m *CardMutation) GetType() (r string, exists bool) {
|
||||
v := m._type
|
||||
if v == nil {
|
||||
return
|
||||
}
|
||||
return *v, true
|
||||
}
|
||||
|
||||
// OldType returns the old "type" field's value of the Card entity.
|
||||
// If the Card object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *CardMutation) OldType(ctx context.Context) (v string, err error) {
|
||||
if !m.op.Is(OpUpdateOne) {
|
||||
return v, errors.New("OldType is only allowed on UpdateOne operations")
|
||||
}
|
||||
if m.id == nil || m.oldValue == nil {
|
||||
return v, errors.New("OldType requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldType: %w", err)
|
||||
}
|
||||
return oldValue.Type, nil
|
||||
}
|
||||
|
||||
// ResetType resets all changes to the "type" field.
|
||||
func (m *CardMutation) ResetType() {
|
||||
m._type = nil
|
||||
}
|
||||
|
||||
// SetNumberHash sets the "number_hash" field.
|
||||
func (m *CardMutation) SetNumberHash(s string) {
|
||||
m.number_hash = &s
|
||||
@@ -432,7 +469,10 @@ func (m *CardMutation) Type() string {
|
||||
// order to get all numeric fields that were incremented/decremented, call
|
||||
// AddedFields().
|
||||
func (m *CardMutation) Fields() []string {
|
||||
fields := make([]string, 0, 4)
|
||||
fields := make([]string, 0, 5)
|
||||
if m._type != nil {
|
||||
fields = append(fields, card.FieldType)
|
||||
}
|
||||
if m.number_hash != nil {
|
||||
fields = append(fields, card.FieldNumberHash)
|
||||
}
|
||||
@@ -453,6 +493,8 @@ func (m *CardMutation) Fields() []string {
|
||||
// schema.
|
||||
func (m *CardMutation) Field(name string) (ent.Value, bool) {
|
||||
switch name {
|
||||
case card.FieldType:
|
||||
return m.GetType()
|
||||
case card.FieldNumberHash:
|
||||
return m.NumberHash()
|
||||
case card.FieldCvvHash:
|
||||
@@ -470,6 +512,8 @@ func (m *CardMutation) Field(name string) (ent.Value, bool) {
|
||||
// database failed.
|
||||
func (m *CardMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
||||
switch name {
|
||||
case card.FieldType:
|
||||
return m.OldType(ctx)
|
||||
case card.FieldNumberHash:
|
||||
return m.OldNumberHash(ctx)
|
||||
case card.FieldCvvHash:
|
||||
@@ -487,6 +531,13 @@ func (m *CardMutation) OldField(ctx context.Context, name string) (ent.Value, er
|
||||
// type.
|
||||
func (m *CardMutation) SetField(name string, value ent.Value) error {
|
||||
switch name {
|
||||
case card.FieldType:
|
||||
v, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field %s", value, name)
|
||||
}
|
||||
m.SetType(v)
|
||||
return nil
|
||||
case card.FieldNumberHash:
|
||||
v, ok := value.(string)
|
||||
if !ok {
|
||||
@@ -576,6 +627,9 @@ func (m *CardMutation) ClearField(name string) error {
|
||||
// It returns an error if the field is not defined in the schema.
|
||||
func (m *CardMutation) ResetField(name string) error {
|
||||
switch name {
|
||||
case card.FieldType:
|
||||
m.ResetType()
|
||||
return nil
|
||||
case card.FieldNumberHash:
|
||||
m.ResetNumberHash()
|
||||
return nil
|
||||
|
||||
@@ -22,8 +22,12 @@ import (
|
||||
func init() {
|
||||
cardFields := schema.Card{}.Fields()
|
||||
_ = cardFields
|
||||
// cardDescType is the schema descriptor for type field.
|
||||
cardDescType := cardFields[0].Descriptor()
|
||||
// card.DefaultType holds the default value on creation for the type field.
|
||||
card.DefaultType = cardDescType.Default.(string)
|
||||
// cardDescOwnerID is the schema descriptor for owner_id field.
|
||||
cardDescOwnerID := cardFields[3].Descriptor()
|
||||
cardDescOwnerID := cardFields[4].Descriptor()
|
||||
// card.DefaultOwnerID holds the default value on creation for the owner_id field.
|
||||
card.DefaultOwnerID = cardDescOwnerID.Default.(int)
|
||||
paymentFields := schema.Payment{}.Fields()
|
||||
|
||||
@@ -20,6 +20,8 @@ type Card struct {
|
||||
// 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").
|
||||
|
||||
Reference in New Issue
Block a user