MCPcopy Create free account
hub / github.com/gogs/gogs / Migrate

Function Migrate

internal/database/migrations/migrations.go:67–145  ·  view source on GitHub ↗

Migrate migrates the database schema and/or data to the current version.

(db *gorm.DB)

Source from the content-addressed store, hash-verified

65
66// Migrate migrates the database schema and/or data to the current version.
67func 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(&current).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(`
97Hi there, thank you for using Gogs for so long!
98However, Gogs has stopped supporting auto-migration from your previously installed version.
99But the good news is, it's very easy to fix this problem!
100You can migrate your older database using a previous release, then you can upgrade to the newest version.
101
102Please 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
111Once finished downloading:
112
1131. Extract the archive and to upgrade steps as usual.
1142. Run it once. To verify, you should see some migration traces.
1153. Once it starts web server successfully, stop it.
1164. Now it's time to put back the release archive you originally intent to upgrade.
1175. Enjoy!
118
119In 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.

Callers 1

NewEngineFunction · 0.92

Calls 4

WhereMethod · 0.80
DescriptionMethod · 0.65
MigrateMethod · 0.65
CreateMethod · 0.45

Tested by

no test coverage detected