Files
MonkeyCode/backend/db/teamgrouphost_create.go
yokowu c30c73aa93 feat: 添加 Host/VM ent schema 和数据库迁移
- ent/schema: host, virtualmachine, teamhost, teamgrouphost
- 修改 user/team/teamgroup/model schema 添加 host/vm edges
- migration/000003: hosts, virtualmachines, team_hosts, team_group_hosts 建表
2026-03-17 14:38:26 +08:00

660 lines
20 KiB
Go

// Code generated by ent, DO NOT EDIT.
package db
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/chaitin/MonkeyCode/backend/db/host"
"github.com/chaitin/MonkeyCode/backend/db/teamgroup"
"github.com/chaitin/MonkeyCode/backend/db/teamgrouphost"
"github.com/google/uuid"
)
// TeamGroupHostCreate is the builder for creating a TeamGroupHost entity.
type TeamGroupHostCreate struct {
config
mutation *TeamGroupHostMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetGroupID sets the "group_id" field.
func (_c *TeamGroupHostCreate) SetGroupID(v uuid.UUID) *TeamGroupHostCreate {
_c.mutation.SetGroupID(v)
return _c
}
// SetHostID sets the "host_id" field.
func (_c *TeamGroupHostCreate) SetHostID(v string) *TeamGroupHostCreate {
_c.mutation.SetHostID(v)
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *TeamGroupHostCreate) SetCreatedAt(v time.Time) *TeamGroupHostCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *TeamGroupHostCreate) SetNillableCreatedAt(v *time.Time) *TeamGroupHostCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *TeamGroupHostCreate) SetID(v uuid.UUID) *TeamGroupHostCreate {
_c.mutation.SetID(v)
return _c
}
// SetGroup sets the "group" edge to the TeamGroup entity.
func (_c *TeamGroupHostCreate) SetGroup(v *TeamGroup) *TeamGroupHostCreate {
return _c.SetGroupID(v.ID)
}
// SetHost sets the "host" edge to the Host entity.
func (_c *TeamGroupHostCreate) SetHost(v *Host) *TeamGroupHostCreate {
return _c.SetHostID(v.ID)
}
// Mutation returns the TeamGroupHostMutation object of the builder.
func (_c *TeamGroupHostCreate) Mutation() *TeamGroupHostMutation {
return _c.mutation
}
// Save creates the TeamGroupHost in the database.
func (_c *TeamGroupHostCreate) Save(ctx context.Context) (*TeamGroupHost, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *TeamGroupHostCreate) SaveX(ctx context.Context) *TeamGroupHost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamGroupHostCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamGroupHostCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_c *TeamGroupHostCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := teamgrouphost.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *TeamGroupHostCreate) check() error {
if _, ok := _c.mutation.GroupID(); !ok {
return &ValidationError{Name: "group_id", err: errors.New(`db: missing required field "TeamGroupHost.group_id"`)}
}
if _, ok := _c.mutation.HostID(); !ok {
return &ValidationError{Name: "host_id", err: errors.New(`db: missing required field "TeamGroupHost.host_id"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "TeamGroupHost.created_at"`)}
}
if len(_c.mutation.GroupIDs()) == 0 {
return &ValidationError{Name: "group", err: errors.New(`db: missing required edge "TeamGroupHost.group"`)}
}
if len(_c.mutation.HostIDs()) == 0 {
return &ValidationError{Name: "host", err: errors.New(`db: missing required edge "TeamGroupHost.host"`)}
}
return nil
}
func (_c *TeamGroupHostCreate) sqlSave(ctx context.Context) (*TeamGroupHost, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *TeamGroupHostCreate) createSpec() (*TeamGroupHost, *sqlgraph.CreateSpec) {
var (
_node = &TeamGroupHost{config: _c.config}
_spec = sqlgraph.NewCreateSpec(teamgrouphost.Table, sqlgraph.NewFieldSpec(teamgrouphost.FieldID, field.TypeUUID))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(teamgrouphost.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: teamgrouphost.GroupTable,
Columns: []string{teamgrouphost.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(teamgroup.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.GroupID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.HostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: teamgrouphost.HostTable,
Columns: []string{teamgrouphost.HostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(host.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.HostID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.TeamGroupHost.Create().
// SetGroupID(v).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.TeamGroupHostUpsert) {
// SetGroupID(v+v).
// }).
// Exec(ctx)
func (_c *TeamGroupHostCreate) OnConflict(opts ...sql.ConflictOption) *TeamGroupHostUpsertOne {
_c.conflict = opts
return &TeamGroupHostUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamGroupHostCreate) OnConflictColumns(columns ...string) *TeamGroupHostUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamGroupHostUpsertOne{
create: _c,
}
}
type (
// TeamGroupHostUpsertOne is the builder for "upsert"-ing
// one TeamGroupHost node.
TeamGroupHostUpsertOne struct {
create *TeamGroupHostCreate
}
// TeamGroupHostUpsert is the "OnConflict" setter.
TeamGroupHostUpsert struct {
*sql.UpdateSet
}
)
// SetGroupID sets the "group_id" field.
func (u *TeamGroupHostUpsert) SetGroupID(v uuid.UUID) *TeamGroupHostUpsert {
u.Set(teamgrouphost.FieldGroupID, v)
return u
}
// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsert) UpdateGroupID() *TeamGroupHostUpsert {
u.SetExcluded(teamgrouphost.FieldGroupID)
return u
}
// SetHostID sets the "host_id" field.
func (u *TeamGroupHostUpsert) SetHostID(v string) *TeamGroupHostUpsert {
u.Set(teamgrouphost.FieldHostID, v)
return u
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsert) UpdateHostID() *TeamGroupHostUpsert {
u.SetExcluded(teamgrouphost.FieldHostID)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamGroupHostUpsert) SetCreatedAt(v time.Time) *TeamGroupHostUpsert {
u.Set(teamgrouphost.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamGroupHostUpsert) UpdateCreatedAt() *TeamGroupHostUpsert {
u.SetExcluded(teamgrouphost.FieldCreatedAt)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teamgrouphost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamGroupHostUpsertOne) UpdateNewValues() *TeamGroupHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.ID(); exists {
s.SetIgnore(teamgrouphost.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamGroupHostUpsertOne) Ignore() *TeamGroupHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TeamGroupHostUpsertOne) DoNothing() *TeamGroupHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamGroupHostCreate.OnConflict
// documentation for more info.
func (u *TeamGroupHostUpsertOne) Update(set func(*TeamGroupHostUpsert)) *TeamGroupHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamGroupHostUpsert{UpdateSet: update})
}))
return u
}
// SetGroupID sets the "group_id" field.
func (u *TeamGroupHostUpsertOne) SetGroupID(v uuid.UUID) *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetGroupID(v)
})
}
// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsertOne) UpdateGroupID() *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateGroupID()
})
}
// SetHostID sets the "host_id" field.
func (u *TeamGroupHostUpsertOne) SetHostID(v string) *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetHostID(v)
})
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsertOne) UpdateHostID() *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateHostID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamGroupHostUpsertOne) SetCreatedAt(v time.Time) *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamGroupHostUpsertOne) UpdateCreatedAt() *TeamGroupHostUpsertOne {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamGroupHostUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamGroupHostCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamGroupHostUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *TeamGroupHostUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
if u.create.driver.Dialect() == dialect.MySQL {
// In case of "ON CONFLICT", there is no way to get back non-numeric ID
// fields from the database since MySQL does not support the RETURNING clause.
return id, errors.New("db: TeamGroupHostUpsertOne.ID is not supported by MySQL driver. Use TeamGroupHostUpsertOne.Exec instead")
}
node, err := u.create.Save(ctx)
if err != nil {
return id, err
}
return node.ID, nil
}
// IDX is like ID, but panics if an error occurs.
func (u *TeamGroupHostUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// TeamGroupHostCreateBulk is the builder for creating many TeamGroupHost entities in bulk.
type TeamGroupHostCreateBulk struct {
config
err error
builders []*TeamGroupHostCreate
conflict []sql.ConflictOption
}
// Save creates the TeamGroupHost entities in the database.
func (_c *TeamGroupHostCreateBulk) Save(ctx context.Context) ([]*TeamGroupHost, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*TeamGroupHost, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*TeamGroupHostMutation)
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, _c.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
spec.OnConflict = _c.conflict
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, _c.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
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, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *TeamGroupHostCreateBulk) SaveX(ctx context.Context) []*TeamGroupHost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamGroupHostCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamGroupHostCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.TeamGroupHost.CreateBulk(builders...).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.TeamGroupHostUpsert) {
// SetGroupID(v+v).
// }).
// Exec(ctx)
func (_c *TeamGroupHostCreateBulk) OnConflict(opts ...sql.ConflictOption) *TeamGroupHostUpsertBulk {
_c.conflict = opts
return &TeamGroupHostUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamGroupHostCreateBulk) OnConflictColumns(columns ...string) *TeamGroupHostUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamGroupHostUpsertBulk{
create: _c,
}
}
// TeamGroupHostUpsertBulk is the builder for "upsert"-ing
// a bulk of TeamGroupHost nodes.
type TeamGroupHostUpsertBulk struct {
create *TeamGroupHostCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teamgrouphost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamGroupHostUpsertBulk) UpdateNewValues() *TeamGroupHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.ID(); exists {
s.SetIgnore(teamgrouphost.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamGroupHost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamGroupHostUpsertBulk) Ignore() *TeamGroupHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *TeamGroupHostUpsertBulk) DoNothing() *TeamGroupHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamGroupHostCreateBulk.OnConflict
// documentation for more info.
func (u *TeamGroupHostUpsertBulk) Update(set func(*TeamGroupHostUpsert)) *TeamGroupHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamGroupHostUpsert{UpdateSet: update})
}))
return u
}
// SetGroupID sets the "group_id" field.
func (u *TeamGroupHostUpsertBulk) SetGroupID(v uuid.UUID) *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetGroupID(v)
})
}
// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsertBulk) UpdateGroupID() *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateGroupID()
})
}
// SetHostID sets the "host_id" field.
func (u *TeamGroupHostUpsertBulk) SetHostID(v string) *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetHostID(v)
})
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamGroupHostUpsertBulk) UpdateHostID() *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateHostID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamGroupHostUpsertBulk) SetCreatedAt(v time.Time) *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamGroupHostUpsertBulk) UpdateCreatedAt() *TeamGroupHostUpsertBulk {
return u.Update(func(s *TeamGroupHostUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamGroupHostUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {
return u.create.err
}
for i, b := range u.create.builders {
if len(b.conflict) != 0 {
return fmt.Errorf("db: OnConflict was set for builder %d. Set it on the TeamGroupHostCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamGroupHostCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamGroupHostUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}