-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_config.go
86 lines (74 loc) · 2.3 KB
/
app_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"entgo.io/ent/schema/mixin"
"github.com/Masterminds/semver/v3"
"github.com/nikoksr/dbench/ent/schema/datetime"
)
// AppConfig struct extends ent.Schema to describe the AppConfig database table.
type AppConfig struct {
ent.Schema
}
// AppConfigMixin is a struct with embedded mixin.Schema (a predefined structure)
type AppConfigMixin struct {
mixin.Schema
}
func validateAppVersion(v string) error {
if v == "dev" {
return nil // dev version is always valid
}
// Check if version is valid semver
_, err := semver.NewVersion(v)
return err
}
// Fields method defines the fields within the AppConfig database table.
func (AppConfigMixin) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
Positive().
Unique().
Immutable(),
field.String("version").
Validate(validateAppVersion).
Default("dev").
NotEmpty().
Comment("Version of the app itself that created this schema. We use it to check if the schema is compatible with the current version of dbench and whether a migration is required."),
field.String("initial_version").
Validate(validateAppVersion).
Optional().
Default("dev").
NotEmpty().
Comment("Version of the app itself that originally created this schema."),
}
}
// Mixin function defines the mixins to be incorporated into the AppConfig schema.
func (AppConfig) Mixin() []ent.Mixin {
return []ent.Mixin{
// The AppConfig itself
AppConfigMixin{},
// CreatedAt and UpdatedAt timestamps
datetime.NewMixin(),
}
}
// Edges function defines the relations/edges of the AppConfig schema.
func (AppConfig) Edges() []ent.Edge {
return []ent.Edge{}
}
// Indexes function defines the indexed fields for faster queries on the AppConfig schema.
func (AppConfig) Indexes() []ent.Index {
return []ent.Index{
// Index the version field for faster queries
index.Fields("version"),
}
}
// Annotations function adds annotations to the AppConfig schema.
func (AppConfig) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.WithComments(true),
schema.Comment("AppConfig holds the configuration of the application. There can only be one AppConfig in the database. It's name is always 'self'."),
}
}