1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 01:03:10 +00:00

[SEMVER] store SemVer in ForgejoSemVer after a database upgrade

(cherry picked from commit b7fe7cf401)
(cherry picked from commit cf339eed4f)
(cherry picked from commit 4f3a16168b)
(cherry picked from commit 6f5bbc53fc)
(cherry picked from commit aca42b422e)
(cherry picked from commit 5a7f7580e5)
(cherry picked from commit 06c383c807)
(cherry picked from commit fe831dcb53)
(cherry picked from commit cd12cd0dbc)
(cherry picked from commit cc79163703)
(cherry picked from commit 0102a5715e)
(cherry picked from commit 403f7520b3)
(cherry picked from commit a3b61510a2)
(cherry picked from commit f83f0f9feb)
(cherry picked from commit fd1c3a6d09)
(cherry picked from commit f7cdc3d6f1)
(cherry picked from commit 060121b644)
(cherry picked from commit 62c847ff02)
(cherry picked from commit 4d051b51c2)
(cherry picked from commit 86e6981a93)
(cherry picked from commit c1fc9e441b)
(cherry picked from commit 8bb2f0871a)
(cherry picked from commit 0cd9fe5251)
(cherry picked from commit b0b44778b4)
This commit is contained in:
Earl Warren 2023-08-08 23:55:25 +02:00
parent 0a3f370162
commit 7c2f4f749f
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
9 changed files with 158 additions and 2 deletions

View file

@ -95,7 +95,7 @@ VERSION = ${GITEA_VERSION}
# SemVer # SemVer
FORGEJO_VERSION := 5.0.0+0-gitea-1.20.0 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 LINUX_ARCHS ?= linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64

View file

@ -31,8 +31,11 @@ var (
MakeVersion = "" // "make" program version if built with make MakeVersion = "" // "make" program version if built with make
) )
var ForgejoVersion = "1.0.0"
func init() { func init() {
setting.AppVer = Version setting.AppVer = Version
setting.ForgejoVersion = ForgejoVersion
setting.AppBuiltWith = formatBuiltWith() setting.AppBuiltWith = formatBuiltWith()
setting.AppStartTime = time.Now().UTC() setting.AppStartTime = time.Now().UTC()
} }

View file

@ -44,6 +44,7 @@ type Engine interface {
Incr(column string, arg ...any) *xorm.Session Incr(column string, arg ...any) *xorm.Session
Insert(...any) (int64, error) Insert(...any) (int64, error)
Iterate(any, xorm.IterFunc) error Iterate(any, xorm.IterFunc) error
IsTableExist(any) (bool, error)
Join(joinOperator string, tablename, condition any, args ...any) *xorm.Session Join(joinOperator string, tablename, condition any, args ...any) *xorm.Session
SQL(any, ...any) *xorm.Session SQL(any, ...any) *xorm.Session
Where(any, ...any) *xorm.Session Where(any, ...any) *xorm.Session

View file

@ -68,6 +68,7 @@ func TestPrimaryKeys(t *testing.T) {
whitelist := map[string]string{ whitelist := map[string]string{
"the_table_name_to_skip_checking": "Write a note here to explain why", "the_table_name_to_skip_checking": "Write a note here to explain why",
"forgejo_sem_ver": "seriously dude",
} }
for _, bean := range beans { for _, bean := range beans {

View file

@ -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("..", "..", ".."),
})
}

View file

@ -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
}

View file

@ -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())
}

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"code.gitea.io/gitea/models/forgejo/semver"
forgejo_v1_20 "code.gitea.io/gitea/models/forgejo_migrations/v1_20" forgejo_v1_20 "code.gitea.io/gitea/models/forgejo_migrations/v1_20"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -141,5 +142,10 @@ func Migrate(x *xorm.Engine) error {
return err 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)
} }

View file

@ -15,6 +15,8 @@ import (
"code.gitea.io/gitea/modules/user" "code.gitea.io/gitea/modules/user"
) )
var ForgejoVersion = "1.0.0"
// settings // settings
var ( var (
// AppVer is the version of the current build of Gitea. It is set in main.go from main.Version. // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version.