000000000.go 587 B

1234567891011121314151617181920212223242526272829303132
  1. package migrate
  2. import (
  3. "context"
  4. "fmt"
  5. "golang-fave/engine/sqlw"
  6. "golang-fave/engine/utils"
  7. )
  8. func Run(ctx context.Context, db *sqlw.DB, version int, host string) error {
  9. var last string
  10. for i, fn := range Migrations {
  11. if utils.StrToInt(i) > 1 {
  12. if version < utils.StrToInt(i) {
  13. last = i
  14. if fn != nil {
  15. fn(ctx, db, host)
  16. fmt.Printf("Migrated %s: %s\n", host, i)
  17. }
  18. }
  19. }
  20. }
  21. if last != "" {
  22. if _, err := db.Exec(ctx, `UPDATE fave_settings SET value = ? WHERE name = 'database_version';`, last); err != nil {
  23. return err
  24. }
  25. }
  26. return nil
  27. }