From 5682ad9b2bafbe1b5ddb21770778fe38570d6285 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 24 Oct 2023 00:57:12 +0200 Subject: [PATCH] Chore:stable migrations (#275) * made sure that migrate20220713000000 is stable against model changes * made sure that migrate20221119131300 is stable against model changes * made sure that migrate20221210000000 is stable against model changes * made migrate20230904100000 stable against model changes * added TableName's to migrate20221119131300 --- server/backend/migration/20220713000000.go | 296 +++++++++++++++++++-- server/backend/migration/20221119131300.go | 111 ++++++-- server/backend/migration/20221210000000.go | 39 ++- server/backend/migration/20230904100000.go | 20 +- 4 files changed, 414 insertions(+), 52 deletions(-) diff --git a/server/backend/migration/20220713000000.go b/server/backend/migration/20220713000000.go index 82a5da06..b40ef531 100644 --- a/server/backend/migration/20220713000000.go +++ b/server/backend/migration/20220713000000.go @@ -1,38 +1,294 @@ package migration import ( + "time" + "github.com/TUM-Dev/Campus-Backend/server/model" "github.com/go-gormigrate/gormigrate/v2" "gorm.io/gorm" ) -//migrate20210709193000 +// InitialCafeteria stores all Available cafeterias in the format of the eat-api +type InitialCafeteria struct { + Cafeteria int64 `gorm:"primary_key;AUTO_INCREMENT;column:cafeteria;type:int;not null;" json:"canteen" ` + Name string `gorm:"column:name;type:mediumtext;not null;" json:"name" ` + Address string `gorm:"column:address;type:text;not null;" json:"address" ` + Latitude float32 `gorm:"column:latitude;type:float;not null;" json:"latitude" ` + Longitude float32 `gorm:"column:longitude;type:float;not null;" json:"longitude"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteria) TableName() string { + return "cafeteria" +} + +// InitialCafeteriaRating stores all Available cafeterias in the format of the eat-api +type InitialCafeteriaRating struct { + CafeteriaRating int64 `gorm:"primary_key;AUTO_INCREMENT;column:cafeteriaRating;type:int;not null;" json:"canteenrating"` + Points int32 `gorm:"column:points;type:int;not null;" json:"points"` + Comment string `gorm:"column:comment;type:text;" json:"comment" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"canteenID"` + Timestamp time.Time `gorm:"column:timestamp;type:timestamp;not null;" json:"timestamp" ` + Image string `gorm:"column:image;type:text;" json:"image"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteriaRating) TableName() string { + return "cafeteria_rating" +} + +// InitialCafeteriaRatingTag struct is a row record of the either the dish_tag_rating-table or the cafeteria_rating_tags-table in the database +type InitialCafeteriaRatingTag struct { + CafeteriaRatingTag int64 `gorm:"primary_key;AUTO_INCREMENT;column:CafeteriaRatingTag;type:int;not null;" json:"CanteenRatingTag" ` + CorrespondingRating int64 `gorm:"foreignKey:cafeteriaRatingID;column:correspondingRating;type:int;not null;" json:"correspondingRating"` + Points int32 `gorm:"column:points;type:int;not null;" json:"points"` + TagID int64 `gorm:"foreignKey:cafeteriaRatingTagOption;column:tagID;type:int;not null;" json:"tagID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteriaRatingTag) TableName() string { + return "cafeteria_rating_tag" +} + +// InitialCafeteriaRatingTagOption stores all available options for tags which can be used to quickly rate cafeterias +type InitialCafeteriaRatingTagOption struct { + CafeteriaRatingsTagOption int64 `gorm:"primary_key;AUTO_INCREMENT;column:cafeteriaRatingTagOption;type:int;not null;" json:"canteenRatingTagOption"` + DE string `gorm:"column:DE;text;default:de;not null;" json:"DE"` + EN string `gorm:"column:EN;text;default:en;not null;" json:"EN"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteriaRatingTagOption) TableName() string { + return "cafeteria_rating_tag_option" +} + +// InitialDish represents one dish fin a specific cafeteria +type InitialDish struct { + Dish int64 `gorm:"primary_key;AUTO_INCREMENT;column:dish;type:int;not null;" json:"dish"` + Name string `gorm:"column:name;type:text;not null;" json:"name" ` + Type string `gorm:"column:type;type:text;not null;" json:"type" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDish) TableName() string { + return "dish" +} + +type InitialDishesOfTheWeek struct { + DishesOfTheWeek int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishesOfTheWeek;type:int;not null;" json:"dishesOfTheWeek"` + Year int32 `gorm:"column:year;type:int;not null;" json:"year"` + Week int32 `gorm:"column:week;type:int;not null;" json:"week"` + Day int32 `gorm:"column:day;type:int;not null;" json:"day"` + DishID int64 `gorm:"column:dishID;foreignKey:dish;type:int;not null;" json:"dishID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishesOfTheWeek) TableName() string { + return "dishes_of_the_week" +} + +type InitialDishNameTagOption struct { + DishNameTagOption int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagOption;type:int;not null;" json:"dishNameTagOption"` + DE string `gorm:"column:DE;type:text;not null;" json:"DE"` + EN string `gorm:"column:EN;type:text;not null;" json:"EN"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishNameTagOption) TableName() string { + return "dish_name_tag_option" +} + +type InitialDishRatingTagOption struct { + DishRatingTagOption int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRatingTagOption;type:int;not null;" json:"dishRatingTagOption"` + DE string `gorm:"column:DE;type:text;default:de;not null;" json:"DE"` + EN string `gorm:"column:EN;type:text;default:en;not null;" json:"EN"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishRatingTagOption) TableName() string { + return "dish_rating_tag_option" +} + +type InitialDishNameTag struct { + DishNameTag int64 `gorm:"primary_key;AUTO_INCREMENT;column:DishNameTag;type:int;not null;" json:"DishNameTag"` + CorrespondingRating int64 `gorm:"foreignKey:dish;column:correspondingRating;type:int;not null;" json:"correspondingRating"` + Points int32 `gorm:"column:points;type:int;not null;" json:"points"` + TagNameID int64 `gorm:"foreignKey:tagRatingID;column:tagNameID;type:int;not null;" json:"tagnameID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishNameTag) TableName() string { + return "dish_name_tag" +} + +type InitialDishNameTagOptionExcluded struct { + DishNameTagOptionExcluded int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagOptionExcluded;type:int;not null;" json:"dishNameTagOptionExcluded"` + NameTagID int64 `gorm:"foreignKey:dishNameTagOption;column:nameTagID;type:int;not null;" json:"nameTagID"` + Expression string `gorm:"column:expression;type:text;" json:"expression"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishNameTagOptionExcluded) TableName() string { + return "dish_name_tag_option_excluded" +} + +type InitialDishNameTagOptionIncluded struct { + DishNameTagOptionIncluded int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagOptionIncluded;type:int;not null;" json:"dishNameTagOptionIncluded"` + NameTagID int64 `gorm:"foreignKey:dishNameTagOption;column:nameTagID;type:int;not null;" json:"nameTagID"` + Expression string `gorm:"column:expression;type:text;" json:"expression"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishNameTagOptionIncluded) TableName() string { + return "dish_name_tag_option_included" +} + +type InitialDishRating struct { + DishRating int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRating;type:int;not null;" json:"dishRating"` + Points int32 `gorm:"column:points;type:int;not null;" json:"points"` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"` + DishID int64 `gorm:"column:dishID;foreignKey:dish;type:int;not null;" json:"dishID"` + Comment string `gorm:"column:comment;type:text;" json:"comment"` + Timestamp time.Time `gorm:"column:timestamp;type:timestamp;not null;" json:"timestamp"` + Image string `gorm:"column:image;type:text;" json:"image"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishRating) TableName() string { + return "dish_rating" +} + +type InitialDishRatingTag struct { + DishRatingTag int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRatingTag;type:int;not null;" json:"dishRatingTag"` + CorrespondingRating int64 `gorm:"foreignKey:cafeteriaRating;column:parentRating;type:int;not null;" json:"parentRating"` + Points int32 `gorm:"column:points;type:int;not null;" json:"points"` + TagID int64 `gorm:"foreignKey:dishRatingTagOption;column:tagID;type:int;not null;" json:"tagID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishRatingTag) TableName() string { + return "dish_rating_tag" +} + +type InitialDishToDishNameTag struct { + DishToDishNameTag int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishToDishNameTag;type:int;not null;" json:"dishToDishNameTag"` + DishID int64 `gorm:"column:dishID;foreignKey:dish;type:int;not null;" json:"dishID"` + NameTagID int64 `gorm:"foreignKey:dishNameTagOption;column:nameTagID;type:int;not null;" json:"nameTagID"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishToDishNameTag) TableName() string { + return "dish_to_dish_name_tag" +} + +// InitialCafeteriaRatingAverage stores all precomputed values for the cafeteria ratings +type InitialCafeteriaRatingAverage struct { + CafeteriaRatingAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:cafeteriaRatingAverage;type:int;not null;" json:"canteenRatingAverage" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"canteenID"` + Average float64 `gorm:"column:average;type:float;not null;" json:"average" ` + Min int32 `gorm:"column:min;type:int;not null;" json:"min"` + Max int32 `gorm:"column:max;type:int;not null;" json:"max"` + Std float64 `gorm:"column:std;type:float;not null;" json:"std"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteriaRatingAverage) TableName() string { + return "cafeteria_rating_average" +} + +// InitialCafeteriaRatingTagAverage stores all precomputed values for the cafeteria ratings +type InitialCafeteriaRatingTagAverage struct { + CafeteriaRatingTagsAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:cafeteriaRatingTagsAverage;type:int;not null;" json:"canteenRatingTagsAverage"` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"canteenID"` + TagID int64 `gorm:"column:tagID;foreignKey:cafeteriaRatingTagOption;type:int;not null;" json:"tagID"` + Average float32 `gorm:"column:average;type:float;not null;" json:"average"` + Min int8 `gorm:"column:min;type:int;not null;" json:"min"` + Max int8 `gorm:"column:max;type:int;not null;" json:"max"` + Std float32 `gorm:"column:std;type:float;not null;" json:"std"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialCafeteriaRatingTagAverage) TableName() string { + return "cafeteria_rating_tag_average" +} + +// InitialDishNameTagAverage stores all precomputed values for the DishName ratings +type InitialDishNameTagAverage struct { + DishNameTagAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishNameTagAverage;type:int;not null;" json:"dishNameTagAverage" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"` + TagID int64 `gorm:"column:tagID;foreignKey:DishNameTagOption;type:int;not null;" json:"tagID"` + Average float32 `gorm:"column:average;type:float;not null;" json:"average" ` + Min int8 `gorm:"column:min;type:int;not null;" json:"min"` + Max int8 `gorm:"column:max;type:int;not null;" json:"max"` + Std float32 `gorm:"column:std;type:float;not null;" json:"std"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishNameTagAverage) TableName() string { + return "dish_name_tag_average" +} + +// InitialDishRatingAverage stores all precomputed values for the cafeteria ratings +type InitialDishRatingAverage struct { + DishRatingAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRatingAverage;type:int;not null;" json:"dishRatingAverage" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"` + DishID int64 `gorm:"column:dishID;foreignKey:dish;type:int;not null;" json:"dishID"` + Average float64 `gorm:"column:average;type:float;not null;" json:"average" ` + Min int32 `gorm:"column:min;type:int;not null;" json:"min"` + Max int32 `gorm:"column:max;type:int;not null;" json:"max"` + Std float64 `gorm:"column:std;type:float;not null;" json:"std"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishRatingAverage) TableName() string { + return "dish_rating_average" +} + +// InitialDishRatingTagAverage stores all precomputed values for the cafeteria ratings +type InitialDishRatingTagAverage struct { + DishRatingTagsAverage int64 `gorm:"primary_key;AUTO_INCREMENT;column:dishRatingTagsAverage;type:int;not null;" json:"dishRatingTagsAverage" ` + CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"` + TagID int64 `gorm:"column:tagID;foreignKey:tagID;type:int;not null;" json:"tagID"` + DishID int64 `gorm:"column:dishID;foreignKey:dishID;type:int;not null;" json:"dishID"` + Average float32 `gorm:"column:average;type:float;not null;" json:"average" ` + Min int8 `gorm:"column:min;type:int;not null;" json:"min"` + Max int8 `gorm:"column:max;type:int;not null;" json:"max"` + Std float32 `gorm:"column:std;type:float;not null;" json:"std"` +} + +// TableName sets the insert table name for this struct type +func (n *InitialDishRatingTagAverage) TableName() string { + return "dish_rating_tag_average" +} +// migrate20220713000000 +// - adds all canteen related models func migrate20220713000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20220713000000", Migrate: func(tx *gorm.DB) error { if err := tx.AutoMigrate( - &model.Cafeteria{}, - &model.CafeteriaRating{}, - &model.CafeteriaRatingAverage{}, - &model.CafeteriaRatingTag{}, - &model.CafeteriaRatingTagAverage{}, - &model.CafeteriaRatingTagOption{}, - &model.Dish{}, - &model.DishesOfTheWeek{}, - &model.DishNameTagOption{}, - &model.DishNameTagOptionIncluded{}, - &model.DishNameTagOptionExcluded{}, - &model.DishNameTag{}, - &model.DishNameTagAverage{}, - &model.DishRating{}, - &model.DishRatingAverage{}, - &model.DishRatingTag{}, - &model.DishRatingTagAverage{}, - &model.DishRatingTagOption{}, - &model.DishToDishNameTag{}, + &InitialCafeteria{}, + &InitialCafeteriaRating{}, + &InitialCafeteriaRatingTag{}, + &InitialCafeteriaRatingTagOption{}, + &InitialDish{}, + &InitialDishesOfTheWeek{}, + &InitialDishNameTagOption{}, + &InitialDishNameTagOptionIncluded{}, + &InitialDishNameTagOptionExcluded{}, + &InitialDishNameTag{}, + &InitialDishRating{}, + &InitialDishRatingTag{}, + &InitialDishRatingTagOption{}, + &InitialDishToDishNameTag{}, + &InitialCafeteriaRatingAverage{}, + &InitialCafeteriaRatingTagAverage{}, + &InitialDishNameTagAverage{}, + &InitialDishRatingAverage{}, + &InitialDishRatingTagAverage{}, ); err != nil { return err } diff --git a/server/backend/migration/20221119131300.go b/server/backend/migration/20221119131300.go index fd96ba3b..5387d508 100644 --- a/server/backend/migration/20221119131300.go +++ b/server/backend/migration/20221119131300.go @@ -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" @@ -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 } @@ -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") @@ -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 } diff --git a/server/backend/migration/20221210000000.go b/server/backend/migration/20221210000000.go index 45ac4c47..9471b5aa 100644 --- a/server/backend/migration/20221210000000.go +++ b/server/backend/migration/20221210000000.go @@ -1,31 +1,44 @@ 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"), @@ -33,11 +46,15 @@ func migrate20221210000000() *gormigrate.Migration { }, 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") }, } diff --git a/server/backend/migration/20230904100000.go b/server/backend/migration/20230904100000.go index cd785772..e58b9414 100644 --- a/server/backend/migration/20230904100000.go +++ b/server/backend/migration/20230904100000.go @@ -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