🚧 Refactor backup service
This commit is contained in:
@@ -17,6 +17,14 @@ func NowString() string {
|
||||
return time.Now().Format(TimeFormat)
|
||||
}
|
||||
|
||||
// skipAutoUpdateKey context key for skipping auto update
|
||||
type skipAutoUpdateKey struct{}
|
||||
|
||||
// SkipAutoUpdate 返回跳过自动更新 updated_at 的 context
|
||||
func SkipAutoUpdate(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, skipAutoUpdateKey{}, true)
|
||||
}
|
||||
|
||||
// TimeMixin 时间字段混入
|
||||
// created_at: 创建时间
|
||||
// updated_at: 更新时间(自动更新)
|
||||
@@ -44,6 +52,10 @@ func (TimeMixin) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
func(next ent.Mutator) ent.Mutator {
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
// 跳过自动更新(用于同步导入场景)
|
||||
if ctx.Value(skipAutoUpdateKey{}) != nil {
|
||||
return next.Mutate(ctx, m)
|
||||
}
|
||||
// 只在更新操作时设置 updated_at
|
||||
if m.Op().Is(ent.OpUpdate | ent.OpUpdateOne) {
|
||||
if setter, ok := m.(interface{ SetUpdatedAt(string) }); ok {
|
||||
|
||||
36
internal/models/schema/mixin/uuid.go
Normal file
36
internal/models/schema/mixin/uuid.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// UUIDMixin 添加 UUID 字段用于跨设备同步
|
||||
// 使用 UUIDv7,具有时间有序性,索引性能更好
|
||||
type UUIDMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
// Fields of the UUIDMixin.
|
||||
func (UUIDMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("uuid").
|
||||
DefaultFunc(func() string {
|
||||
return uuid.Must(uuid.NewV7()).String()
|
||||
}).
|
||||
Unique().
|
||||
Immutable().
|
||||
StructTag(`json:"uuid"`).
|
||||
Comment("UUID for cross-device sync (UUIDv7)"),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the UUIDMixin.
|
||||
func (UUIDMixin) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("uuid"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user