Skip to content

Commit

Permalink
refactored the migrations to not depend on the TumDBMigrator struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 22, 2023
1 parent 592ee91 commit 30c348e
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 54 deletions.
4 changes: 2 additions & 2 deletions server/backend/migration/20200000000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20210709193000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20220126230000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20220713000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20221119131300.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20221210000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20230530000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20230825000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20230826000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20230904000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20230904100000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/backend/migration/20231003000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
75 changes: 35 additions & 40 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,50 @@ 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,
IDColumnSize: gormigrate.DefaultOptions.IDColumnSize,
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)
}
}
2 changes: 1 addition & 1 deletion server/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit 30c348e

Please sign in to comment.