aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-10-17 07:29:26 +0800
committerGitHub <noreply@github.com>2022-10-17 07:29:26 +0800
commitf860a6d2e4177ed4f4c2a58a07882bd00a1a52ad (patch)
tree93abb2f354576e50c87d70b0b4bb46369fb3a1f1 /models/migrations
parent5d3dbffa150d832d2f9aedd9f90ca91178a95f9c (diff)
downloadgitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.tar.gz
gitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.zip
Add system setting table with cache and also add cache supports for user setting (#18058)
Diffstat (limited to 'models/migrations')
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v227.go64
2 files changed, 66 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index 2a38772180..afe1445a23 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -415,6 +415,8 @@ var migrations = []Migration{
NewMigration("Alter gpg_key/public_key content TEXT fields to MEDIUMTEXT", alterPublicGPGKeyContentFieldsToMediumText),
// v226 -> v227
NewMigration("Conan and generic packages do not need to be semantically versioned", fixPackageSemverField),
+ // v227 -> v228
+ NewMigration("Create key/value table for system settings", createSystemSettingsTable),
}
// GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v227.go b/models/migrations/v227.go
new file mode 100644
index 0000000000..8a3a9c877e
--- /dev/null
+++ b/models/migrations/v227.go
@@ -0,0 +1,64 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "fmt"
+ "strconv"
+
+ "code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/timeutil"
+
+ "xorm.io/xorm"
+)
+
+type SystemSetting struct {
+ ID int64 `xorm:"pk autoincr"`
+ SettingKey string `xorm:"varchar(255) unique"` // ensure key is always lowercase
+ SettingValue string `xorm:"text"`
+ Version int `xorm:"version"` // prevent to override
+ Created timeutil.TimeStamp `xorm:"created"`
+ Updated timeutil.TimeStamp `xorm:"updated"`
+}
+
+func insertSettingsIfNotExist(x *xorm.Engine, sysSettings []*SystemSetting) error {
+ sess := x.NewSession()
+ defer sess.Close()
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+ for _, setting := range sysSettings {
+ exist, err := sess.Table("system_setting").Where("setting_key=?", setting.SettingKey).Exist()
+ if err != nil {
+ return err
+ }
+ if !exist {
+ if _, err := sess.Insert(setting); err != nil {
+ return err
+ }
+ }
+ }
+ return sess.Commit()
+}
+
+func createSystemSettingsTable(x *xorm.Engine) error {
+ if err := x.Sync2(new(SystemSetting)); err != nil {
+ return fmt.Errorf("sync2: %v", err)
+ }
+
+ // migrate xx to database
+ sysSettings := []*SystemSetting{
+ {
+ SettingKey: "picture.disable_gravatar",
+ SettingValue: strconv.FormatBool(setting.DisableGravatar),
+ },
+ {
+ SettingKey: "picture.enable_federated_avatar",
+ SettingValue: strconv.FormatBool(setting.EnableFederatedAvatar),
+ },
+ }
+
+ return insertSettingsIfNotExist(x, sysSettings)
+}