forked from c-bata/goptuna
/
model.go
164 lines (140 loc) · 5.78 KB
/
model.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package rdb
import (
"time"
"gorm.io/gorm"
)
const (
directionMinimize = "MINIMIZE"
directionMaximize = "MAXIMIZE"
)
const (
trialStateRunning = "RUNNING"
trialStateComplete = "COMPLETE"
trialStatePruned = "PRUNED"
trialStateFail = "FAIL"
trialStateWaiting = "WAITING"
)
// https://gorm.io/docs/models.html
type studyModel struct {
ID int `gorm:"column:study_id;primaryKey"`
Name string `gorm:"column:study_name;type:varchar(512);unique;not null"`
Direction string `gorm:"column:direction;not null"`
// Associations
UserAttributes []studyUserAttributeModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:UserAttributeReferStudy;association_foreignkey:ID"`
SystemAttributes []studySystemAttributeModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:SystemAttributeReferStudy;association_foreignkey:ID"`
Trials []trialModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:TrialReferStudy;association_foreignkey:ID"`
}
func (m studyModel) TableName() string {
return "studies"
}
type studyUserAttributeModel struct {
ID int `gorm:"column:study_user_attribute_id;primaryKey"`
UserAttributeReferStudy int `gorm:"column:study_id;uniqueIndex:idx_study_user_attr_key"`
Key string `gorm:"column:key;uniqueIndex:idx_study_user_attr_key;type:varchar(512)"`
Value string `gorm:"column:value;type:varchar(2048)"`
}
func (m studyUserAttributeModel) TableName() string {
return "study_user_attributes"
}
type studySystemAttributeModel struct {
ID int `gorm:"column:study_system_attribute_id;primaryKey"`
SystemAttributeReferStudy int `gorm:"column:study_id;uniqueIndex:idx_study_system_attr_key"`
Key string `gorm:"column:key;uniqueIndex:idx_study_system_attr_key;type:varchar(512)"`
Value string `gorm:"column:value;type:varchar(2048)"`
}
func (m studySystemAttributeModel) TableName() string {
return "study_system_attributes"
}
type trialModel struct {
ID int `gorm:"column:trial_id;primaryKey"`
Number int `gorm:"column:number"`
TrialReferStudy int `gorm:"column:study_id"`
State string `gorm:"column:state;not null"`
Value float64 `gorm:"column:value"`
DatetimeStart *time.Time `gorm:"column:datetime_start"`
DatetimeComplete *time.Time `gorm:"column:datetime_complete"`
// Associations
UserAttributes []trialUserAttributeModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:UserAttributeReferTrial;association_foreignkey:ID"`
SystemAttributes []trialSystemAttributeModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:SystemAttributeReferTrial;association_foreignkey:ID"`
TrialParams []trialParamModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:TrialParamReferTrial;association_foreignkey:ID"`
TrialValues []trialValueModel `gorm:"Constraint:OnDelete:CASCADE;foreignkey:TrialValueReferTrial;association_foreignkey:ID"`
}
func (m trialModel) TableName() string {
return "trials"
}
type trialUserAttributeModel struct {
ID int `gorm:"column:trial_user_attribute_id;primaryKey"`
UserAttributeReferTrial int `gorm:"column:trial_id;uniqueIndex:idx_trial_user_attr_key"`
Key string `gorm:"column:key;uniqueIndex:idx_trial_user_attr_key;type:varchar(512)"`
Value string `gorm:"column:value;type:varchar(2048)"`
}
func (m trialUserAttributeModel) TableName() string {
return "trial_user_attributes"
}
type trialSystemAttributeModel struct {
ID int `gorm:"column:trial_system_attribute_id;primaryKey"`
SystemAttributeReferTrial int `gorm:"column:trial_id;uniqueIndex:idx_trial_system_attr_key"`
Key string `gorm:"column:key;uniqueIndex:idx_trial_system_attr_key;type:varchar(512)"`
Value string `gorm:"column:value;type:varchar(2048)"`
}
func (m trialSystemAttributeModel) TableName() string {
return "trial_system_attributes"
}
type trialParamModel struct {
ID int `gorm:"column:param_id;primaryKey"`
TrialParamReferTrial int `gorm:"column:trial_id;uniqueIndex:idx_trial_param_name"`
Name string `gorm:"column:param_name;uniqueIndex:idx_trial_param_name;type:varchar(512)"`
Value float64 `gorm:"column:param_value"`
DistributionJSON string `gorm:"column:distribution_json;type:varchar(2048)"`
}
func (m trialParamModel) TableName() string {
return "trial_params"
}
type trialValueModel struct {
ID int `gorm:"column:trial_value_id;primaryKey"`
TrialValueReferTrial int `gorm:"column:trial_id;uniqueIndex:idx_trial_value_step"`
Step int `gorm:"column:step;uniqueIndex:idx_trial_value_step"`
Value float64 `gorm:"column:value"`
}
func (m trialValueModel) TableName() string {
return "trial_values"
}
// RunAutoMigrate runs Auto-Migration. This will ONLY create tables,
// missing columns and missing indexes, and WON’T change existing
// column’s type or delete unused columns to protect your data.
func RunAutoMigrate(db *gorm.DB) error {
var err error
err = db.AutoMigrate(&studyModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&studyUserAttributeModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&studySystemAttributeModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&trialModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&trialUserAttributeModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&trialSystemAttributeModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&trialParamModel{})
if err != nil {
return err
}
err = db.AutoMigrate(&trialValueModel{})
if err != nil {
return err
}
return nil
}