Skip to content

Commit

Permalink
fixed a migration not getting run as it build on false assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 14, 2023
1 parent 57d5b22 commit 6f39d2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions server/backend/migration/20230825000000.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package migration

import (
"database/sql"

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

Expand All @@ -12,14 +15,19 @@ func (m TumDBMigrator) migrate20230825000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20230825000000",
Migrate: func(tx *gorm.DB) error {
// deactivete the crontab (Rollback deletes this from the enum)
// given that previously, not cronjobs for this type existed there is no need to remove offending entries first
if err := tx.Delete(&model.Crontab{}, "type = 'chat'").Error; err != nil {
return err
}
return SafeEnumRollback(tx, &model.Crontab{}, "type", "chat")
},
Rollback: func(tx *gorm.DB) error {
// activete the crontab (Migrate adds this from to the enum)
// given that previously, not cronjobs for this type existed there is no need to add entries first
return SafeEnumMigrate(tx, &model.Crontab{}, "type", "chat")
if err := SafeEnumMigrate(tx, &model.Crontab{}, "type", "chat"); err != nil {
return err
}
return tx.Create(&model.Crontab{
Interval: 60 * 10, // Every 10 minutes
Type: null.String{NullString: sql.NullString{String: "chat", Valid: true}},
}).Error
},
}
}
2 changes: 1 addition & 1 deletion server/backend/migration/20230904000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (m TumDBMigrator) migrate20230904000000() *gormigrate.Migration {
ID: "20230904000000",
Migrate: func(tx *gorm.DB) error {
// remove "canteenHeadCount" in the enum
if err := tx.Where("type = ?", "ticketsales").Delete(&model.Crontab{}).Error; err != nil {
if err := tx.Delete(&model.Crontab{}, "type = 'ticketsales'").Error; err != nil {
return err
}
if err := SafeEnumRollback(tx, model.Crontab{}, "type", "ticketsales"); err != nil {
Expand Down

0 comments on commit 6f39d2f

Please sign in to comment.