From 1bd3a7bb9a1dd8ca8dcd903c5b447c3c9908172d Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 22 Oct 2023 21:02:55 +0200 Subject: [PATCH] added information how long migrations take as debug logs --- server/backend/migration/migration.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index 38cd58ce..420652b7 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -6,6 +6,7 @@ import ( "github.com/go-gormigrate/gormigrate/v2" log "github.com/sirupsen/logrus" "gorm.io/gorm" + "time" ) func autoMigrate(db *gorm.DB) error { @@ -28,7 +29,7 @@ func manualMigrate(db *gorm.DB) error { UseTransaction: true, ValidateUnknownMigrations: true, } - mig := gormigrate.New(db, gormigrateOptions, []*gormigrate.Migration{ + migrations := []*gormigrate.Migration{ migrate20200000000000(), migrate20210709193000(), migrate20220126230000(), @@ -41,16 +42,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 }