From 081a155cc19948fe95385611489564ca2f722cac Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Tue, 8 Aug 2023 23:55:25 +0200 Subject: [PATCH] [SEMVER] store SemVer in ForgejoSemVer after a database upgrade (cherry picked from commit b7fe7cf401f4bddd6455efc651f7ac054f3fe1cf) (cherry picked from commit cf339eed4f4851b18448dbdd83df32d00bc6f45b) (cherry picked from commit 4f3a16168bbeced519a60c32e10e2895d9367238) (cherry picked from commit 6f5bbc53fcebd614f5ee8627c7d6e3c637ffa694) (cherry picked from commit aca42b422e76668387769c15868ef77b073bb7db) (cherry picked from commit 5a7f7580e525694d1f27e12329c8532bd29273f8) (cherry picked from commit 06c383c807ad49e1e35b429a10c6a11c65aeebe5) (cherry picked from commit fe831dcb53b81b6cc632be751bfcbfb9bc00efd3) (cherry picked from commit cd12cd0dbce47c6117ea579ae5019c182155b3a9) (cherry picked from commit cc79163703ce31706c86b88c38fb8a20ed745e20) (cherry picked from commit 0102a5715ea6a03b560cc2f0b6cbe2b2576c255e) (cherry picked from commit 403f7520b3056eace36eae505afbab6a05f597b7) (cherry picked from commit a3b61510a246f61c174ddc3c288e556522a6aab9) (cherry picked from commit f83f0f9feb76a8b62ca4d74dc2785c713fbec282) (cherry picked from commit fd1c3a6d09a057070844cd955d0920518e60f408) (cherry picked from commit f7cdc3d6f1ad1ff01bb1814207fcb41210db80e8) (cherry picked from commit 060121b644e0515a6b673cdd514d52e6fa1e6ec3) (cherry picked from commit 62c847ff0235196e73e860d0d1658f0734985270) (cherry picked from commit 4d051b51c2813828b978ef84b6b72c0ba051741b) (cherry picked from commit 86e6981a936b1937065605892083b2488424bf5e) (cherry picked from commit c1fc9e441b1467033911f3848f82a89aaacf98a9) (cherry picked from commit 8bb2f0871a507d881acf74acf8fb90fd5ebff567) (cherry picked from commit 0cd9fe52511ee06f1b8849325d196f60d8ccd151) (cherry picked from commit b0b44778b4d3e51852d0669dfe6d9a6c334b90e4) (cherry picked from commit 7c2f4f749f1c8986875eebf95254a3db151e5248) --- Makefile | 2 +- main.go | 3 ++ models/db/engine.go | 1 + models/db/engine_test.go | 1 + models/forgejo/semver/main_test.go | 20 ++++++++ models/forgejo/semver/semver.go | 77 ++++++++++++++++++++++++++++ models/forgejo/semver/semver_test.go | 46 +++++++++++++++++ models/forgejo_migrations/migrate.go | 8 ++- modules/setting/setting.go | 2 + 9 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 models/forgejo/semver/main_test.go create mode 100644 models/forgejo/semver/semver.go create mode 100644 models/forgejo/semver/semver_test.go diff --git a/Makefile b/Makefile index bdd118311..5441a84d9 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ VERSION = ${GITEA_VERSION} # SemVer FORGEJO_VERSION := 5.0.0+0-gitea-1.20.0 -LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(GITEA_VERSION)" -X "main.Tags=$(TAGS)" -X "code.gitea.io/gitea/routers/api/forgejo/v1.ForgejoVersion=$(FORGEJO_VERSION)" +LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(GITEA_VERSION)" -X "main.Tags=$(TAGS)" -X "code.gitea.io/gitea/routers/api/forgejo/v1.ForgejoVersion=$(FORGEJO_VERSION)" -X "main.ForgejoVersion=$(FORGEJO_VERSION)" LINUX_ARCHS ?= linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64 diff --git a/main.go b/main.go index 775c729c5..700b75d74 100644 --- a/main.go +++ b/main.go @@ -31,8 +31,11 @@ var ( MakeVersion = "" // "make" program version if built with make ) +var ForgejoVersion = "1.0.0" + func init() { setting.AppVer = Version + setting.ForgejoVersion = ForgejoVersion setting.AppBuiltWith = formatBuiltWith() setting.AppStartTime = time.Now().UTC() } diff --git a/models/db/engine.go b/models/db/engine.go index 2cd1c36c5..b2fbdcfbf 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -44,6 +44,7 @@ type Engine interface { Incr(column string, arg ...any) *xorm.Session Insert(...any) (int64, error) Iterate(any, xorm.IterFunc) error + IsTableExist(any) (bool, error) Join(joinOperator string, tablename, condition any, args ...any) *xorm.Session SQL(any, ...any) *xorm.Session Where(any, ...any) *xorm.Session diff --git a/models/db/engine_test.go b/models/db/engine_test.go index e3dbfbdc2..c9ae5f154 100644 --- a/models/db/engine_test.go +++ b/models/db/engine_test.go @@ -68,6 +68,7 @@ func TestPrimaryKeys(t *testing.T) { whitelist := map[string]string{ "the_table_name_to_skip_checking": "Write a note here to explain why", + "forgejo_sem_ver": "seriously dude", } for _, bean := range beans { diff --git a/models/forgejo/semver/main_test.go b/models/forgejo/semver/main_test.go new file mode 100644 index 000000000..34c4f5539 --- /dev/null +++ b/models/forgejo/semver/main_test.go @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT + +package semver + +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/models/forgejo/semver/semver.go b/models/forgejo/semver/semver.go new file mode 100644 index 000000000..50923d0a0 --- /dev/null +++ b/models/forgejo/semver/semver.go @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT + +package semver + +import ( + "context" + + "code.gitea.io/gitea/models/db" + + "github.com/hashicorp/go-version" +) + +func init() { + db.RegisterModel(new(ForgejoSemVer)) +} + +type ForgejoSemVer struct { + Version string +} + +func GetVersion(ctx context.Context) (*version.Version, error) { + return GetVersionWithEngine(db.GetEngine(ctx)) +} + +func GetVersionWithEngine(e db.Engine) (*version.Version, error) { + versionString := "v1.0.0" + exists, err := e.IsTableExist("forgejo_sem_ver") + if err != nil { + return nil, err + } + if exists { + var semver ForgejoSemVer + has, err := e.Get(&semver) + if err != nil { + return nil, err + } else if has { + versionString = semver.Version + } + } + + v, err := version.NewVersion(versionString) + if err != nil { + return nil, err + } + return v, nil +} + +func SetVersionString(ctx context.Context, versionString string) error { + return SetVersionStringWithEngine(db.GetEngine(ctx), versionString) +} + +func SetVersionStringWithEngine(e db.Engine, versionString string) error { + v, err := version.NewVersion(versionString) + if err != nil { + return err + } + return SetVersionWithEngine(e, v) +} + +func SetVersion(ctx context.Context, v *version.Version) error { + return SetVersionWithEngine(db.GetEngine(ctx), v) +} + +func SetVersionWithEngine(e db.Engine, v *version.Version) error { + var semver ForgejoSemVer + has, err := e.Get(&semver) + if err != nil { + return err + } + + if !has { + _, err = e.Exec("insert into forgejo_sem_ver values (?)", v.String()) + } else { + _, err = e.Exec("update forgejo_sem_ver set version = ?", v.String()) + } + return err +} diff --git a/models/forgejo/semver/semver_test.go b/models/forgejo/semver/semver_test.go new file mode 100644 index 000000000..8aca7bee5 --- /dev/null +++ b/models/forgejo/semver/semver_test.go @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT + +package semver + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + + "github.com/hashicorp/go-version" + "github.com/stretchr/testify/assert" +) + +func TestForgejoSemVerSetGet(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + ctx := db.DefaultContext + + newVersion, err := version.NewVersion("v1.2.3") + assert.NoError(t, err) + assert.NoError(t, SetVersionString(ctx, newVersion.String())) + databaseVersion, err := GetVersion(ctx) + assert.NoError(t, err) + assert.EqualValues(t, newVersion.String(), databaseVersion.String()) + assert.True(t, newVersion.Equal(databaseVersion)) +} + +func TestForgejoSemVerMissing(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + ctx := db.DefaultContext + e := db.GetEngine(ctx) + + _, err := e.Exec("delete from forgejo_sem_ver") + assert.NoError(t, err) + + v, err := GetVersion(ctx) + assert.NoError(t, err) + assert.EqualValues(t, "1.0.0", v.String()) + + _, err = e.Exec("drop table forgejo_sem_ver") + assert.NoError(t, err) + + v, err = GetVersion(ctx) + assert.NoError(t, err) + assert.EqualValues(t, "1.0.0", v.String()) +} diff --git a/models/forgejo_migrations/migrate.go b/models/forgejo_migrations/migrate.go index 268edef4f..2becf1b71 100644 --- a/models/forgejo_migrations/migrate.go +++ b/models/forgejo_migrations/migrate.go @@ -8,6 +8,7 @@ import ( "fmt" "os" + "code.gitea.io/gitea/models/forgejo/semver" forgejo_v1_20 "code.gitea.io/gitea/models/forgejo_migrations/v1_20" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -141,5 +142,10 @@ func Migrate(x *xorm.Engine) error { return err } } - return nil + + if err := x.Sync(new(semver.ForgejoSemVer)); err != nil { + return fmt.Errorf("sync: %w", err) + } + + return semver.SetVersionStringWithEngine(x, setting.ForgejoVersion) } diff --git a/modules/setting/setting.go b/modules/setting/setting.go index d444d9a01..ebfd3b27b 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -15,6 +15,8 @@ import ( "code.gitea.io/gitea/modules/user" ) +var ForgejoVersion = "1.0.0" + // settings var ( // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version.