Skip to content

Commit

Permalink
chore: address migration feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Oct 2, 2023
1 parent a94ac3a commit 5378f9a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 38 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 9 additions & 12 deletions migrations/202309271616_initial_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions migrations/202309271617_fix_preimage_null.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions migrations/202309271618_add_payment_sum_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions migrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 2 additions & 7 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ 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,
_202309271617_fix_preimage_null,
_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()
}

0 comments on commit 5378f9a

Please sign in to comment.