Files
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
19 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/team"
"github.com/chaitin/MonkeyCode/backend/db/teamhost"
"github.com/google/uuid"
)
// TeamHostCreate is the builder for creating a TeamHost entity.
type TeamHostCreate struct {
config
mutation *TeamHostMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetTeamID sets the "team_id" field.
func (_c *TeamHostCreate) SetTeamID(v uuid.UUID) *TeamHostCreate {
_c.mutation.SetTeamID(v)
return _c
}
// SetHostID sets the "host_id" field.
func (_c *TeamHostCreate) SetHostID(v string) *TeamHostCreate {
_c.mutation.SetHostID(v)
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *TeamHostCreate) SetCreatedAt(v time.Time) *TeamHostCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *TeamHostCreate) SetNillableCreatedAt(v *time.Time) *TeamHostCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *TeamHostCreate) SetID(v uuid.UUID) *TeamHostCreate {
_c.mutation.SetID(v)
return _c
}
// SetTeam sets the "team" edge to the Team entity.
func (_c *TeamHostCreate) SetTeam(v *Team) *TeamHostCreate {
return _c.SetTeamID(v.ID)
}
// SetHost sets the "host" edge to the Host entity.
func (_c *TeamHostCreate) SetHost(v *Host) *TeamHostCreate {
return _c.SetHostID(v.ID)
}
// Mutation returns the TeamHostMutation object of the builder.
func (_c *TeamHostCreate) Mutation() *TeamHostMutation {
return _c.mutation
}
// Save creates the TeamHost in the database.
func (_c *TeamHostCreate) Save(ctx context.Context) (*TeamHost, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *TeamHostCreate) SaveX(ctx context.Context) *TeamHost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamHostCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamHostCreate) 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 *TeamHostCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := teamhost.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *TeamHostCreate) check() error {
if _, ok := _c.mutation.TeamID(); !ok {
return &ValidationError{Name: "team_id", err: errors.New(`db: missing required field "TeamHost.team_id"`)}
}
if _, ok := _c.mutation.HostID(); !ok {
return &ValidationError{Name: "host_id", err: errors.New(`db: missing required field "TeamHost.host_id"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "TeamHost.created_at"`)}
}
if len(_c.mutation.TeamIDs()) == 0 {
return &ValidationError{Name: "team", err: errors.New(`db: missing required edge "TeamHost.team"`)}
}
if len(_c.mutation.HostIDs()) == 0 {
return &ValidationError{Name: "host", err: errors.New(`db: missing required edge "TeamHost.host"`)}
}
return nil
}
func (_c *TeamHostCreate) sqlSave(ctx context.Context) (*TeamHost, 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 *TeamHostCreate) createSpec() (*TeamHost, *sqlgraph.CreateSpec) {
var (
_node = &TeamHost{config: _c.config}
_spec = sqlgraph.NewCreateSpec(teamhost.Table, sqlgraph.NewFieldSpec(teamhost.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(teamhost.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.TeamIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: teamhost.TeamTable,
Columns: []string{teamhost.TeamColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(team.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.TeamID = 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: teamhost.HostTable,
Columns: []string{teamhost.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.TeamHost.Create().
// SetTeamID(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.TeamHostUpsert) {
// SetTeamID(v+v).
// }).
// Exec(ctx)
func (_c *TeamHostCreate) OnConflict(opts ...sql.ConflictOption) *TeamHostUpsertOne {
_c.conflict = opts
return &TeamHostUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamHost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamHostCreate) OnConflictColumns(columns ...string) *TeamHostUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamHostUpsertOne{
create: _c,
}
}
type (
// TeamHostUpsertOne is the builder for "upsert"-ing
// one TeamHost node.
TeamHostUpsertOne struct {
create *TeamHostCreate
}
// TeamHostUpsert is the "OnConflict" setter.
TeamHostUpsert struct {
*sql.UpdateSet
}
)
// SetTeamID sets the "team_id" field.
func (u *TeamHostUpsert) SetTeamID(v uuid.UUID) *TeamHostUpsert {
u.Set(teamhost.FieldTeamID, v)
return u
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamHostUpsert) UpdateTeamID() *TeamHostUpsert {
u.SetExcluded(teamhost.FieldTeamID)
return u
}
// SetHostID sets the "host_id" field.
func (u *TeamHostUpsert) SetHostID(v string) *TeamHostUpsert {
u.Set(teamhost.FieldHostID, v)
return u
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamHostUpsert) UpdateHostID() *TeamHostUpsert {
u.SetExcluded(teamhost.FieldHostID)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamHostUpsert) SetCreatedAt(v time.Time) *TeamHostUpsert {
u.Set(teamhost.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamHostUpsert) UpdateCreatedAt() *TeamHostUpsert {
u.SetExcluded(teamhost.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.TeamHost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teamhost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamHostUpsertOne) UpdateNewValues() *TeamHostUpsertOne {
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(teamhost.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamHost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamHostUpsertOne) Ignore() *TeamHostUpsertOne {
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 *TeamHostUpsertOne) DoNothing() *TeamHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamHostCreate.OnConflict
// documentation for more info.
func (u *TeamHostUpsertOne) Update(set func(*TeamHostUpsert)) *TeamHostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamHostUpsert{UpdateSet: update})
}))
return u
}
// SetTeamID sets the "team_id" field.
func (u *TeamHostUpsertOne) SetTeamID(v uuid.UUID) *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.SetTeamID(v)
})
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamHostUpsertOne) UpdateTeamID() *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateTeamID()
})
}
// SetHostID sets the "host_id" field.
func (u *TeamHostUpsertOne) SetHostID(v string) *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.SetHostID(v)
})
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamHostUpsertOne) UpdateHostID() *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateHostID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamHostUpsertOne) SetCreatedAt(v time.Time) *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamHostUpsertOne) UpdateCreatedAt() *TeamHostUpsertOne {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamHostUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamHostCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamHostUpsertOne) 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 *TeamHostUpsertOne) 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: TeamHostUpsertOne.ID is not supported by MySQL driver. Use TeamHostUpsertOne.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 *TeamHostUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// TeamHostCreateBulk is the builder for creating many TeamHost entities in bulk.
type TeamHostCreateBulk struct {
config
err error
builders []*TeamHostCreate
conflict []sql.ConflictOption
}
// Save creates the TeamHost entities in the database.
func (_c *TeamHostCreateBulk) Save(ctx context.Context) ([]*TeamHost, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*TeamHost, 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.(*TeamHostMutation)
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 *TeamHostCreateBulk) SaveX(ctx context.Context) []*TeamHost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamHostCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamHostCreateBulk) 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.TeamHost.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.TeamHostUpsert) {
// SetTeamID(v+v).
// }).
// Exec(ctx)
func (_c *TeamHostCreateBulk) OnConflict(opts ...sql.ConflictOption) *TeamHostUpsertBulk {
_c.conflict = opts
return &TeamHostUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamHost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamHostCreateBulk) OnConflictColumns(columns ...string) *TeamHostUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamHostUpsertBulk{
create: _c,
}
}
// TeamHostUpsertBulk is the builder for "upsert"-ing
// a bulk of TeamHost nodes.
type TeamHostUpsertBulk struct {
create *TeamHostCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.TeamHost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teamhost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamHostUpsertBulk) UpdateNewValues() *TeamHostUpsertBulk {
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(teamhost.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamHost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamHostUpsertBulk) Ignore() *TeamHostUpsertBulk {
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 *TeamHostUpsertBulk) DoNothing() *TeamHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamHostCreateBulk.OnConflict
// documentation for more info.
func (u *TeamHostUpsertBulk) Update(set func(*TeamHostUpsert)) *TeamHostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamHostUpsert{UpdateSet: update})
}))
return u
}
// SetTeamID sets the "team_id" field.
func (u *TeamHostUpsertBulk) SetTeamID(v uuid.UUID) *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.SetTeamID(v)
})
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamHostUpsertBulk) UpdateTeamID() *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateTeamID()
})
}
// SetHostID sets the "host_id" field.
func (u *TeamHostUpsertBulk) SetHostID(v string) *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.SetHostID(v)
})
}
// UpdateHostID sets the "host_id" field to the value that was provided on create.
func (u *TeamHostUpsertBulk) UpdateHostID() *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateHostID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamHostUpsertBulk) SetCreatedAt(v time.Time) *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamHostUpsertBulk) UpdateCreatedAt() *TeamHostUpsertBulk {
return u.Update(func(s *TeamHostUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamHostUpsertBulk) 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 TeamHostCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamHostCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamHostUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}