migrations.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "github.com/pkg/errors"
  7. "gorm.io/gorm"
  8. log "unknwon.dev/clog/v2"
  9. )
  10. const minDBVersion = 19
  11. type Migration interface {
  12. Description() string
  13. Migrate(*gorm.DB) error
  14. }
  15. type migration struct {
  16. description string
  17. migrate func(*gorm.DB) error
  18. }
  19. func NewMigration(desc string, fn func(*gorm.DB) error) Migration {
  20. return &migration{desc, fn}
  21. }
  22. func (m *migration) Description() string {
  23. return m.description
  24. }
  25. func (m *migration) Migrate(db *gorm.DB) error {
  26. return m.migrate(db)
  27. }
  28. // Version represents the version table. It 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. // v19 -> v20:v0.13.0
  44. NewMigration("migrate access tokens to store SHA56", migrateAccessTokenToSHA256),
  45. // v20 -> v21:v0.13.0
  46. NewMigration("add index to action.user_id", addIndexToActionUserID),
  47. }
  48. // Migrate migrates the database schema and/or data to the current version.
  49. func Migrate(db *gorm.DB) error {
  50. // NOTE: GORM has problem migrating tables that happen to have columns with the
  51. // same name, see https://github.com/gogs/gogs/issues/7056.
  52. if !db.Migrator().HasTable(new(Version)) {
  53. err := db.AutoMigrate(new(Version))
  54. if err != nil {
  55. return errors.Wrap(err, `auto migrate "version" table`)
  56. }
  57. }
  58. var current Version
  59. err := db.Where("id = ?", 1).First(&current).Error
  60. if err == gorm.ErrRecordNotFound {
  61. err = db.Create(
  62. &Version{
  63. ID: 1,
  64. Version: int64(minDBVersion + len(migrations)),
  65. },
  66. ).Error
  67. if err != nil {
  68. return errors.Wrap(err, "create the version record")
  69. }
  70. return nil
  71. } else if err != nil {
  72. return errors.Wrap(err, "get the version record")
  73. }
  74. if minDBVersion > current.Version {
  75. log.Fatal(`
  76. Hi there, thank you for using Gogs for so long!
  77. However, Gogs has stopped supporting auto-migration from your previously installed version.
  78. But the good news is, it's very easy to fix this problem!
  79. You can migrate your older database using a previous release, then you can upgrade to the newest version.
  80. Please save following instructions to somewhere and start working:
  81. - If you were using below 0.6.0 (e.g. 0.5.x), download last supported archive from following link:
  82. https://gogs.io/gogs/releases/tag/v0.7.33
  83. - If you were using below 0.7.0 (e.g. 0.6.x), download last supported archive from following link:
  84. https://gogs.io/gogs/releases/tag/v0.9.141
  85. - If you were using below 0.11.55 (e.g. 0.9.141), download last supported archive from following link:
  86. https://gogs.io/gogs/releases/tag/v0.12.0
  87. Once finished downloading:
  88. 1. Extract the archive and to upgrade steps as usual.
  89. 2. Run it once. To verify, you should see some migration traces.
  90. 3. Once it starts web server successfully, stop it.
  91. 4. Now it's time to put back the release archive you originally intent to upgrade.
  92. 5. Enjoy!
  93. In case you're stilling getting this notice, go through instructions again until it disappears.`)
  94. return nil
  95. }
  96. if int(current.Version-minDBVersion) > len(migrations) {
  97. // User downgraded Gogs.
  98. current.Version = int64(len(migrations) + minDBVersion)
  99. return db.Where("id = ?", current.ID).Updates(current).Error
  100. }
  101. for i, m := range migrations[current.Version-minDBVersion:] {
  102. log.Info("Migration: %s", m.Description())
  103. if err = m.Migrate(db); err != nil {
  104. return errors.Wrap(err, "do migrate")
  105. }
  106. current.Version += int64(i) + 1
  107. err = db.Where("id = ?", current.ID).Updates(current).Error
  108. if err != nil {
  109. return errors.Wrap(err, "update the version record")
  110. }
  111. }
  112. return nil
  113. }