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