Files
MonkeyCode/backend/db/teammodel_create.go
yokowu d4aecca9db feat: 添加模型和镜像管理模块(Phase 2 迁移)
从内部项目迁移模型配置和镜像配置模块,包括个人用户设置和团队级管理,
涵盖完整 CRUD、健康检查、供应商模型列表、所有权模型和 Swagger 注释。

- Ent schema: model, image, team_model, team_image, team_group_model, team_group_image
- Domain 层: 接口、请求/响应类型、From 转换器
- Setting 模块: 个人模型/镜像 repo、usecase(代理重试、健康检查)、handler
- Team 模块: 团队模型/镜像 repo、usecase、handler(TeamAuth + 审计中间件)
- SQL 迁移: 000002_model_image up/down
2026-03-16 21:11:42 +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/model"
"github.com/chaitin/MonkeyCode/backend/db/team"
"github.com/chaitin/MonkeyCode/backend/db/teammodel"
"github.com/google/uuid"
)
// TeamModelCreate is the builder for creating a TeamModel entity.
type TeamModelCreate struct {
config
mutation *TeamModelMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetTeamID sets the "team_id" field.
func (_c *TeamModelCreate) SetTeamID(v uuid.UUID) *TeamModelCreate {
_c.mutation.SetTeamID(v)
return _c
}
// SetModelID sets the "model_id" field.
func (_c *TeamModelCreate) SetModelID(v uuid.UUID) *TeamModelCreate {
_c.mutation.SetModelID(v)
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *TeamModelCreate) SetCreatedAt(v time.Time) *TeamModelCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *TeamModelCreate) SetNillableCreatedAt(v *time.Time) *TeamModelCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *TeamModelCreate) SetID(v uuid.UUID) *TeamModelCreate {
_c.mutation.SetID(v)
return _c
}
// SetTeam sets the "team" edge to the Team entity.
func (_c *TeamModelCreate) SetTeam(v *Team) *TeamModelCreate {
return _c.SetTeamID(v.ID)
}
// SetModel sets the "model" edge to the Model entity.
func (_c *TeamModelCreate) SetModel(v *Model) *TeamModelCreate {
return _c.SetModelID(v.ID)
}
// Mutation returns the TeamModelMutation object of the builder.
func (_c *TeamModelCreate) Mutation() *TeamModelMutation {
return _c.mutation
}
// Save creates the TeamModel in the database.
func (_c *TeamModelCreate) Save(ctx context.Context) (*TeamModel, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *TeamModelCreate) SaveX(ctx context.Context) *TeamModel {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamModelCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamModelCreate) 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 *TeamModelCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := teammodel.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *TeamModelCreate) check() error {
if _, ok := _c.mutation.TeamID(); !ok {
return &ValidationError{Name: "team_id", err: errors.New(`db: missing required field "TeamModel.team_id"`)}
}
if _, ok := _c.mutation.ModelID(); !ok {
return &ValidationError{Name: "model_id", err: errors.New(`db: missing required field "TeamModel.model_id"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "TeamModel.created_at"`)}
}
if len(_c.mutation.TeamIDs()) == 0 {
return &ValidationError{Name: "team", err: errors.New(`db: missing required edge "TeamModel.team"`)}
}
if len(_c.mutation.ModelIDs()) == 0 {
return &ValidationError{Name: "model", err: errors.New(`db: missing required edge "TeamModel.model"`)}
}
return nil
}
func (_c *TeamModelCreate) sqlSave(ctx context.Context) (*TeamModel, 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 *TeamModelCreate) createSpec() (*TeamModel, *sqlgraph.CreateSpec) {
var (
_node = &TeamModel{config: _c.config}
_spec = sqlgraph.NewCreateSpec(teammodel.Table, sqlgraph.NewFieldSpec(teammodel.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(teammodel.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.TeamIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: teammodel.TeamTable,
Columns: []string{teammodel.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.ModelIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: teammodel.ModelTable,
Columns: []string{teammodel.ModelColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(model.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ModelID = 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.TeamModel.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.TeamModelUpsert) {
// SetTeamID(v+v).
// }).
// Exec(ctx)
func (_c *TeamModelCreate) OnConflict(opts ...sql.ConflictOption) *TeamModelUpsertOne {
_c.conflict = opts
return &TeamModelUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamModel.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamModelCreate) OnConflictColumns(columns ...string) *TeamModelUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamModelUpsertOne{
create: _c,
}
}
type (
// TeamModelUpsertOne is the builder for "upsert"-ing
// one TeamModel node.
TeamModelUpsertOne struct {
create *TeamModelCreate
}
// TeamModelUpsert is the "OnConflict" setter.
TeamModelUpsert struct {
*sql.UpdateSet
}
)
// SetTeamID sets the "team_id" field.
func (u *TeamModelUpsert) SetTeamID(v uuid.UUID) *TeamModelUpsert {
u.Set(teammodel.FieldTeamID, v)
return u
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamModelUpsert) UpdateTeamID() *TeamModelUpsert {
u.SetExcluded(teammodel.FieldTeamID)
return u
}
// SetModelID sets the "model_id" field.
func (u *TeamModelUpsert) SetModelID(v uuid.UUID) *TeamModelUpsert {
u.Set(teammodel.FieldModelID, v)
return u
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *TeamModelUpsert) UpdateModelID() *TeamModelUpsert {
u.SetExcluded(teammodel.FieldModelID)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamModelUpsert) SetCreatedAt(v time.Time) *TeamModelUpsert {
u.Set(teammodel.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamModelUpsert) UpdateCreatedAt() *TeamModelUpsert {
u.SetExcluded(teammodel.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.TeamModel.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teammodel.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamModelUpsertOne) UpdateNewValues() *TeamModelUpsertOne {
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(teammodel.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamModel.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamModelUpsertOne) Ignore() *TeamModelUpsertOne {
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 *TeamModelUpsertOne) DoNothing() *TeamModelUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamModelCreate.OnConflict
// documentation for more info.
func (u *TeamModelUpsertOne) Update(set func(*TeamModelUpsert)) *TeamModelUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamModelUpsert{UpdateSet: update})
}))
return u
}
// SetTeamID sets the "team_id" field.
func (u *TeamModelUpsertOne) SetTeamID(v uuid.UUID) *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.SetTeamID(v)
})
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamModelUpsertOne) UpdateTeamID() *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateTeamID()
})
}
// SetModelID sets the "model_id" field.
func (u *TeamModelUpsertOne) SetModelID(v uuid.UUID) *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.SetModelID(v)
})
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *TeamModelUpsertOne) UpdateModelID() *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateModelID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamModelUpsertOne) SetCreatedAt(v time.Time) *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamModelUpsertOne) UpdateCreatedAt() *TeamModelUpsertOne {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamModelUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamModelCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamModelUpsertOne) 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 *TeamModelUpsertOne) 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: TeamModelUpsertOne.ID is not supported by MySQL driver. Use TeamModelUpsertOne.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 *TeamModelUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// TeamModelCreateBulk is the builder for creating many TeamModel entities in bulk.
type TeamModelCreateBulk struct {
config
err error
builders []*TeamModelCreate
conflict []sql.ConflictOption
}
// Save creates the TeamModel entities in the database.
func (_c *TeamModelCreateBulk) Save(ctx context.Context) ([]*TeamModel, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*TeamModel, 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.(*TeamModelMutation)
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 *TeamModelCreateBulk) SaveX(ctx context.Context) []*TeamModel {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TeamModelCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TeamModelCreateBulk) 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.TeamModel.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.TeamModelUpsert) {
// SetTeamID(v+v).
// }).
// Exec(ctx)
func (_c *TeamModelCreateBulk) OnConflict(opts ...sql.ConflictOption) *TeamModelUpsertBulk {
_c.conflict = opts
return &TeamModelUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.TeamModel.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TeamModelCreateBulk) OnConflictColumns(columns ...string) *TeamModelUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TeamModelUpsertBulk{
create: _c,
}
}
// TeamModelUpsertBulk is the builder for "upsert"-ing
// a bulk of TeamModel nodes.
type TeamModelUpsertBulk struct {
create *TeamModelCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.TeamModel.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(teammodel.FieldID)
// }),
// ).
// Exec(ctx)
func (u *TeamModelUpsertBulk) UpdateNewValues() *TeamModelUpsertBulk {
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(teammodel.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.TeamModel.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TeamModelUpsertBulk) Ignore() *TeamModelUpsertBulk {
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 *TeamModelUpsertBulk) DoNothing() *TeamModelUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TeamModelCreateBulk.OnConflict
// documentation for more info.
func (u *TeamModelUpsertBulk) Update(set func(*TeamModelUpsert)) *TeamModelUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TeamModelUpsert{UpdateSet: update})
}))
return u
}
// SetTeamID sets the "team_id" field.
func (u *TeamModelUpsertBulk) SetTeamID(v uuid.UUID) *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.SetTeamID(v)
})
}
// UpdateTeamID sets the "team_id" field to the value that was provided on create.
func (u *TeamModelUpsertBulk) UpdateTeamID() *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateTeamID()
})
}
// SetModelID sets the "model_id" field.
func (u *TeamModelUpsertBulk) SetModelID(v uuid.UUID) *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.SetModelID(v)
})
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *TeamModelUpsertBulk) UpdateModelID() *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateModelID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TeamModelUpsertBulk) SetCreatedAt(v time.Time) *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TeamModelUpsertBulk) UpdateCreatedAt() *TeamModelUpsertBulk {
return u.Update(func(s *TeamModelUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TeamModelUpsertBulk) 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 TeamModelCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for TeamModelCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TeamModelUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}