Files
MonkeyCode/backend/db/gitbottask_create.go
yokowu 34320f9ed5 feat(phase6): 添加 gitbot/gittask 实体 schema 和 migration
- 新增 git_bots, git_bot_users, git_bot_tasks, project_git_bots 表
- 添加 user, host, project 到 gitbot 的关联边
- 使用 soft delete 和 created_at 游标
2026-03-19 18:14:53 +08:00

672 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/gitbot"
"github.com/chaitin/MonkeyCode/backend/db/gitbottask"
"github.com/chaitin/MonkeyCode/backend/db/task"
"github.com/google/uuid"
)
// GitBotTaskCreate is the builder for creating a GitBotTask entity.
type GitBotTaskCreate struct {
config
mutation *GitBotTaskMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetGitBotID sets the "git_bot_id" field.
func (_c *GitBotTaskCreate) SetGitBotID(v uuid.UUID) *GitBotTaskCreate {
_c.mutation.SetGitBotID(v)
return _c
}
// SetTaskID sets the "task_id" field.
func (_c *GitBotTaskCreate) SetTaskID(v uuid.UUID) *GitBotTaskCreate {
_c.mutation.SetTaskID(v)
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *GitBotTaskCreate) SetCreatedAt(v time.Time) *GitBotTaskCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *GitBotTaskCreate) SetNillableCreatedAt(v *time.Time) *GitBotTaskCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *GitBotTaskCreate) SetID(v uuid.UUID) *GitBotTaskCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *GitBotTaskCreate) SetNillableID(v *uuid.UUID) *GitBotTaskCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetTask sets the "task" edge to the Task entity.
func (_c *GitBotTaskCreate) SetTask(v *Task) *GitBotTaskCreate {
return _c.SetTaskID(v.ID)
}
// SetGitBot sets the "git_bot" edge to the GitBot entity.
func (_c *GitBotTaskCreate) SetGitBot(v *GitBot) *GitBotTaskCreate {
return _c.SetGitBotID(v.ID)
}
// Mutation returns the GitBotTaskMutation object of the builder.
func (_c *GitBotTaskCreate) Mutation() *GitBotTaskMutation {
return _c.mutation
}
// Save creates the GitBotTask in the database.
func (_c *GitBotTaskCreate) Save(ctx context.Context) (*GitBotTask, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *GitBotTaskCreate) SaveX(ctx context.Context) *GitBotTask {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *GitBotTaskCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *GitBotTaskCreate) 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 *GitBotTaskCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := gitbottask.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := gitbottask.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *GitBotTaskCreate) check() error {
if _, ok := _c.mutation.GitBotID(); !ok {
return &ValidationError{Name: "git_bot_id", err: errors.New(`db: missing required field "GitBotTask.git_bot_id"`)}
}
if _, ok := _c.mutation.TaskID(); !ok {
return &ValidationError{Name: "task_id", err: errors.New(`db: missing required field "GitBotTask.task_id"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "GitBotTask.created_at"`)}
}
if len(_c.mutation.TaskIDs()) == 0 {
return &ValidationError{Name: "task", err: errors.New(`db: missing required edge "GitBotTask.task"`)}
}
if len(_c.mutation.GitBotIDs()) == 0 {
return &ValidationError{Name: "git_bot", err: errors.New(`db: missing required edge "GitBotTask.git_bot"`)}
}
return nil
}
func (_c *GitBotTaskCreate) sqlSave(ctx context.Context) (*GitBotTask, 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 *GitBotTaskCreate) createSpec() (*GitBotTask, *sqlgraph.CreateSpec) {
var (
_node = &GitBotTask{config: _c.config}
_spec = sqlgraph.NewCreateSpec(gitbottask.Table, sqlgraph.NewFieldSpec(gitbottask.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(gitbottask.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.TaskIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: gitbottask.TaskTable,
Columns: []string{gitbottask.TaskColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(task.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.TaskID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.GitBotIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: gitbottask.GitBotTable,
Columns: []string{gitbottask.GitBotColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(gitbot.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.GitBotID = 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.GitBotTask.Create().
// SetGitBotID(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.GitBotTaskUpsert) {
// SetGitBotID(v+v).
// }).
// Exec(ctx)
func (_c *GitBotTaskCreate) OnConflict(opts ...sql.ConflictOption) *GitBotTaskUpsertOne {
_c.conflict = opts
return &GitBotTaskUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.GitBotTask.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *GitBotTaskCreate) OnConflictColumns(columns ...string) *GitBotTaskUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &GitBotTaskUpsertOne{
create: _c,
}
}
type (
// GitBotTaskUpsertOne is the builder for "upsert"-ing
// one GitBotTask node.
GitBotTaskUpsertOne struct {
create *GitBotTaskCreate
}
// GitBotTaskUpsert is the "OnConflict" setter.
GitBotTaskUpsert struct {
*sql.UpdateSet
}
)
// SetGitBotID sets the "git_bot_id" field.
func (u *GitBotTaskUpsert) SetGitBotID(v uuid.UUID) *GitBotTaskUpsert {
u.Set(gitbottask.FieldGitBotID, v)
return u
}
// UpdateGitBotID sets the "git_bot_id" field to the value that was provided on create.
func (u *GitBotTaskUpsert) UpdateGitBotID() *GitBotTaskUpsert {
u.SetExcluded(gitbottask.FieldGitBotID)
return u
}
// SetTaskID sets the "task_id" field.
func (u *GitBotTaskUpsert) SetTaskID(v uuid.UUID) *GitBotTaskUpsert {
u.Set(gitbottask.FieldTaskID, v)
return u
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *GitBotTaskUpsert) UpdateTaskID() *GitBotTaskUpsert {
u.SetExcluded(gitbottask.FieldTaskID)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *GitBotTaskUpsert) SetCreatedAt(v time.Time) *GitBotTaskUpsert {
u.Set(gitbottask.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *GitBotTaskUpsert) UpdateCreatedAt() *GitBotTaskUpsert {
u.SetExcluded(gitbottask.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.GitBotTask.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(gitbottask.FieldID)
// }),
// ).
// Exec(ctx)
func (u *GitBotTaskUpsertOne) UpdateNewValues() *GitBotTaskUpsertOne {
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(gitbottask.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.GitBotTask.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *GitBotTaskUpsertOne) Ignore() *GitBotTaskUpsertOne {
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 *GitBotTaskUpsertOne) DoNothing() *GitBotTaskUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the GitBotTaskCreate.OnConflict
// documentation for more info.
func (u *GitBotTaskUpsertOne) Update(set func(*GitBotTaskUpsert)) *GitBotTaskUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&GitBotTaskUpsert{UpdateSet: update})
}))
return u
}
// SetGitBotID sets the "git_bot_id" field.
func (u *GitBotTaskUpsertOne) SetGitBotID(v uuid.UUID) *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetGitBotID(v)
})
}
// UpdateGitBotID sets the "git_bot_id" field to the value that was provided on create.
func (u *GitBotTaskUpsertOne) UpdateGitBotID() *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateGitBotID()
})
}
// SetTaskID sets the "task_id" field.
func (u *GitBotTaskUpsertOne) SetTaskID(v uuid.UUID) *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetTaskID(v)
})
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *GitBotTaskUpsertOne) UpdateTaskID() *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateTaskID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *GitBotTaskUpsertOne) SetCreatedAt(v time.Time) *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *GitBotTaskUpsertOne) UpdateCreatedAt() *GitBotTaskUpsertOne {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *GitBotTaskUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for GitBotTaskCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *GitBotTaskUpsertOne) 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 *GitBotTaskUpsertOne) 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: GitBotTaskUpsertOne.ID is not supported by MySQL driver. Use GitBotTaskUpsertOne.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 *GitBotTaskUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// GitBotTaskCreateBulk is the builder for creating many GitBotTask entities in bulk.
type GitBotTaskCreateBulk struct {
config
err error
builders []*GitBotTaskCreate
conflict []sql.ConflictOption
}
// Save creates the GitBotTask entities in the database.
func (_c *GitBotTaskCreateBulk) Save(ctx context.Context) ([]*GitBotTask, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*GitBotTask, 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.(*GitBotTaskMutation)
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 *GitBotTaskCreateBulk) SaveX(ctx context.Context) []*GitBotTask {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *GitBotTaskCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *GitBotTaskCreateBulk) 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.GitBotTask.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.GitBotTaskUpsert) {
// SetGitBotID(v+v).
// }).
// Exec(ctx)
func (_c *GitBotTaskCreateBulk) OnConflict(opts ...sql.ConflictOption) *GitBotTaskUpsertBulk {
_c.conflict = opts
return &GitBotTaskUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.GitBotTask.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *GitBotTaskCreateBulk) OnConflictColumns(columns ...string) *GitBotTaskUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &GitBotTaskUpsertBulk{
create: _c,
}
}
// GitBotTaskUpsertBulk is the builder for "upsert"-ing
// a bulk of GitBotTask nodes.
type GitBotTaskUpsertBulk struct {
create *GitBotTaskCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.GitBotTask.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(gitbottask.FieldID)
// }),
// ).
// Exec(ctx)
func (u *GitBotTaskUpsertBulk) UpdateNewValues() *GitBotTaskUpsertBulk {
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(gitbottask.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.GitBotTask.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *GitBotTaskUpsertBulk) Ignore() *GitBotTaskUpsertBulk {
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 *GitBotTaskUpsertBulk) DoNothing() *GitBotTaskUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the GitBotTaskCreateBulk.OnConflict
// documentation for more info.
func (u *GitBotTaskUpsertBulk) Update(set func(*GitBotTaskUpsert)) *GitBotTaskUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&GitBotTaskUpsert{UpdateSet: update})
}))
return u
}
// SetGitBotID sets the "git_bot_id" field.
func (u *GitBotTaskUpsertBulk) SetGitBotID(v uuid.UUID) *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetGitBotID(v)
})
}
// UpdateGitBotID sets the "git_bot_id" field to the value that was provided on create.
func (u *GitBotTaskUpsertBulk) UpdateGitBotID() *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateGitBotID()
})
}
// SetTaskID sets the "task_id" field.
func (u *GitBotTaskUpsertBulk) SetTaskID(v uuid.UUID) *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetTaskID(v)
})
}
// UpdateTaskID sets the "task_id" field to the value that was provided on create.
func (u *GitBotTaskUpsertBulk) UpdateTaskID() *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateTaskID()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *GitBotTaskUpsertBulk) SetCreatedAt(v time.Time) *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *GitBotTaskUpsertBulk) UpdateCreatedAt() *GitBotTaskUpsertBulk {
return u.Update(func(s *GitBotTaskUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *GitBotTaskUpsertBulk) 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 GitBotTaskCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for GitBotTaskCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *GitBotTaskUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}