diff --git a/main.go b/main.go index de7addd0..1904741f 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,11 @@ func main() { sqlDb.SetMaxIdleConns(cfg.DatabaseMaxIdleConns) sqlDb.SetConnMaxLifetime(time.Duration(cfg.DatabaseConnMaxLifetime) * time.Second) - migrations.Migrate(db) + err = migrations.Migrate(db) + if err != nil { + log.Fatalf("Migration failed: %v", err) + } + log.Println("Any pending migrations ran successfully") if cfg.NostrSecretKey == "" { if cfg.LNBackendType == AlbyBackendType { diff --git a/migrations/202309271616_initial_migration.go b/migrations/202309271616_initial_migration.go index e940acd5..823c2547 100644 --- a/migrations/202309271616_initial_migration.go +++ b/migrations/202309271616_initial_migration.go @@ -13,28 +13,25 @@ var initialMigrationPostgres string //go:embed initial_migration_sqlite.sql var initialMigrationSqlite string +var initialMigrations = map[string]string { + "postgres": initialMigrationPostgres, + "sqlite": initialMigrationSqlite, +} + // Initial migration var _202309271616_initial_migration = &gormigrate.Migration { ID: "202309271616_initial_migration", Migrate: func(tx *gorm.DB) error { - // only execute migration if apps table doesn't exist - err := tx.Exec("Select * from apps").Error; + err := tx.Exec("SELECT * FROM apps").Error; if err != nil { // find which initial migration should be executed - var initialMigration string - if tx.Dialector.Name() == "postgres" { - initialMigration = initialMigrationPostgres - } else if tx.Dialector.Name() == "sqlite" { - initialMigration = initialMigrationSqlite - } else { + initialMigration := initialMigrations[tx.Dialector.Name()] + if initialMigration == "" { log.Fatalf("unsupported database type: %s", tx.Dialector.Name()) } - err := tx.Exec(initialMigration).Error - if err != nil { - return err - } + return tx.Exec(initialMigration).Error } return nil diff --git a/migrations/202309271617_fix_preimage_null.go b/migrations/202309271617_fix_preimage_null.go index facb8e1b..a1d38227 100644 --- a/migrations/202309271617_fix_preimage_null.go +++ b/migrations/202309271617_fix_preimage_null.go @@ -9,12 +9,7 @@ import ( var _202309271617_fix_preimage_null = &gormigrate.Migration { ID: "202309271617_fix_preimage_null", Migrate: func(tx *gorm.DB) error { - err := tx.Table("payments").Where("preimage = ?", "").Update("preimage", nil).Error; - - if err != nil { - return err - } - return nil + return tx.Table("payments").Where("preimage = ?", "").Update("preimage", nil).Error; }, Rollback: func(tx *gorm.DB) error { return nil; diff --git a/migrations/202309271618_add_payment_sum_index.go b/migrations/202309271618_add_payment_sum_index.go index c61afcc6..e35be36a 100644 --- a/migrations/202309271618_add_payment_sum_index.go +++ b/migrations/202309271618_add_payment_sum_index.go @@ -21,12 +21,7 @@ var _202309271618_add_payment_sum_index = &gormigrate.Migration { log.Fatalf("unsupported database type: %s", tx.Dialector.Name()) } - err := tx.Exec(sql).Error - if err != nil { - return err - } - - return nil + return tx.Exec(sql).Error }, Rollback: func(tx *gorm.DB) error { return nil; diff --git a/migrations/README.md b/migrations/README.md index 4c3f2c1c..5156ec99 100644 --- a/migrations/README.md +++ b/migrations/README.md @@ -19,12 +19,7 @@ import ( var _MY_ID_HERE = &gormigrate.Migration { ID: "MY_ID_HERE", Migrate: func(tx *gorm.DB) error { - err := DO_SOMETHING_HERE.Error; - - if err != nil { - return err - } - return nil + return DO_SOMETHING_HERE.Error; }, Rollback: func(tx *gorm.DB) error { return nil; diff --git a/migrations/migrate.go b/migrations/migrate.go index 44df1bb0..a3281a89 100644 --- a/migrations/migrate.go +++ b/migrations/migrate.go @@ -2,11 +2,10 @@ package migrations import ( "github.com/go-gormigrate/gormigrate/v2" - log "github.com/sirupsen/logrus" "gorm.io/gorm" ) -func Migrate(db *gorm.DB) { +func Migrate(db *gorm.DB) error { m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ _202309271616_initial_migration, @@ -14,9 +13,5 @@ func Migrate(db *gorm.DB) { _202309271618_add_payment_sum_index, }) - err := m.Migrate() - if err != nil { - log.Fatalf("Migration failed: %v", err) - } - log.Println("Any pending migrations ran successfully") + return m.Migrate() } \ No newline at end of file