Skip to content

Commit

Permalink
Deduplicate some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 27, 2024
1 parent 4ddea70 commit 4c8061c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions cmd/migrate_db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ func down() error {
}

logger.Info("Running migration: %s", name)

queryBytes, err := content.ReadFile(fmt.Sprintf("migrations/%s", name))
err = runMigration(name, migrationIndex)

if err != nil {
_ = setMigrationState(migrationIndex, true)
return err
}

_, err = database.DB.Exec(string(queryBytes))

if err != nil {
_ = setMigrationState(migrationIndex, true)
return err
}
}
Expand Down Expand Up @@ -117,18 +108,9 @@ func up() error {
}

logger.Info("Running migration: %s", name)

queryBytes, err := content.ReadFile(fmt.Sprintf("migrations/%s", name))
err = runMigration(name, migrationIndex)

if err != nil {
_ = setMigrationState(migrationIndex, true)
return err
}

_, err = database.DB.Exec(string(queryBytes))

if err != nil {
_ = setMigrationState(migrationIndex, true)
return err
}
}
Expand Down Expand Up @@ -212,3 +194,21 @@ func setMigrationState(version int, dirty bool) error {

return nil
}

func runMigration(filename string, index int) error {
queryBytes, err := content.ReadFile(fmt.Sprintf("migrations/%s", filename))

if err != nil {
_ = setMigrationState(index, true)
return err
}

_, err = database.DB.Exec(string(queryBytes))

if err != nil {
_ = setMigrationState(index, true)
return err
}

return nil
}

0 comments on commit 4c8061c

Please sign in to comment.