From bcf3faf69843421517bf3ddf032fe92cbf0a766d Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 9 Aug 2023 00:05:06 +0200 Subject: [PATCH] [UPGRADE] run sanity checks before the database is upgraded (cherry picked from commit 69741e4e66932a9ac092089e7ba27399c55dcd1a) (cherry picked from commit 2a3c7b09cbdfd62cca2619aaf37b6913a373d142) (cherry picked from commit a1554c1168d897e8db4024d716a837c012bf74e9) (cherry picked from commit edae2c6d2dda7f44e40ae88fba60a15f61b72232) (cherry picked from commit 49737cf009a6a0fc119cf8a1a1593493c77c841a) (cherry picked from commit ec53704c34a3e9491b4f210250d5e053f4b221e5) (cherry picked from commit 7a1c5c0f323cb5e9235d8fa6e59a0a1b172d3abb) (cherry picked from commit e658c20c0f21b42f741c0149e0e79ea0bb3b949c) (cherry picked from commit baf575468f39c5dd0c2ff3498ef9f706d12d114e) (cherry picked from commit 40cb14eff427c801243e374f7c60fef994bcb792) (cherry picked from commit 25ab4d07136d023236de00e9143957c856b4d196) (cherry picked from commit 5a29005215a5e9419ed3096c1bc0aae172f45089) (cherry picked from commit fef1260e990719af49644970aaa2a7219438b681) (cherry picked from commit eadbbb1afe6f36cfaf4cc3fc346b510893f21011) (cherry picked from commit db22d61eb47b56cc7cf0f44934f8f550df029e76) (cherry picked from commit 9d3b0be39a008e2e1a4f474b0ab74627e003430d) (cherry picked from commit b3fa3c1292228ef4833b6ea1f120102471478256) (cherry picked from commit c8300d4fe24fccaefc18e95ec8c6d689f1cb4d7a) (cherry picked from commit 8ba6a4c9dbd9cbb2758b02016f1858d1e85633ce) (cherry picked from commit 8b8df652c1eb7806e590751942b4689e374e1128) (cherry picked from commit fc8fa050c688451c9c2079b1f0f71ed11a6d7e4d) --- models/migrations/migrations.go | 8 ++++++++ services/forgejo/main_test.go | 20 ++++++++++++++++++++ services/forgejo/sanity.go | 25 +++++++++++++++++++++++++ services/forgejo/sanity_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 services/forgejo/main_test.go create mode 100644 services/forgejo/sanity.go create mode 100644 services/forgejo/sanity_test.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 39598000be..2a4fa88aac 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -29,6 +29,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + forgejo_services "code.gitea.io/gitea/services/forgejo" "xorm.io/xorm" "xorm.io/xorm/names" @@ -609,6 +610,7 @@ func Migrate(x *xorm.Engine) error { return fmt.Errorf("sync: %w", err) } + var previousVersion int64 currentVersion := &Version{ID: 1} has, err := x.Get(currentVersion) if err != nil { @@ -622,6 +624,8 @@ func Migrate(x *xorm.Engine) error { if _, err = x.InsertOne(currentVersion); err != nil { return fmt.Errorf("insert: %w", err) } + } else { + previousVersion = currentVersion.Version } v := currentVersion.Version @@ -649,6 +653,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 for i, m := range migrations[v-minDBVersion:] { log.Info("Migration[%d]: %s", v+int64(i), m.Description()) diff --git a/services/forgejo/main_test.go b/services/forgejo/main_test.go new file mode 100644 index 0000000000..70b6e7a15f --- /dev/null +++ b/services/forgejo/main_test.go @@ -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("..", ".."), + }) +} diff --git a/services/forgejo/sanity.go b/services/forgejo/sanity.go new file mode 100644 index 0000000000..0ca85c8406 --- /dev/null +++ b/services/forgejo/sanity.go @@ -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 +} diff --git a/services/forgejo/sanity_test.go b/services/forgejo/sanity_test.go new file mode 100644 index 0000000000..29ed3bbfff --- /dev/null +++ b/services/forgejo/sanity_test.go @@ -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 +}