* implement highAvailability feature --------- Co-authored-by: Marco Mariani <marco@crowdsec.net>
216 lines
5.5 KiB
Go
216 lines
5.5 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/lock"
|
|
)
|
|
|
|
// LockCreate is the builder for creating a Lock entity.
|
|
type LockCreate struct {
|
|
config
|
|
mutation *LockMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (lc *LockCreate) SetName(s string) *LockCreate {
|
|
lc.mutation.SetName(s)
|
|
return lc
|
|
}
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
func (lc *LockCreate) SetCreatedAt(t time.Time) *LockCreate {
|
|
lc.mutation.SetCreatedAt(t)
|
|
return lc
|
|
}
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
func (lc *LockCreate) SetNillableCreatedAt(t *time.Time) *LockCreate {
|
|
if t != nil {
|
|
lc.SetCreatedAt(*t)
|
|
}
|
|
return lc
|
|
}
|
|
|
|
// Mutation returns the LockMutation object of the builder.
|
|
func (lc *LockCreate) Mutation() *LockMutation {
|
|
return lc.mutation
|
|
}
|
|
|
|
// Save creates the Lock in the database.
|
|
func (lc *LockCreate) Save(ctx context.Context) (*Lock, error) {
|
|
lc.defaults()
|
|
return withHooks(ctx, lc.sqlSave, lc.mutation, lc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (lc *LockCreate) SaveX(ctx context.Context) *Lock {
|
|
v, err := lc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (lc *LockCreate) Exec(ctx context.Context) error {
|
|
_, err := lc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (lc *LockCreate) ExecX(ctx context.Context) {
|
|
if err := lc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (lc *LockCreate) defaults() {
|
|
if _, ok := lc.mutation.CreatedAt(); !ok {
|
|
v := lock.DefaultCreatedAt()
|
|
lc.mutation.SetCreatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (lc *LockCreate) check() error {
|
|
if _, ok := lc.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Lock.name"`)}
|
|
}
|
|
if _, ok := lc.mutation.CreatedAt(); !ok {
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Lock.created_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (lc *LockCreate) sqlSave(ctx context.Context) (*Lock, error) {
|
|
if err := lc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := lc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
lc.mutation.id = &_node.ID
|
|
lc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (lc *LockCreate) createSpec() (*Lock, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Lock{config: lc.config}
|
|
_spec = sqlgraph.NewCreateSpec(lock.Table, sqlgraph.NewFieldSpec(lock.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := lc.mutation.Name(); ok {
|
|
_spec.SetField(lock.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if value, ok := lc.mutation.CreatedAt(); ok {
|
|
_spec.SetField(lock.FieldCreatedAt, field.TypeTime, value)
|
|
_node.CreatedAt = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// LockCreateBulk is the builder for creating many Lock entities in bulk.
|
|
type LockCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*LockCreate
|
|
}
|
|
|
|
// Save creates the Lock entities in the database.
|
|
func (lcb *LockCreateBulk) Save(ctx context.Context) ([]*Lock, error) {
|
|
if lcb.err != nil {
|
|
return nil, lcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(lcb.builders))
|
|
nodes := make([]*Lock, len(lcb.builders))
|
|
mutators := make([]Mutator, len(lcb.builders))
|
|
for i := range lcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := lcb.builders[i]
|
|
builder.defaults()
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*LockMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err := builder.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
builder.mutation = mutation
|
|
var err error
|
|
nodes[i], specs[i] = builder.createSpec()
|
|
if i < len(mutators)-1 {
|
|
_, err = mutators[i+1].Mutate(root, lcb.builders[i+1].mutation)
|
|
} else {
|
|
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
|
if err = sqlgraph.BatchCreate(ctx, lcb.driver, spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mutation.id = &nodes[i].ID
|
|
if specs[i].ID.Value != nil {
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int(id)
|
|
}
|
|
mutation.done = true
|
|
return nodes[i], nil
|
|
})
|
|
for i := len(builder.hooks) - 1; i >= 0; i-- {
|
|
mut = builder.hooks[i](mut)
|
|
}
|
|
mutators[i] = mut
|
|
}(i, ctx)
|
|
}
|
|
if len(mutators) > 0 {
|
|
if _, err := mutators[0].Mutate(ctx, lcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (lcb *LockCreateBulk) SaveX(ctx context.Context) []*Lock {
|
|
v, err := lcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (lcb *LockCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := lcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (lcb *LockCreateBulk) ExecX(ctx context.Context) {
|
|
if err := lcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|