Skip to content

Commit

Permalink
Merge branch 'main' into chore/full-migrations-test
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/main.yml
#	server/backend/migration/20200000000000.go
#	server/backend/migration/migration.go
#	server/utils/db.go
  • Loading branch information
CommanderStorm committed Oct 22, 2023
2 parents 986adaf + a59f827 commit 509b32c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions server/backend/migration/20200000000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migration

import (
_ "embed"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -34,9 +35,10 @@ func migrate20200000000000() *gormigrate.Migration {
return nil
},
Rollback: func(tx *gorm.DB) error {
tables := []string{"actions", "alarm_ban", "alarm_log", "barrierFree_moreInfo", "barrierFree_persons", "card_type", "chat_room", "crontab", "curricula", "dish", "dishflags", "dish2dishflags", "faculty", "feedback", "files", "kino", "lecture", "location", "member", "card", "card_box", "card_comment", "card_option", "chat_message", "chat_room2members", "devices", "device2stats", "members_card", "members_card_answer_history", "mensa", "dish2mensa", "mensaplan_mensa", "mensaprices", "migrations", "newsSource", "news", "news_alert", "notification_type", "notification", "notification_confirmation", "openinghours", "question", "question2answer", "question2faculty", "questionAnswers", "reports", "rights", "menu", "modules", "roles", "roles2rights", "roomfinder_building2area", "roomfinder_buildings", "roomfinder_buildings2gps", "roomfinder_buildings2maps", "roomfinder_maps", "roomfinder_rooms", "roomfinder_rooms2maps", "roomfinder_schedules", "sessions", "tag", "card2tag", "ticket_admin", "ticket_group", "event", "ticket_admin2group", "ticket_payment", "ticket_type", "ticket_history", "update_note", "users", "log", "recover", "users2info", "users2roles", "wifi_measurement"}
re := regexp.MustCompile(`create table if not exists (?P<table_name>\S+)`)
tables := re.FindAllStringSubmatch(sourceSchema, -1)
for _, table := range tables {
if err := tx.Migrator().DropTable(table); err != nil {
if err := tx.Migrator().DropTable(table[re.SubexpIndex("table_name")]); err != nil {
return err
}
}
Expand Down
18 changes: 12 additions & 6 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package migration

import (
"time"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -64,7 +66,7 @@ func manualMigrate(db *gorm.DB) error {
UseTransaction: true,
ValidateUnknownMigrations: true,
}
mig := gormigrate.New(db, gormigrateOptions, []*gormigrate.Migration{
migrations := []*gormigrate.Migration{
migrate20200000000000(),
migrate20210709193000(),
migrate20220126230000(),
Expand All @@ -77,16 +79,20 @@ func manualMigrate(db *gorm.DB) error {
migrate20230904100000(),
migrate20230826000000(),
migrate20231003000000(),
})
return mig.Migrate()
}
return gormigrate.New(db, gormigrateOptions, migrations).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")
log.WithField("shouldAutoMigrate", shouldAutoMigrate).Info("starting migration")
start := time.Now()
var err error
if shouldAutoMigrate {
return autoMigrate(db)
err = autoMigrate(db)
} else {
return manualMigrate(db)
err = manualMigrate(db)
}
log.WithField("elapsed", time.Since(start)).Info("migration done")
return err
}

0 comments on commit 509b32c

Please sign in to comment.