From 30c348e966056116628b35c0cd08be2176b4b177 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 22 Oct 2023 14:46:16 +0200 Subject: [PATCH] refactored the migrations to not depend on the TumDBMigrator struct --- server/backend/migration/20200000000000.go | 4 +- server/backend/migration/20210709193000.go | 2 +- server/backend/migration/20220126230000.go | 2 +- server/backend/migration/20220713000000.go | 2 +- server/backend/migration/20221119131300.go | 2 +- server/backend/migration/20221210000000.go | 2 +- server/backend/migration/20230530000000.go | 2 +- server/backend/migration/20230825000000.go | 2 +- server/backend/migration/20230826000000.go | 2 +- server/backend/migration/20230904000000.go | 2 +- server/backend/migration/20230904100000.go | 2 +- server/backend/migration/20231003000000.go | 2 +- server/backend/migration/migration.go | 75 ++++++++++------------ server/utils/db.go | 2 +- 14 files changed, 49 insertions(+), 54 deletions(-) diff --git a/server/backend/migration/20200000000000.go b/server/backend/migration/20200000000000.go index 0e22ba8c..c8b7e376 100644 --- a/server/backend/migration/20200000000000.go +++ b/server/backend/migration/20200000000000.go @@ -15,8 +15,8 @@ import ( var sourceSchema string // migrate20200000000000 -// adds the source shema -func (m TumDBMigrator) migrate20200000000000() *gormigrate.Migration { +// adds the source schema +func migrate20200000000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20200000000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20210709193000.go b/server/backend/migration/20210709193000.go index 4968b74e..d18a3cb5 100644 --- a/server/backend/migration/20210709193000.go +++ b/server/backend/migration/20210709193000.go @@ -20,7 +20,7 @@ type File struct { // adds a "url" column to the database containing the url the file was downloaded from. // adds a "finished" column to the database that indicates, that a files download is finished. // adds a "fileDownload" cron job that runs every 5 minutes. -func (m TumDBMigrator) migrate20210709193000() *gormigrate.Migration { +func migrate20210709193000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20210709193000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20220126230000.go b/server/backend/migration/20220126230000.go index d6bd0f6f..da802364 100644 --- a/server/backend/migration/20220126230000.go +++ b/server/backend/migration/20220126230000.go @@ -8,7 +8,7 @@ import ( // migrate20220126230000 // adds a fulltext index to the roomfinder_rooms table -func (m TumDBMigrator) migrate20220126230000() *gormigrate.Migration { +func migrate20220126230000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20220126230000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20220713000000.go b/server/backend/migration/20220713000000.go index 0f3eadf5..3281c8bd 100644 --- a/server/backend/migration/20220713000000.go +++ b/server/backend/migration/20220713000000.go @@ -8,7 +8,7 @@ import ( //migrate20210709193000 -func (m TumDBMigrator) migrate20220713000000() *gormigrate.Migration { +func migrate20220713000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20220713000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20221119131300.go b/server/backend/migration/20221119131300.go index 8ea73cea..fd96ba3b 100644 --- a/server/backend/migration/20221119131300.go +++ b/server/backend/migration/20221119131300.go @@ -16,7 +16,7 @@ import ( //go:embed static_data/iosInitialSchedulingPriorities.json var iosInitialPrioritiesFile []byte -func (m TumDBMigrator) migrate20221119131300() *gormigrate.Migration { +func migrate20221119131300() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20221119131300", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20221210000000.go b/server/backend/migration/20221210000000.go index 9ec49d3c..45ac4c47 100644 --- a/server/backend/migration/20221210000000.go +++ b/server/backend/migration/20221210000000.go @@ -9,7 +9,7 @@ import ( // migrate20221210000000 // adds a "canteenHeadCount" cron job that runs every 5 minutes. -func (m TumDBMigrator) migrate20221210000000() *gormigrate.Migration { +func migrate20221210000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20221210000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20230530000000.go b/server/backend/migration/20230530000000.go index add100b8..9e044a70 100644 --- a/server/backend/migration/20230530000000.go +++ b/server/backend/migration/20230530000000.go @@ -26,7 +26,7 @@ type NewExamResultsSubscriber struct { LastNotifiedAt null.Time } -func (m TumDBMigrator) migrate20230530000000() *gormigrate.Migration { +func migrate20230530000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20230530000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20230825000000.go b/server/backend/migration/20230825000000.go index db35ca9d..b35630c0 100644 --- a/server/backend/migration/20230825000000.go +++ b/server/backend/migration/20230825000000.go @@ -9,7 +9,7 @@ import ( // migrate20230825000000 // Removes the ability to run chat cronjobs -func (m TumDBMigrator) migrate20230825000000() *gormigrate.Migration { +func migrate20230825000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20230825000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20230826000000.go b/server/backend/migration/20230826000000.go index 3b8fe0dc..92bb8e40 100644 --- a/server/backend/migration/20230826000000.go +++ b/server/backend/migration/20230826000000.go @@ -20,7 +20,7 @@ func (n *Feedback) TableName() string { // migrate20230826000000 // adds a "feedbackEmail" cron job that runs every 30 minutes. -func (m TumDBMigrator) migrate20230826000000() *gormigrate.Migration { +func migrate20230826000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20230826000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20230904000000.go b/server/backend/migration/20230904000000.go index 6c96cba6..aa15f2e6 100644 --- a/server/backend/migration/20230904000000.go +++ b/server/backend/migration/20230904000000.go @@ -9,7 +9,7 @@ import ( // migrate20230904000000 // Removes ticketsales from the db-enums -func (m TumDBMigrator) migrate20230904000000() *gormigrate.Migration { +func migrate20230904000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20230904000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20230904100000.go b/server/backend/migration/20230904100000.go index c744432c..d2c515b9 100644 --- a/server/backend/migration/20230904100000.go +++ b/server/backend/migration/20230904100000.go @@ -19,7 +19,7 @@ type NewsSource struct { // migrate20230904100000 // migrates the crontab from kino to movie crontab -func (m TumDBMigrator) migrate20230904100000() *gormigrate.Migration { +func migrate20230904100000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20230904100000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/20231003000000.go b/server/backend/migration/20231003000000.go index b15eea95..1c0b696f 100644 --- a/server/backend/migration/20231003000000.go +++ b/server/backend/migration/20231003000000.go @@ -84,7 +84,7 @@ var staticData embed.FS // migrate20231003000000 // migrates the static data for the canteen rating system and adds the necessary cronjob entries -func (m TumDBMigrator) migrate20231003000000() *gormigrate.Migration { +func migrate20231003000000() *gormigrate.Migration { return &gormigrate.Migration{ ID: "20231003000000", Migrate: func(tx *gorm.DB) error { diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index 2e1c19aa..3d9f14eb 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -8,33 +8,20 @@ import ( "gorm.io/gorm" ) -// TumDBMigrator contains a reference to our database -type TumDBMigrator struct { - database *gorm.DB - shouldAutoMigrate bool -} - -// New creates a new TumDBMigrator with a database -func New(db *gorm.DB, shouldAutoMigrate bool) TumDBMigrator { - return TumDBMigrator{database: db, shouldAutoMigrate: shouldAutoMigrate} +func autoMigrate(db *gorm.DB) error { + err := db.AutoMigrate( + &model.TopNews{}, + &model.Crontab{}, + &model.File{}, + &model.NewsSource{}, + &model.NewsAlert{}, + &model.News{}, + &model.CanteenHeadCount{}, + ) + return err } -// Migrate starts the migration either by using AutoMigrate in development environments or manually in prod -func (m TumDBMigrator) Migrate() error { - if m.shouldAutoMigrate { - log.Info("Using automigration") - err := m.database.AutoMigrate( - &model.TopNews{}, - &model.Crontab{}, - &model.File{}, - &model.NewsSource{}, - &model.NewsAlert{}, - &model.News{}, - &model.CanteenHeadCount{}, - ) - return err - } - log.Info("Using manual migration") +func manualMigrate(db *gorm.DB) error { gormigrateOptions := &gormigrate.Options{ TableName: gormigrate.DefaultOptions.TableName, IDColumnName: gormigrate.DefaultOptions.IDColumnName, @@ -42,21 +29,29 @@ func (m TumDBMigrator) Migrate() error { UseTransaction: true, ValidateUnknownMigrations: true, } - mig := gormigrate.New(m.database, gormigrateOptions, []*gormigrate.Migration{ - m.migrate20200000000000(), - m.migrate20210709193000(), - m.migrate20220126230000(), - m.migrate20220713000000(), - m.migrate20221119131300(), - m.migrate20221210000000(), - m.migrate20230825000000(), - m.migrate20230904000000(), - m.migrate20230530000000(), - m.migrate20230904100000(), - m.migrate20230826000000(), - m.migrate20231003000000(), + mig := gormigrate.New(db, gormigrateOptions, []*gormigrate.Migration{ + migrate20200000000000(), + migrate20210709193000(), + migrate20220126230000(), + migrate20220713000000(), + migrate20221119131300(), + migrate20221210000000(), + migrate20230825000000(), + migrate20230904000000(), + migrate20230530000000(), + migrate20230904100000(), + migrate20230826000000(), + migrate20231003000000(), }) - err := mig.Migrate() - return err + return mig.Migrate() +} +// Migrate starts the migration either by using AutoMigrate in development environments or manually in prod +func Migrate(db *gorm.DB, shouldAutoMigrate bool) error { + log.WithField("shouldAutoMigrate", shouldAutoMigrate).Debug("starting migration") + if shouldAutoMigrate { + return autoMigrate(db) + } else { + return manualMigrate(db) + } } diff --git a/server/utils/db.go b/server/utils/db.go index 3c9cb471..f89a85eb 100644 --- a/server/utils/db.go +++ b/server/utils/db.go @@ -25,7 +25,7 @@ func SetupDB() *gorm.DB { // Migrate the schema // currently not activated as - if err := migration.New(db, false).Migrate(); err != nil { + if err := migration.Migrate(db, false); err != nil { log.WithError(err).Fatal("Failed to migrate database") }