Skip to content

Commit

Permalink
Merge branch 'main' into chore/movie-db
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 23, 2023
2 parents d30d66d + 5682ad9 commit a129f0d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 32 deletions.
111 changes: 96 additions & 15 deletions server/backend/migration/20221119131300.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migration
import (
_ "embed"
"encoding/json"
"time"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
Expand All @@ -16,19 +17,99 @@ import (
//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(
&model.IOSDevice{},
&model.Crontab{},
&model.IOSSchedulingPriority{},
&model.IOSScheduledUpdateLog{},
&model.IOSDeviceRequestLog{},
&model.IOSEncryptedGrade{},
&model.IOSDevicesActivityReset{},
&InitialIOSDevice{},
&InitialIOSSchedulingPriority{},
&InitialIOSScheduledUpdateLog{},
&InitialIOSDeviceRequestLog{},
&InitialIOSEncryptedGrade{},
&InitialIOSDevicesActivityReset{},
); err != nil {
return err
}
Expand All @@ -37,7 +118,7 @@ func migrate20221119131300() *gormigrate.Migration {
return err
}

var priorities []model.IOSSchedulingPriority
var priorities []InitialIOSSchedulingPriority

if err := json.Unmarshal(iosInitialPrioritiesFile, &priorities); err != nil {
log.WithError(err).Error("could not unmarshal json")
Expand Down Expand Up @@ -66,22 +147,22 @@ func migrate20221119131300() *gormigrate.Migration {
},

Rollback: func(tx *gorm.DB) error {
if err := tx.Migrator().DropTable(&model.IOSDevice{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSDevice{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&model.IOSSchedulingPriority{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSSchedulingPriority{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&model.IOSScheduledUpdateLog{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSScheduledUpdateLog{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&model.IOSDeviceRequestLog{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSDeviceRequestLog{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&model.IOSEncryptedGrade{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSEncryptedGrade{}); err != nil {
return err
}
if err := tx.Migrator().DropTable(&model.IOSDevicesActivityReset{}); err != nil {
if err := tx.Migrator().DropTable(&InitialIOSDevicesActivityReset{}); err != nil {
return err
}

Expand Down
39 changes: 28 additions & 11 deletions server/backend/migration/20221210000000.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
package migration

import (
"time"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
"gorm.io/gorm"
)

// InitialCanteenHeadCount stores all available people counts for available canteens. The CanteenId represents the same ID, as for the canteen inside the eat-api.
type InitialCanteenHeadCount struct {
CanteenId string `gorm:"primary_key;column:canteen_id;type:varchar(64);not null;" json:"canteen_id"`
Count uint32 `gorm:"column:count;type:int;not null;" json:"count"`
MaxCount uint32 `gorm:"column:max_count;type:int;not null;" json:"max_count"`
Percent float32 `gorm:"column:percent;type:float;not null;" json:"percent"`
Timestamp time.Time `gorm:"column:timestamp;type:timestamp;not null;" json:"timestamp" `
}

// TableName sets the insert table name for this struct type
func (n *InitialCanteenHeadCount) TableName() string {
return "canteen_head_count"
}

// migrate20221210000000
// adds a "canteenHeadCount" cron job that runs every 5 minutes.
// - adds InitialCanteenHeadCount table
// - adds a "canteenHeadCount" cron job that runs every 5 minutes.
func migrate20221210000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20221210000000",
Migrate: func(tx *gorm.DB) error {

err := tx.AutoMigrate(
&model.CanteenHeadCount{},
)
if err != nil {
// table
if err := tx.AutoMigrate(&InitialCanteenHeadCount{}); err != nil {
return err
}

// allow "canteenHeadCount" in the enum
// cron
if err := SafeEnumAdd(tx, model.Crontab{}, "type", "canteenHeadCount"); err != nil {
return err
}

return tx.Create(&model.Crontab{
Interval: 60 * 5, // Every 5 minutes
Type: null.StringFrom("canteenHeadCount"),
}).Error
},

Rollback: func(tx *gorm.DB) error {
err := tx.Delete(&model.Crontab{}, "type = 'canteenHeadCount'").Error
if err != nil {
// table
if err := tx.Migrator().DropTable(&InitialCanteenHeadCount{}); err != nil {
return err
}

// cron
if err := tx.Delete(&model.Crontab{}, "type = 'canteenHeadCount'").Error; err != nil {
return err
}
// Remove the 'canteenHeadCount' from the enum
return SafeEnumRemove(tx, model.Crontab{}, "type", "canteenHeadCount")
},
}
Expand Down
20 changes: 14 additions & 6 deletions server/backend/migration/20230904100000.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import (
"gorm.io/gorm"
)

type NewsSourceFile struct {
File int64 `gorm:"primary_key;AUTO_INCREMENT;column:file;type:int;"`
}

func (n *NewsSourceFile) TableName() string {
return "files"
}

// NewsSource struct is a row record of the newsSource table in the tca database
type NewsSource struct {
Source int64 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"`
Title string `gorm:"column:title;type:text;size:16777215;"`
URL null.String `gorm:"column:url;type:text;size:16777215;"`
FileID int64 `gorm:"column:icon;not null;type:int;"`
File model.File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Hook null.String `gorm:"column:hook;type:char;size:12;"`
Source int64 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"`
Title string `gorm:"column:title;type:text;size:16777215;"`
URL null.String `gorm:"column:url;type:text;size:16777215;"`
FileID int64 `gorm:"column:icon;not null;type:int;"`
File NewsSourceFile `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Hook null.String `gorm:"column:hook;type:char;size:12;"`
}

// TableName sets the insert table name for this struct type
Expand Down

0 comments on commit a129f0d

Please sign in to comment.