问题: 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>
307 lines
8.4 KiB
Go
307 lines
8.4 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/predicate"
|
|
"github.com/Wei-Shaw/sub2api/ent/setting"
|
|
)
|
|
|
|
// SettingUpdate is the builder for updating Setting entities.
|
|
type SettingUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *SettingMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the SettingUpdate builder.
|
|
func (_u *SettingUpdate) Where(ps ...predicate.Setting) *SettingUpdate {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// SetKey sets the "key" field.
|
|
func (_u *SettingUpdate) SetKey(v string) *SettingUpdate {
|
|
_u.mutation.SetKey(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableKey sets the "key" field if the given value is not nil.
|
|
func (_u *SettingUpdate) SetNillableKey(v *string) *SettingUpdate {
|
|
if v != nil {
|
|
_u.SetKey(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (_u *SettingUpdate) SetValue(v string) *SettingUpdate {
|
|
_u.mutation.SetValue(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableValue sets the "value" field if the given value is not nil.
|
|
func (_u *SettingUpdate) SetNillableValue(v *string) *SettingUpdate {
|
|
if v != nil {
|
|
_u.SetValue(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (_u *SettingUpdate) SetUpdatedAt(v time.Time) *SettingUpdate {
|
|
_u.mutation.SetUpdatedAt(v)
|
|
return _u
|
|
}
|
|
|
|
// Mutation returns the SettingMutation object of the builder.
|
|
func (_u *SettingUpdate) Mutation() *SettingMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (_u *SettingUpdate) Save(ctx context.Context) (int, error) {
|
|
_u.defaults()
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *SettingUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (_u *SettingUpdate) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *SettingUpdate) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (_u *SettingUpdate) defaults() {
|
|
if _, ok := _u.mutation.UpdatedAt(); !ok {
|
|
v := setting.UpdateDefaultUpdatedAt()
|
|
_u.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (_u *SettingUpdate) check() error {
|
|
if v, ok := _u.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *SettingUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
|
|
if ps := _u.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := _u.mutation.Key(); ok {
|
|
_spec.SetField(setting.FieldKey, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Value(); ok {
|
|
_spec.SetField(setting.FieldValue, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{setting.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
// SettingUpdateOne is the builder for updating a single Setting entity.
|
|
type SettingUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *SettingMutation
|
|
}
|
|
|
|
// SetKey sets the "key" field.
|
|
func (_u *SettingUpdateOne) SetKey(v string) *SettingUpdateOne {
|
|
_u.mutation.SetKey(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableKey sets the "key" field if the given value is not nil.
|
|
func (_u *SettingUpdateOne) SetNillableKey(v *string) *SettingUpdateOne {
|
|
if v != nil {
|
|
_u.SetKey(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetValue sets the "value" field.
|
|
func (_u *SettingUpdateOne) SetValue(v string) *SettingUpdateOne {
|
|
_u.mutation.SetValue(v)
|
|
return _u
|
|
}
|
|
|
|
// SetNillableValue sets the "value" field if the given value is not nil.
|
|
func (_u *SettingUpdateOne) SetNillableValue(v *string) *SettingUpdateOne {
|
|
if v != nil {
|
|
_u.SetValue(*v)
|
|
}
|
|
return _u
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (_u *SettingUpdateOne) SetUpdatedAt(v time.Time) *SettingUpdateOne {
|
|
_u.mutation.SetUpdatedAt(v)
|
|
return _u
|
|
}
|
|
|
|
// Mutation returns the SettingMutation object of the builder.
|
|
func (_u *SettingUpdateOne) Mutation() *SettingMutation {
|
|
return _u.mutation
|
|
}
|
|
|
|
// Where appends a list predicates to the SettingUpdate builder.
|
|
func (_u *SettingUpdateOne) Where(ps ...predicate.Setting) *SettingUpdateOne {
|
|
_u.mutation.Where(ps...)
|
|
return _u
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (_u *SettingUpdateOne) Select(field string, fields ...string) *SettingUpdateOne {
|
|
_u.fields = append([]string{field}, fields...)
|
|
return _u
|
|
}
|
|
|
|
// Save executes the query and returns the updated Setting entity.
|
|
func (_u *SettingUpdateOne) Save(ctx context.Context) (*Setting, error) {
|
|
_u.defaults()
|
|
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (_u *SettingUpdateOne) SaveX(ctx context.Context) *Setting {
|
|
node, err := _u.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (_u *SettingUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := _u.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (_u *SettingUpdateOne) ExecX(ctx context.Context) {
|
|
if err := _u.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (_u *SettingUpdateOne) defaults() {
|
|
if _, ok := _u.mutation.UpdatedAt(); !ok {
|
|
v := setting.UpdateDefaultUpdatedAt()
|
|
_u.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (_u *SettingUpdateOne) check() error {
|
|
if v, ok := _u.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_u *SettingUpdateOne) sqlSave(ctx context.Context) (_node *Setting, err error) {
|
|
if err := _u.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
|
|
id, ok := _u.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Setting.id" for update`)}
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if fields := _u.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, setting.FieldID)
|
|
for _, f := range fields {
|
|
if !setting.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != setting.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
|
}
|
|
}
|
|
}
|
|
if ps := _u.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := _u.mutation.Key(); ok {
|
|
_spec.SetField(setting.FieldKey, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.Value(); ok {
|
|
_spec.SetField(setting.FieldValue, field.TypeString, value)
|
|
}
|
|
if value, ok := _u.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
_node = &Setting{config: _u.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{setting.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
_u.mutation.done = true
|
|
return _node, nil
|
|
}
|