migrations.go 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package migrations
  2. import (
  3. "errors"
  4. "github.com/go-xorm/xorm"
  5. )
  6. type migration func(*xorm.Engine) error
  7. // The version table. Should have only one row with id==1
  8. type Version struct {
  9. Id int64 `xorm:"pk"`
  10. Version int64
  11. }
  12. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  13. // If you want to "retire" a migration, replace it with "expiredMigration"
  14. var migrations = []migration{}
  15. // Migrate database to current version
  16. func Migrate(x *xorm.Engine) error {
  17. x.Sync(new(Version))
  18. currentVersion := &Version{Id: 1}
  19. has, err := x.Get(currentVersion)
  20. if err != nil {
  21. return err
  22. }
  23. if !has {
  24. _, err = x.InsertOne(currentVersion)
  25. }
  26. v := currentVersion.Version
  27. for i, migration := range migrations[v:] {
  28. if err = migration(x); err != nil {
  29. return err
  30. }
  31. currentVersion.Version = v + int64(i) + 1
  32. x.Id(1).Update(currentVersion)
  33. }
  34. return nil
  35. }
  36. func expiredMigration(x *xorm.Engine) error {
  37. return errors.New("You are migrating from a too old gogs version")
  38. }