migrations.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "fmt"
  7. log "unknwon.dev/clog/v2"
  8. "xorm.io/xorm"
  9. )
  10. const minDBVersion = 19
  11. type Migration interface {
  12. Description() string
  13. Migrate(*xorm.Engine) error
  14. }
  15. type migration struct {
  16. description string
  17. migrate func(*xorm.Engine) error
  18. }
  19. func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
  20. return &migration{desc, fn}
  21. }
  22. func (m *migration) Description() string {
  23. return m.description
  24. }
  25. func (m *migration) Migrate(x *xorm.Engine) error {
  26. return m.migrate(x)
  27. }
  28. // The version table. Should have only one row with id==1
  29. type Version struct {
  30. ID int64
  31. Version int64
  32. }
  33. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  34. // If you want to "retire" a migration, remove it from the top of the list and
  35. // update _MIN_VER_DB accordingly
  36. var migrations = []Migration{
  37. // v0 -> v4 : before 0.6.0 -> last support 0.7.33
  38. // v4 -> v10: before 0.7.0 -> last support 0.9.141
  39. // v10 -> v19: before 0.11.55 -> last support 0.12.0
  40. // Add new migration here, example:
  41. // v18 -> v19:v0.11.55
  42. // NewMigration("clean unlinked webhook and hook_tasks", cleanUnlinkedWebhookAndHookTasks),
  43. }
  44. // Migrate database to current version
  45. func Migrate(x *xorm.Engine) error {
  46. if err := x.Sync(new(Version)); err != nil {
  47. return fmt.Errorf("sync: %v", err)
  48. }
  49. currentVersion := &Version{ID: 1}
  50. has, err := x.Get(currentVersion)
  51. if err != nil {
  52. return fmt.Errorf("get: %v", err)
  53. } else if !has {
  54. // If the version record does not exist we think
  55. // it is a fresh installation and we can skip all migrations.
  56. currentVersion.ID = 0
  57. currentVersion.Version = int64(minDBVersion + len(migrations))
  58. if _, err = x.InsertOne(currentVersion); err != nil {
  59. return fmt.Errorf("insert: %v", err)
  60. }
  61. }
  62. v := currentVersion.Version
  63. if minDBVersion > v {
  64. log.Fatal(`
  65. Hi there, thank you for using Gogs for so long!
  66. However, Gogs has stopped supporting auto-migration from your previously installed version.
  67. But the good news is, it's very easy to fix this problem!
  68. You can migrate your older database using a previous release, then you can upgrade to the newest version.
  69. Please save following instructions to somewhere and start working:
  70. - If you were using below 0.6.0 (e.g. 0.5.x), download last supported archive from following link:
  71. https://gogs.io/gogs/releases/tag/v0.7.33
  72. - If you were using below 0.7.0 (e.g. 0.6.x), download last supported archive from following link:
  73. https://gogs.io/gogs/releases/tag/v0.9.141
  74. - If you were using below 0.11.55 (e.g. 0.9.141), download last supported archive from following link:
  75. https://gogs.io/gogs/releases/tag/v0.12.0
  76. Once finished downloading:
  77. 1. Extract the archive and to upgrade steps as usual.
  78. 2. Run it once. To verify, you should see some migration traces.
  79. 3. Once it starts web server successfully, stop it.
  80. 4. Now it's time to put back the release archive you originally intent to upgrade.
  81. 5. Enjoy!
  82. In case you're stilling getting this notice, go through instructions again until it disappears.`)
  83. return nil
  84. }
  85. if int(v-minDBVersion) > len(migrations) {
  86. // User downgraded Gogs.
  87. currentVersion.Version = int64(len(migrations) + minDBVersion)
  88. _, err = x.Id(1).Update(currentVersion)
  89. return err
  90. }
  91. for i, m := range migrations[v-minDBVersion:] {
  92. log.Info("Migration: %s", m.Description())
  93. if err = m.Migrate(x); err != nil {
  94. return fmt.Errorf("do migrate: %v", err)
  95. }
  96. currentVersion.Version = v + int64(i) + 1
  97. if _, err = x.Id(1).Update(currentVersion); err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }