问题: 1. Setting.value 字段设置了 NotEmpty() 约束,导致保存空字符串值时验证失败 2. 数据库 settings 表缺少 key 字段的唯一约束,导致 ON CONFLICT 语句执行失败 修复: - 移除 ent/schema/setting.go 中 value 字段的 NotEmpty() 约束 - 新增迁移 015_fix_settings_unique_constraint.sql 添加缺失的唯一约束 - 添加3个回归测试确保空值保存功能正常 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
585 lines
16 KiB
Go
585 lines
16 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/Wei-Shaw/sub2api/ent/setting"
|
|
)
|
|
|
|
// SettingCreate is the builder for creating a Setting entity.
|
|
type SettingCreate struct {
|
|
config
|
|
mutation *SettingMutation
|
|
hooks []Hook
|
|
conflict []sql.ConflictOption
|
|
}
|
|
|
|
// SetKey sets the "key" field.
|
|
func (_c *SettingCreate) SetKey(v string) *SettingCreate {
|
|
_c.mutation.SetKey(v)
|
|
return _c
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (_c *SettingCreate) SetValue(v string) *SettingCreate {
|
|
_c.mutation.SetValue(v)
|
|
return _c
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (_c *SettingCreate) SetUpdatedAt(v time.Time) *SettingCreate {
|
|
_c.mutation.SetUpdatedAt(v)
|
|
return _c
|
|
}
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
func (_c *SettingCreate) SetNillableUpdatedAt(v *time.Time) *SettingCreate {
|
|
if v != nil {
|
|
_c.SetUpdatedAt(*v)
|
|
}
|
|
return _c
|
|
}
|
|
|
|
// Mutation returns the SettingMutation object of the builder.
|
|
func (_c *SettingCreate) Mutation() *SettingMutation {
|
|
return _c.mutation
|
|
}
|
|
|
|
// Save creates the Setting in the database.
|
|
func (_c *SettingCreate) Save(ctx context.Context) (*Setting, error) {
|
|
_c.defaults()
|
|
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (_c *SettingCreate) SaveX(ctx context.Context) *Setting {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *SettingCreate) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *SettingCreate) 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 *SettingCreate) defaults() {
|
|
if _, ok := _c.mutation.UpdatedAt(); !ok {
|
|
v := setting.DefaultUpdatedAt()
|
|
_c.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (_c *SettingCreate) check() error {
|
|
if _, ok := _c.mutation.Key(); !ok {
|
|
return &ValidationError{Name: "key", err: errors.New(`ent: missing required field "Setting.key"`)}
|
|
}
|
|
if v, ok := _c.mutation.Key(); ok {
|
|
if err := setting.KeyValidator(v); err != nil {
|
|
return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := _c.mutation.Value(); !ok {
|
|
return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Setting.value"`)}
|
|
}
|
|
if _, ok := _c.mutation.UpdatedAt(); !ok {
|
|
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Setting.updated_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_c *SettingCreate) sqlSave(ctx context.Context) (*Setting, 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
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int64(id)
|
|
_c.mutation.id = &_node.ID
|
|
_c.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (_c *SettingCreate) createSpec() (*Setting, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Setting{config: _c.config}
|
|
_spec = sqlgraph.NewCreateSpec(setting.Table, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
|
|
)
|
|
_spec.OnConflict = _c.conflict
|
|
if value, ok := _c.mutation.Key(); ok {
|
|
_spec.SetField(setting.FieldKey, field.TypeString, value)
|
|
_node.Key = value
|
|
}
|
|
if value, ok := _c.mutation.Value(); ok {
|
|
_spec.SetField(setting.FieldValue, field.TypeString, value)
|
|
_node.Value = value
|
|
}
|
|
if value, ok := _c.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
|
|
_node.UpdatedAt = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
|
|
// of the `INSERT` statement. For example:
|
|
//
|
|
// client.Setting.Create().
|
|
// SetKey(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.SettingUpsert) {
|
|
// SetKey(v+v).
|
|
// }).
|
|
// Exec(ctx)
|
|
func (_c *SettingCreate) OnConflict(opts ...sql.ConflictOption) *SettingUpsertOne {
|
|
_c.conflict = opts
|
|
return &SettingUpsertOne{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// OnConflictColumns calls `OnConflict` and configures the columns
|
|
// as conflict target. Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(sql.ConflictColumns(columns...)).
|
|
// Exec(ctx)
|
|
func (_c *SettingCreate) OnConflictColumns(columns ...string) *SettingUpsertOne {
|
|
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
|
|
return &SettingUpsertOne{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
type (
|
|
// SettingUpsertOne is the builder for "upsert"-ing
|
|
// one Setting node.
|
|
SettingUpsertOne struct {
|
|
create *SettingCreate
|
|
}
|
|
|
|
// SettingUpsert is the "OnConflict" setter.
|
|
SettingUpsert struct {
|
|
*sql.UpdateSet
|
|
}
|
|
)
|
|
|
|
// SetKey sets the "key" field.
|
|
func (u *SettingUpsert) SetKey(v string) *SettingUpsert {
|
|
u.Set(setting.FieldKey, v)
|
|
return u
|
|
}
|
|
|
|
// UpdateKey sets the "key" field to the value that was provided on create.
|
|
func (u *SettingUpsert) UpdateKey() *SettingUpsert {
|
|
u.SetExcluded(setting.FieldKey)
|
|
return u
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (u *SettingUpsert) SetValue(v string) *SettingUpsert {
|
|
u.Set(setting.FieldValue, v)
|
|
return u
|
|
}
|
|
|
|
// UpdateValue sets the "value" field to the value that was provided on create.
|
|
func (u *SettingUpsert) UpdateValue() *SettingUpsert {
|
|
u.SetExcluded(setting.FieldValue)
|
|
return u
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (u *SettingUpsert) SetUpdatedAt(v time.Time) *SettingUpsert {
|
|
u.Set(setting.FieldUpdatedAt, v)
|
|
return u
|
|
}
|
|
|
|
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
|
|
func (u *SettingUpsert) UpdateUpdatedAt() *SettingUpsert {
|
|
u.SetExcluded(setting.FieldUpdatedAt)
|
|
return u
|
|
}
|
|
|
|
// UpdateNewValues updates the mutable fields using the new values that were set on create.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(
|
|
// sql.ResolveWithNewValues(),
|
|
// ).
|
|
// Exec(ctx)
|
|
func (u *SettingUpsertOne) UpdateNewValues() *SettingUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
|
|
return u
|
|
}
|
|
|
|
// Ignore sets each column to itself in case of conflict.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(sql.ResolveWithIgnore()).
|
|
// Exec(ctx)
|
|
func (u *SettingUpsertOne) Ignore() *SettingUpsertOne {
|
|
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 *SettingUpsertOne) DoNothing() *SettingUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.DoNothing())
|
|
return u
|
|
}
|
|
|
|
// Update allows overriding fields `UPDATE` values. See the SettingCreate.OnConflict
|
|
// documentation for more info.
|
|
func (u *SettingUpsertOne) Update(set func(*SettingUpsert)) *SettingUpsertOne {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
|
|
set(&SettingUpsert{UpdateSet: update})
|
|
}))
|
|
return u
|
|
}
|
|
|
|
// SetKey sets the "key" field.
|
|
func (u *SettingUpsertOne) SetKey(v string) *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetKey(v)
|
|
})
|
|
}
|
|
|
|
// UpdateKey sets the "key" field to the value that was provided on create.
|
|
func (u *SettingUpsertOne) UpdateKey() *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateKey()
|
|
})
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (u *SettingUpsertOne) SetValue(v string) *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetValue(v)
|
|
})
|
|
}
|
|
|
|
// UpdateValue sets the "value" field to the value that was provided on create.
|
|
func (u *SettingUpsertOne) UpdateValue() *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateValue()
|
|
})
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (u *SettingUpsertOne) SetUpdatedAt(v time.Time) *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetUpdatedAt(v)
|
|
})
|
|
}
|
|
|
|
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
|
|
func (u *SettingUpsertOne) UpdateUpdatedAt() *SettingUpsertOne {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateUpdatedAt()
|
|
})
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (u *SettingUpsertOne) Exec(ctx context.Context) error {
|
|
if len(u.create.conflict) == 0 {
|
|
return errors.New("ent: missing options for SettingCreate.OnConflict")
|
|
}
|
|
return u.create.Exec(ctx)
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (u *SettingUpsertOne) 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 *SettingUpsertOne) ID(ctx context.Context) (id int64, err error) {
|
|
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 *SettingUpsertOne) IDX(ctx context.Context) int64 {
|
|
id, err := u.ID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// SettingCreateBulk is the builder for creating many Setting entities in bulk.
|
|
type SettingCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*SettingCreate
|
|
conflict []sql.ConflictOption
|
|
}
|
|
|
|
// Save creates the Setting entities in the database.
|
|
func (_c *SettingCreateBulk) Save(ctx context.Context) ([]*Setting, error) {
|
|
if _c.err != nil {
|
|
return nil, _c.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
|
|
nodes := make([]*Setting, 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.(*SettingMutation)
|
|
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
|
|
if specs[i].ID.Value != nil {
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int64(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 *SettingCreateBulk) SaveX(ctx context.Context) []*Setting {
|
|
v, err := _c.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_c *SettingCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := _c.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_c *SettingCreateBulk) 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.Setting.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.SettingUpsert) {
|
|
// SetKey(v+v).
|
|
// }).
|
|
// Exec(ctx)
|
|
func (_c *SettingCreateBulk) OnConflict(opts ...sql.ConflictOption) *SettingUpsertBulk {
|
|
_c.conflict = opts
|
|
return &SettingUpsertBulk{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// OnConflictColumns calls `OnConflict` and configures the columns
|
|
// as conflict target. Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(sql.ConflictColumns(columns...)).
|
|
// Exec(ctx)
|
|
func (_c *SettingCreateBulk) OnConflictColumns(columns ...string) *SettingUpsertBulk {
|
|
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
|
|
return &SettingUpsertBulk{
|
|
create: _c,
|
|
}
|
|
}
|
|
|
|
// SettingUpsertBulk is the builder for "upsert"-ing
|
|
// a bulk of Setting nodes.
|
|
type SettingUpsertBulk struct {
|
|
create *SettingCreateBulk
|
|
}
|
|
|
|
// UpdateNewValues updates the mutable fields using the new values that
|
|
// were set on create. Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(
|
|
// sql.ResolveWithNewValues(),
|
|
// ).
|
|
// Exec(ctx)
|
|
func (u *SettingUpsertBulk) UpdateNewValues() *SettingUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
|
|
return u
|
|
}
|
|
|
|
// Ignore sets each column to itself in case of conflict.
|
|
// Using this option is equivalent to using:
|
|
//
|
|
// client.Setting.Create().
|
|
// OnConflict(sql.ResolveWithIgnore()).
|
|
// Exec(ctx)
|
|
func (u *SettingUpsertBulk) Ignore() *SettingUpsertBulk {
|
|
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 *SettingUpsertBulk) DoNothing() *SettingUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.DoNothing())
|
|
return u
|
|
}
|
|
|
|
// Update allows overriding fields `UPDATE` values. See the SettingCreateBulk.OnConflict
|
|
// documentation for more info.
|
|
func (u *SettingUpsertBulk) Update(set func(*SettingUpsert)) *SettingUpsertBulk {
|
|
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
|
|
set(&SettingUpsert{UpdateSet: update})
|
|
}))
|
|
return u
|
|
}
|
|
|
|
// SetKey sets the "key" field.
|
|
func (u *SettingUpsertBulk) SetKey(v string) *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetKey(v)
|
|
})
|
|
}
|
|
|
|
// UpdateKey sets the "key" field to the value that was provided on create.
|
|
func (u *SettingUpsertBulk) UpdateKey() *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateKey()
|
|
})
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (u *SettingUpsertBulk) SetValue(v string) *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetValue(v)
|
|
})
|
|
}
|
|
|
|
// UpdateValue sets the "value" field to the value that was provided on create.
|
|
func (u *SettingUpsertBulk) UpdateValue() *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateValue()
|
|
})
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (u *SettingUpsertBulk) SetUpdatedAt(v time.Time) *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.SetUpdatedAt(v)
|
|
})
|
|
}
|
|
|
|
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
|
|
func (u *SettingUpsertBulk) UpdateUpdatedAt() *SettingUpsertBulk {
|
|
return u.Update(func(s *SettingUpsert) {
|
|
s.UpdateUpdatedAt()
|
|
})
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (u *SettingUpsertBulk) 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("ent: OnConflict was set for builder %d. Set it on the SettingCreateBulk instead", i)
|
|
}
|
|
}
|
|
if len(u.create.conflict) == 0 {
|
|
return errors.New("ent: missing options for SettingCreateBulk.OnConflict")
|
|
}
|
|
return u.create.Exec(ctx)
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (u *SettingUpsertBulk) ExecX(ctx context.Context) {
|
|
if err := u.create.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|