2022-10-16 17:29:26 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-16 17:29:26 -06:00
|
|
|
|
2022-12-08 01:21:37 -07:00
|
|
|
package v1_18 //nolint
|
2022-10-16 17:29:26 -06:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-11-02 02:54:36 -06:00
|
|
|
func CreateSystemSettingsTable(x *xorm.Engine) error {
|
2023-08-13 13:17:21 -06:00
|
|
|
if err := x.Sync(new(SystemSetting)); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("sync2: %w", err)
|
2022-10-16 17:29:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|