// ExpectedVersion returns the expected Forgejo database version.
funcExpectedVersion()int64{
returnint64(len(migrations))
}
// EnsureUpToDate will check if the Forgejo database is at the correct version.
funcEnsureUpToDate(x*xorm.Engine)error{
currentDB,err:=GetCurrentDBVersion(x)
iferr!=nil{
returnerr
}
ifcurrentDB<0{
returnfmt.Errorf("database has not been initialized")
}
expected:=ExpectedVersion()
ifcurrentDB!=expected{
returnfmt.Errorf(`current Forgejo database version %d is not equal to the expected version %d. Please run "forgejo [--config /path/to/app.ini] migrate" to update the database version`,currentDB,expected)
}
returnnil
}
// Migrate Forgejo database to current version.
funcMigrate(x*xorm.Engine)error{
// Set a new clean the default mapper to GonicMapper as that is the default for .
x.SetMapper(names.GonicMapper{})
iferr:=x.Sync(new(ForgejoVersion));err!=nil{
returnfmt.Errorf("sync: %w",err)
}
currentVersion:=&ForgejoVersion{ID:1}
has,err:=x.Get(currentVersion)
iferr!=nil{
returnfmt.Errorf("get: %w",err)
}elseif!has{
// If the version record does not exist we think
// it is a fresh installation and we can skip all migrations.
currentVersion.ID=0
currentVersion.Version=ExpectedVersion()
if_,err=x.InsertOne(currentVersion);err!=nil{
returnfmt.Errorf("insert: %w",err)
}
}
v:=currentVersion.Version
// Downgrading Forgejo's database version not supported
ifv>ExpectedVersion(){
msg:=fmt.Sprintf("Your Forgejo database (migration version: %d) is for a newer version of Forgejo, you cannot use the newer database for this old Forgejo release (%d).",v,ExpectedVersion())
msg+="\nForgejo will exit to keep your database safe and unchanged. Please use the correct Forgejo release, do not change the migration version manually (incorrect manual operation may cause data loss)."
if!setting.IsProd{
msg+=fmt.Sprintf("\nIf you are in development and really know what you're doing, you can force changing the migration version by executing: UPDATE forgejo_version SET version=%d WHERE id=1;",ExpectedVersion())