aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-11-28 01:15:40 +0800
committerGitHub <noreply@github.com>2023-11-27 17:15:40 +0000
commita6aed0fee6b238dff3b747c9f260931411e81bb8 (patch)
tree72ce6eaf8749ee6a97430f3823feed8ef9aab962
parent8572e71a7f0d6e49226a2fade69c02d03580fb92 (diff)
downloadgitea-a6aed0fee6b238dff3b747c9f260931411e81bb8.tar.gz
gitea-a6aed0fee6b238dff3b747c9f260931411e81bb8.zip
Increase "version" when update the setting value to a same value as before (#28243)
Setting the same value should not trigger DuplicateKey error, and the "version" should be increased
-rw-r--r--models/system/setting.go2
-rw-r--r--models/system/setting_test.go12
2 files changed, 13 insertions, 1 deletions
diff --git a/models/system/setting.go b/models/system/setting.go
index 507d23cff6..cd1d373b98 100644
--- a/models/system/setting.go
+++ b/models/system/setting.go
@@ -81,7 +81,7 @@ func SetSettings(ctx context.Context, settings map[string]string) error {
return err
}
for k, v := range settings {
- res, err := e.Exec("UPDATE system_setting SET setting_value=? WHERE setting_key=?", v, k)
+ res, err := e.Exec("UPDATE system_setting SET version=version+1, setting_value=? WHERE setting_key=?", v, k)
if err != nil {
return err
}
diff --git a/models/system/setting_test.go b/models/system/setting_test.go
index 4117420e5f..0316ea144d 100644
--- a/models/system/setting_test.go
+++ b/models/system/setting_test.go
@@ -39,4 +39,16 @@ func TestSettings(t *testing.T) {
assert.EqualValues(t, 3, rev)
assert.Len(t, settings, 2)
assert.EqualValues(t, "false", settings[keyName])
+
+ // setting the same value should not trigger DuplicateKey error, and the "version" should be increased
+ setting := &system.Setting{SettingKey: keyName}
+ _, err = db.GetByBean(db.DefaultContext, setting)
+ assert.NoError(t, err)
+ assert.EqualValues(t, 2, setting.Version)
+ err = system.SetSettings(db.DefaultContext, map[string]string{keyName: "false"})
+ assert.NoError(t, err)
+ setting = &system.Setting{SettingKey: keyName}
+ _, err = db.GetByBean(db.DefaultContext, setting)
+ assert.NoError(t, err)
+ assert.EqualValues(t, 3, setting.Version)
}