/
20221119131300.go
183 lines (154 loc) · 5.89 KB
/
20221119131300.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package migration
import (
_ "embed"
"encoding/json"
"time"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
//migrate20221115000000
//go:embed static_data/iosInitialSchedulingPriorities.json
var iosInitialPrioritiesFile []byte
type InitialIOSDevice struct {
DeviceID string `gorm:"primary_key" json:"deviceId"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
PublicKey string `gorm:"not null" json:"publicKey"`
ActivityToday int32 `gorm:"default:0" json:"activityToday"`
ActivityThisWeek int32 `gorm:"default:0" json:"activityThisWeek"`
ActivityThisMonth int32 `gorm:"default:0" json:"activityThisMonth"`
ActivityThisYear int32 `gorm:"default:0" json:"activityThisYear"`
}
// TableName sets the insert table name for this struct type
func (n *InitialIOSDevice) TableName() string {
return "ios_devices"
}
type InitialIOSSchedulingPriority struct {
ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"`
FromDay int `gorm:"not null" json:"from_day"`
ToDay int `gorm:"not null" json:"to_day"`
FromHour int `gorm:"not null" json:"from_hour"`
ToHour int `gorm:"not null" json:"to_hour"`
Priority int `gorm:"not null" json:"priority"`
}
// TableName sets the insert table name for this struct type
func (p *InitialIOSSchedulingPriority) TableName() string {
return "ios_scheduling_priorities"
}
// InitialIOSScheduledUpdateLog logs the last time a device was updated.
type InitialIOSScheduledUpdateLog struct {
ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"`
DeviceID string `gorm:"index:idx_scheduled_update_log_device,unique" json:"deviceId"`
Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"`
Type string `gorm:"type:enum ('grades');" json:"type"`
CreatedAt time.Time `gorm:"index:idx_scheduled_update_log_created,unique;autoCreateTime" json:"createdAt"`
}
// TableName sets the insert table name for this struct type
func (p *InitialIOSScheduledUpdateLog) TableName() string {
return "ios_scheduled_update_logs"
}
type InitialIOSDeviceRequestLog struct {
RequestID string `gorm:"primary_key;default:UUID()" json:"requestId"`
DeviceID string `gorm:"size:200;not null" json:"deviceId"`
Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"`
RequestType string `gorm:"not null;type:enum ('CAMPUS_TOKEN_REQUEST');" json:"requestType"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
}
// TableName sets the insert table name for this struct type
func (p *InitialIOSDeviceRequestLog) TableName() string {
return "ios_device_request_logs"
}
type InitialIOSEncryptedGrade struct {
ID int64 `gorm:"primaryKey"`
Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE"`
DeviceID string `gorm:"index;not null"`
LectureTitle string `gorm:"not null"`
Grade string `gorm:"not null"`
IsEncrypted bool `gorm:"not null,default:true"`
}
// TableName sets the insert table name for this struct type
func (p *InitialIOSEncryptedGrade) TableName() string {
return "ios_encrypted_grades"
}
type InitialIOSDevicesActivityReset struct {
Type string `gorm:"primary_key;type:enum('day', 'week', 'month', 'year')"`
LastReset time.Time `gorm:"autoCreateTime"`
}
// TableName sets the insert table name for this struct type
func (p *InitialIOSDevicesActivityReset) TableName() string {
return "ios_devices_activity_resets"
}
// migrate20221119131300
// - adds the ability to connect to ios devices
func migrate20221119131300() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20221119131300",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(
&InitialIOSDevice{},
&InitialIOSSchedulingPriority{},
&InitialIOSScheduledUpdateLog{},
&InitialIOSDeviceRequestLog{},
&InitialIOSEncryptedGrade{},
&InitialIOSDevicesActivityReset{},
); err != nil {
return err
}
if err := SafeEnumAdd(tx, &model.Crontab{}, "type", "iosNotifications", "iosActivityReset"); err != nil {
return err
}
var priorities []InitialIOSSchedulingPriority
if err := json.Unmarshal(iosInitialPrioritiesFile, &priorities); err != nil {
log.WithError(err).Error("could not unmarshal json")
return err
}
if err := tx.Create(&priorities).Error; err != nil {
log.WithError(err).Error("could not save priority's")
return err
}
err := tx.Create(&model.Crontab{
Interval: 60,
Type: null.StringFrom("iosNotifications"),
}).Error
if err != nil {
log.WithError(err).Error("could not create crontab")
return err
}
return tx.Create(&model.Crontab{
Type: null.StringFrom("iosActivityReset"),
Interval: 86400,
}).Error
},
Rollback: func(tx *gorm.DB) error {
if err := tx.Migrator().DropTable(&InitialIOSDevice{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&InitialIOSSchedulingPriority{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&InitialIOSScheduledUpdateLog{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&InitialIOSDeviceRequestLog{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&InitialIOSEncryptedGrade{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&InitialIOSDevicesActivityReset{}); err != nil {
return err
}
err := tx.Delete(&model.Crontab{}, "type = 'iosNotifications'").Error
if err != nil {
return err
}
err = tx.Delete(&model.Crontab{}, "type = 'iosActivityReset'").Error
if err != nil {
return err
}
return SafeEnumRemove(tx, &model.Crontab{}, "type", "iosNotifications", "iosActivityReset")
},
}
}