setting.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package schema
  2. import (
  3. "time"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect"
  6. "entgo.io/ent/dialect/entsql"
  7. "entgo.io/ent/schema"
  8. "entgo.io/ent/schema/field"
  9. )
  10. // Setting holds the schema definition for the Setting entity.
  11. //
  12. // 删除策略:硬删除
  13. // Setting 使用硬删除而非软删除,原因如下:
  14. // - 系统设置是简单的键值对,删除即意味着恢复默认值
  15. // - 设置变更通常通过应用日志追踪,无需在数据库层面保留历史
  16. // - 保持表结构简洁,避免无效数据积累
  17. //
  18. // 如需设置变更审计,建议在更新/删除前将变更记录写入审计日志表。
  19. type Setting struct {
  20. ent.Schema
  21. }
  22. func (Setting) Annotations() []schema.Annotation {
  23. return []schema.Annotation{
  24. entsql.Annotation{Table: "settings"},
  25. }
  26. }
  27. func (Setting) Fields() []ent.Field {
  28. return []ent.Field{
  29. field.String("key").
  30. MaxLen(100).
  31. NotEmpty().
  32. Unique(),
  33. field.String("value").
  34. SchemaType(map[string]string{
  35. dialect.Postgres: "text",
  36. }),
  37. field.Time("updated_at").
  38. Default(time.Now).
  39. UpdateDefault(time.Now).
  40. SchemaType(map[string]string{
  41. dialect.Postgres: "timestamptz",
  42. }),
  43. }
  44. }
  45. func (Setting) Indexes() []ent.Index {
  46. // key 字段已在 Fields() 中声明 Unique(),无需额外索引
  47. return nil
  48. }