orm: correct behavior of created_at, updated_at, define immutable fields

This commit is contained in:
marco 2024-03-05 15:28:50 +01:00
parent 05b54687b6
commit 142108529d
8 changed files with 19 additions and 19 deletions

View file

@ -19,10 +19,10 @@ func (Alert) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), Immutable(),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow),
field.String("scenario"), field.String("scenario"),
field.String("bucketId").Default("").Optional(), field.String("bucketId").Default("").Optional(),
field.String("message").Default("").Optional(), field.String("message").Default("").Optional(),

View file

@ -16,10 +16,10 @@ func (Bouncer) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional().StructTag(`json:"created_at"`), StructTag(`json:"created_at"`),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional().StructTag(`json:"updated_at"`), UpdateDefault(types.UtcNow).StructTag(`json:"updated_at"`),
field.String("name").Unique().StructTag(`json:"name"`), field.String("name").Unique().StructTag(`json:"name"`),
field.String("api_key").Sensitive(), // hash of api_key field.String("api_key").Sensitive(), // hash of api_key
field.Bool("revoked").StructTag(`json:"revoked"`), field.Bool("revoked").StructTag(`json:"revoked"`),

View file

@ -11,21 +11,20 @@ type ConfigItem struct {
ent.Schema ent.Schema
} }
// Fields of the Bouncer.
func (ConfigItem) Fields() []ent.Field { func (ConfigItem) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional().StructTag(`json:"created_at"`), Immutable().
StructTag(`json:"created_at"`),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional().StructTag(`json:"updated_at"`), UpdateDefault(types.UtcNow).StructTag(`json:"updated_at"`),
field.String("name").Unique().StructTag(`json:"name"`), field.String("name").Unique().StructTag(`json:"name"`),
field.String("value").StructTag(`json:"value"`), // a json object field.String("value").StructTag(`json:"value"`), // a json object
} }
} }
// Edges of the Bouncer.
func (ConfigItem) Edges() []ent.Edge { func (ConfigItem) Edges() []ent.Edge {
return nil return nil
} }

View file

@ -19,10 +19,10 @@ func (Decision) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), Immutable(),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow),
field.Time("until").Nillable().Optional().SchemaType(map[string]string{ field.Time("until").Nillable().Optional().SchemaType(map[string]string{
dialect.MySQL: "datetime", dialect.MySQL: "datetime",
}), }),

View file

@ -18,10 +18,10 @@ func (Event) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), Immutable(),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow),
field.Time("time"), field.Time("time"),
field.String("serialized").MaxLen(8191), field.String("serialized").MaxLen(8191),
field.Int("alert_events").Optional(), field.Int("alert_events").Optional(),

View file

@ -12,7 +12,7 @@ type Lock struct {
func (Lock) Fields() []ent.Field { func (Lock) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.String("name").Unique().StructTag(`json:"name"`), field.String("name").Unique().Immutable().StructTag(`json:"name"`),
field.Time("created_at").Default(types.UtcNow).StructTag(`json:"created_at"`), field.Time("created_at").Default(types.UtcNow).StructTag(`json:"created_at"`),
} }
} }

View file

@ -17,17 +17,19 @@ func (Machine) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), Immutable(),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow),
field.Time("last_push"). field.Time("last_push").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow).Nillable().Optional(),
field.Time("last_heartbeat"). field.Time("last_heartbeat").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow).Nillable().Optional(),
field.String("machineId").Unique(), field.String("machineId").
Unique().
Immutable(),
field.String("password").Sensitive(), field.String("password").Sensitive(),
field.String("ipAddress"), field.String("ipAddress"),
field.String("scenarios").MaxLen(100000).Optional(), field.String("scenarios").MaxLen(100000).Optional(),

View file

@ -17,11 +17,10 @@ type Meta struct {
func (Meta) Fields() []ent.Field { func (Meta) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Time("created_at"). field.Time("created_at").
Default(types.UtcNow). Default(types.UtcNow),
UpdateDefault(types.UtcNow).Nillable().Optional(),
field.Time("updated_at"). field.Time("updated_at").
Default(types.UtcNow). Default(types.UtcNow).
UpdateDefault(types.UtcNow).Nillable().Optional(), UpdateDefault(types.UtcNow),
field.String("key"), field.String("key"),
field.String("value").MaxLen(4095), field.String("value").MaxLen(4095),
field.Int("alert_metas").Optional(), field.Int("alert_metas").Optional(),