1
0

migrations.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package migrations
  2. import (
  3. "github.com/pkg/errors"
  4. "gorm.io/gorm"
  5. log "unknwon.dev/clog/v2"
  6. )
  7. const minDBVersion = 19
  8. type Migration interface {
  9. Description() string
  10. Migrate(*gorm.DB) error
  11. }
  12. type migration struct {
  13. description string
  14. migrate func(*gorm.DB) error
  15. }
  16. func NewMigration(desc string, fn func(*gorm.DB) error) Migration {
  17. return &migration{desc, fn}
  18. }
  19. func (m *migration) Description() string {
  20. return m.description
  21. }
  22. func (m *migration) Migrate(db *gorm.DB) error {
  23. return m.migrate(db)
  24. }
  25. // Version represents the version table. It should have only one row with `id == 1`.
  26. type Version struct {
  27. ID int64
  28. Version int64
  29. }
  30. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  31. // If you want to "retire" a migration, remove it from the top of the list and
  32. // update _MIN_VER_DB accordingly
  33. var migrations = []Migration{
  34. // v0 -> v4 : before 0.6.0 -> last support 0.7.33
  35. // v4 -> v10: before 0.7.0 -> last support 0.9.141
  36. // v10 -> v19: before 0.11.55 -> last support 0.12.0
  37. // Add new migration here, example:
  38. // v18 -> v19:v0.11.55
  39. // NewMigration("clean unlinked webhook and hook_tasks", cleanUnlinkedWebhookAndHookTasks),
  40. // v19 -> v20:v0.13.0
  41. NewMigration("migrate access tokens to store SHA56", migrateAccessTokenToSHA256),
  42. // v20 -> v21:v0.13.0
  43. NewMigration("add index to action.user_id", addIndexToActionUserID),
  44. // v21 -> v22:v0.13.0
  45. //
  46. // NOTE: There was a bug in calculating the value of the `version.version`
  47. // column after a migration is done, thus some instances are on v21 but some are
  48. // on v22. Let's make a noop v22 to make sure every instance will not miss a
  49. // real future migration.
  50. NewMigration("noop", func(*gorm.DB) error { return nil }),
  51. }
  52. var errMigrationSkipped = errors.New("the migration has been skipped")
  53. // Migrate migrates the database schema and/or data to the current version.
  54. func Migrate(db *gorm.DB) error {
  55. // NOTE: GORM has problem migrating tables that happen to have columns with the
  56. // same name, see https://github.com/gogs/gogs/issues/7056.
  57. if !db.Migrator().HasTable(new(Version)) {
  58. err := db.AutoMigrate(new(Version))
  59. if err != nil {
  60. return errors.Wrap(err, `auto migrate "version" table`)
  61. }
  62. }
  63. var current Version
  64. err := db.Where("id = ?", 1).First(&current).Error
  65. if err == gorm.ErrRecordNotFound {
  66. err = db.Create(
  67. &Version{
  68. ID: 1,
  69. Version: int64(minDBVersion + len(migrations)),
  70. },
  71. ).Error
  72. if err != nil {
  73. return errors.Wrap(err, "create the version record")
  74. }
  75. return nil
  76. } else if err != nil {
  77. return errors.Wrap(err, "get the version record")
  78. }
  79. if minDBVersion > current.Version {
  80. log.Fatal(`
  81. Hi there, thank you for using Gogs for so long!
  82. However, Gogs has stopped supporting auto-migration from your previously installed version.
  83. But the good news is, it's very easy to fix this problem!
  84. You can migrate your older database using a previous release, then you can upgrade to the newest version.
  85. Please save following instructions to somewhere and start working:
  86. - If you were using below 0.6.0 (e.g. 0.5.x), download last supported archive from following link:
  87. https://gogs.io/gogs/releases/tag/v0.7.33
  88. - If you were using below 0.7.0 (e.g. 0.6.x), download last supported archive from following link:
  89. https://gogs.io/gogs/releases/tag/v0.9.141
  90. - If you were using below 0.11.55 (e.g. 0.9.141), download last supported archive from following link:
  91. https://gogs.io/gogs/releases/tag/v0.12.0
  92. Once finished downloading:
  93. 1. Extract the archive and to upgrade steps as usual.
  94. 2. Run it once. To verify, you should see some migration traces.
  95. 3. Once it starts web server successfully, stop it.
  96. 4. Now it's time to put back the release archive you originally intent to upgrade.
  97. 5. Enjoy!
  98. In case you're stilling getting this notice, go through instructions again until it disappears.`)
  99. return nil
  100. }
  101. if int(current.Version-minDBVersion) > len(migrations) {
  102. // User downgraded Gogs.
  103. current.Version = int64(len(migrations) + minDBVersion)
  104. return db.Where("id = ?", current.ID).Updates(current).Error
  105. }
  106. for _, m := range migrations[current.Version-minDBVersion:] {
  107. log.Info("Migration: %s", m.Description())
  108. if err = m.Migrate(db); err != nil {
  109. if err != errMigrationSkipped {
  110. return errors.Wrap(err, "do migrate")
  111. }
  112. log.Trace("The migration %q has been skipped", m.Description())
  113. }
  114. current.Version++
  115. err = db.Where("id = ?", current.ID).Updates(current).Error
  116. if err != nil {
  117. return errors.Wrap(err, "update the version record")
  118. }
  119. }
  120. return nil
  121. }