Migrate migrates the database schema and/or data to the current version.
(db *gorm.DB)
| 65 | |
| 66 | // Migrate migrates the database schema and/or data to the current version. |
| 67 | func Migrate(db *gorm.DB) error { |
| 68 | // NOTE: GORM has problem migrating tables that happen to have columns with the |
| 69 | // same name, see https://github.com/gogs/gogs/issues/7056. |
| 70 | if !db.Migrator().HasTable(new(Version)) { |
| 71 | err := db.AutoMigrate(new(Version)) |
| 72 | if err != nil { |
| 73 | return errors.Wrap(err, `auto migrate "version" table`) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | var current Version |
| 78 | err := db.Where("id = ?", 1).First(¤t).Error |
| 79 | if err == gorm.ErrRecordNotFound { |
| 80 | err = db.Create( |
| 81 | &Version{ |
| 82 | ID: 1, |
| 83 | Version: int64(minDBVersion + len(migrations)), |
| 84 | }, |
| 85 | ).Error |
| 86 | if err != nil { |
| 87 | return errors.Wrap(err, "create the version record") |
| 88 | } |
| 89 | return nil |
| 90 | |
| 91 | } else if err != nil { |
| 92 | return errors.Wrap(err, "get the version record") |
| 93 | } |
| 94 | |
| 95 | if minDBVersion > current.Version { |
| 96 | log.Fatal(` |
| 97 | Hi there, thank you for using Gogs for so long! |
| 98 | However, Gogs has stopped supporting auto-migration from your previously installed version. |
| 99 | But the good news is, it's very easy to fix this problem! |
| 100 | You can migrate your older database using a previous release, then you can upgrade to the newest version. |
| 101 | |
| 102 | Please save following instructions to somewhere and start working: |
| 103 | |
| 104 | - If you were using below 0.6.0 (e.g. 0.5.x), download last supported archive from following link: |
| 105 | https://gogs.io/gogs/releases/tag/v0.7.33 |
| 106 | - If you were using below 0.7.0 (e.g. 0.6.x), download last supported archive from following link: |
| 107 | https://gogs.io/gogs/releases/tag/v0.9.141 |
| 108 | - If you were using below 0.11.55 (e.g. 0.9.141), download last supported archive from following link: |
| 109 | https://gogs.io/gogs/releases/tag/v0.12.0 |
| 110 | |
| 111 | Once finished downloading: |
| 112 | |
| 113 | 1. Extract the archive and to upgrade steps as usual. |
| 114 | 2. Run it once. To verify, you should see some migration traces. |
| 115 | 3. Once it starts web server successfully, stop it. |
| 116 | 4. Now it's time to put back the release archive you originally intent to upgrade. |
| 117 | 5. Enjoy! |
| 118 | |
| 119 | In case you're stilling getting this notice, go through instructions again until it disappears.`) |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | if int(current.Version-minDBVersion) > len(migrations) { |
| 124 | // User downgraded Gogs. |
no test coverage detected