mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-18 15:08:20 +00:00
[UPGRADE] run sanity checks before the database is upgraded
(cherry picked from commit69741e4e66
) (cherry picked from commit2a3c7b09cb
) (cherry picked from commita1554c1168
) (cherry picked from commitedae2c6d2d
) (cherry picked from commit49737cf009
) (cherry picked from commitec53704c34
) (cherry picked from commit7a1c5c0f32
) (cherry picked from commite658c20c0f
) (cherry picked from commitbaf575468f
) (cherry picked from commit40cb14eff4
) (cherry picked from commit25ab4d0713
) (cherry picked from commit5a29005215
) (cherry picked from commitfef1260e99
) (cherry picked from commiteadbbb1afe
) (cherry picked from commitdb22d61eb4
) (cherry picked from commit9d3b0be39a
) (cherry picked from commitb3fa3c1292
) (cherry picked from commitc8300d4fe2
) (cherry picked from commit8ba6a4c9db
) (cherry picked from commit8b8df652c1
) (cherry picked from commitfc8fa050c6
) (cherry picked from commitbcf3faf698
) (cherry picked from commit514a631aa6
) (cherry picked from commit529c7a09f7
)
This commit is contained in:
parent
e70d51bd26
commit
0d093d76e2
|
@ -29,6 +29,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
forgejo_services "code.gitea.io/gitea/services/forgejo"
|
||||||
|
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
|
@ -615,6 +616,7 @@ func Migrate(x *xorm.Engine) error {
|
||||||
return fmt.Errorf("sync: %w", err)
|
return fmt.Errorf("sync: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var previousVersion int64
|
||||||
currentVersion := &Version{ID: 1}
|
currentVersion := &Version{ID: 1}
|
||||||
has, err := x.Get(currentVersion)
|
has, err := x.Get(currentVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -628,6 +630,8 @@ func Migrate(x *xorm.Engine) error {
|
||||||
if _, err = x.InsertOne(currentVersion); err != nil {
|
if _, err = x.InsertOne(currentVersion); err != nil {
|
||||||
return fmt.Errorf("insert: %w", err)
|
return fmt.Errorf("insert: %w", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
previousVersion = currentVersion.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
v := currentVersion.Version
|
v := currentVersion.Version
|
||||||
|
@ -655,6 +659,10 @@ Please try upgrading to a lower version first (suggested v1.6.4), then upgrade t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := forgejo_services.PreMigrationSanityChecks(x, previousVersion, setting.CfgProvider); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
for i, m := range migrations[v-minDBVersion:] {
|
for i, m := range migrations[v-minDBVersion:] {
|
||||||
log.Info("Migration[%d]: %s", v+int64(i), m.Description())
|
log.Info("Migration[%d]: %s", v+int64(i), m.Description())
|
||||||
|
|
20
services/forgejo/main_test.go
Normal file
20
services/forgejo/main_test.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package forgejo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/unittest"
|
||||||
|
|
||||||
|
_ "code.gitea.io/gitea/models"
|
||||||
|
_ "code.gitea.io/gitea/models/actions"
|
||||||
|
_ "code.gitea.io/gitea/models/activities"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
unittest.MainTest(m, &unittest.TestOptions{
|
||||||
|
GiteaRootPath: filepath.Join("..", ".."),
|
||||||
|
})
|
||||||
|
}
|
25
services/forgejo/sanity.go
Normal file
25
services/forgejo/sanity.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package forgejo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ForgejoV5DatabaseVersion = int64(260)
|
||||||
|
ForgejoV4DatabaseVersion = int64(244)
|
||||||
|
)
|
||||||
|
|
||||||
|
var logFatal = log.Fatal
|
||||||
|
|
||||||
|
func fatal(err error) error {
|
||||||
|
logFatal("%v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func PreMigrationSanityChecks(e db.Engine, dbVersion int64, cfg setting.ConfigProvider) error {
|
||||||
|
return nil
|
||||||
|
}
|
31
services/forgejo/sanity_test.go
Normal file
31
services/forgejo/sanity_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package forgejo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
|
"code.gitea.io/gitea/models/unittest"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestForgejo_PreMigrationSanityChecks(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
ctx := db.DefaultContext
|
||||||
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
|
assert.NoError(t, PreMigrationSanityChecks(e, ForgejoV4DatabaseVersion, configFixture(t, "")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func configFixture(t *testing.T, content string) setting.ConfigProvider {
|
||||||
|
config := filepath.Join(t.TempDir(), "app.ini")
|
||||||
|
assert.NoError(t, os.WriteFile(config, []byte(content), 0o777))
|
||||||
|
cfg, err := setting.NewConfigProviderFromFile(config)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
return cfg
|
||||||
|
}
|
Loading…
Reference in a new issue