Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid warning for system setting when start up #23054

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions models/avatars/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
return DefaultAvatarLink()
}

enableFederatedAvatar := system_model.GetSettingBool(ctx, system_model.KeyPictureEnableFederatedAvatar)
enableFederatedAvatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureEnableFederatedAvatar)

var err error
if enableFederatedAvatar && system_model.LibravatarService != nil {
Expand All @@ -174,7 +174,7 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
return urlStr
}

disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
if !disableGravatar {
// copy GravatarSourceURL, because we will modify its Path.
avatarURLCopy := *system_model.GravatarSourceURL
Expand Down
2 changes: 1 addition & 1 deletion models/avatars/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func enableGravatar(t *testing.T) {
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
assert.NoError(t, err)
setting.GravatarSource = gravatarSource
err = system_model.Init()
err = system_model.Init(db.DefaultContext)
assert.NoError(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import (
var ItemsPerPage = 40

// Init initialize model
func Init() error {
func Init(ctx context.Context) error {
unit.LoadUnitConfig()
return system_model.Init()
return system_model.Init(ctx)
}

// DeleteRepository deletes a repository for a user or organization.
Expand Down
49 changes: 29 additions & 20 deletions models/system/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func IsErrDataExpired(err error) bool {
return ok
}

// GetSettingNoCache returns specific setting without using the cache
func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {
// GetSetting returns specific setting without using the cache
func GetSetting(ctx context.Context, key string) (*Setting, error) {
v, err := GetSettings(ctx, []string{key})
if err != nil {
return nil, err
Expand All @@ -93,11 +93,11 @@ func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) {

const contextCacheKey = "system_setting"

// GetSetting returns the setting value via the key
func GetSetting(ctx context.Context, key string) (string, error) {
// GetSettingWithCache returns the setting value via the key
func GetSettingWithCache(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
res, err := GetSetting(ctx, key)
if err != nil {
return "", err
}
Expand All @@ -110,6 +110,15 @@ func GetSetting(ctx context.Context, key string) (string, error) {
// none existing keys and errors are ignored and result in false
func GetSettingBool(ctx context.Context, key string) bool {
s, _ := GetSetting(ctx, key)
if s == nil {
return false
}
v, _ := strconv.ParseBool(s.SettingValue)
return v
}

func GetSettingWithCacheBool(ctx context.Context, key string) bool {
s, _ := GetSettingWithCache(ctx, key)
v, _ := strconv.ParseBool(s)
return v
}
Expand All @@ -120,7 +129,7 @@ func GetSettings(ctx context.Context, keys []string) (map[string]*Setting, error
keys[i] = strings.ToLower(keys[i])
}
settings := make([]*Setting, 0, len(keys))
if err := db.GetEngine(db.DefaultContext).
if err := db.GetEngine(ctx).
Where(builder.In("setting_key", keys)).
Find(&settings); err != nil {
return nil, err
Expand Down Expand Up @@ -151,9 +160,9 @@ func (settings AllSettings) GetVersion(key string) int {
}

// GetAllSettings returns all settings from user
func GetAllSettings() (AllSettings, error) {
func GetAllSettings(ctx context.Context) (AllSettings, error) {
settings := make([]*Setting, 0, 5)
if err := db.GetEngine(db.DefaultContext).
if err := db.GetEngine(ctx).
Find(&settings); err != nil {
return nil, err
}
Expand All @@ -168,12 +177,12 @@ func GetAllSettings() (AllSettings, error) {
func DeleteSetting(ctx context.Context, setting *Setting) error {
cache.RemoveContextData(ctx, contextCacheKey, setting.SettingKey)
cache.Remove(genSettingCacheKey(setting.SettingKey))
_, err := db.GetEngine(db.DefaultContext).Delete(setting)
_, err := db.GetEngine(ctx).Delete(setting)
return err
}

func SetSettingNoVersion(ctx context.Context, key, value string) error {
s, err := GetSettingNoCache(ctx, key)
s, err := GetSetting(ctx, key)
if IsErrSettingIsNotExist(err) {
return SetSetting(ctx, &Setting{
SettingKey: key,
Expand All @@ -189,7 +198,7 @@ func SetSettingNoVersion(ctx context.Context, key, value string) error {

// SetSetting updates a users' setting for a specific key
func SetSetting(ctx context.Context, setting *Setting) error {
if err := upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil {
if err := upsertSettingValue(ctx, strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil {
return err
}

Expand All @@ -205,8 +214,8 @@ func SetSetting(ctx context.Context, setting *Setting) error {
return nil
}

func upsertSettingValue(key, value string, version int) error {
return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
func upsertSettingValue(parentCtx context.Context, key, value string, version int) error {
return db.WithTx(parentCtx, func(ctx context.Context) error {
e := db.GetEngine(ctx)

// here we use a general method to do a safe upsert for different databases (and most transaction levels)
Expand Down Expand Up @@ -249,9 +258,9 @@ var (
LibravatarService *libravatar.Libravatar
)

func Init() error {
func Init(ctx context.Context) error {
var disableGravatar bool
disableGravatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureDisableGravatar)
disableGravatarSetting, err := GetSetting(ctx, KeyPictureDisableGravatar)
if IsErrSettingIsNotExist(err) {
disableGravatar = setting_module.GetDefaultDisableGravatar()
disableGravatarSetting = &Setting{SettingValue: strconv.FormatBool(disableGravatar)}
Expand All @@ -262,7 +271,7 @@ func Init() error {
}

var enableFederatedAvatar bool
enableFederatedAvatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureEnableFederatedAvatar)
enableFederatedAvatarSetting, err := GetSetting(ctx, KeyPictureEnableFederatedAvatar)
if IsErrSettingIsNotExist(err) {
enableFederatedAvatar = setting_module.GetDefaultEnableFederatedAvatar(disableGravatar)
enableFederatedAvatarSetting = &Setting{SettingValue: strconv.FormatBool(enableFederatedAvatar)}
Expand All @@ -275,13 +284,13 @@ func Init() error {
if setting_module.OfflineMode {
disableGravatar = true
enableFederatedAvatar = false
if !GetSettingBool(db.DefaultContext, KeyPictureDisableGravatar) {
if err := SetSettingNoVersion(db.DefaultContext, KeyPictureDisableGravatar, "true"); err != nil {
if !GetSettingBool(ctx, KeyPictureDisableGravatar) {
if err := SetSettingNoVersion(ctx, KeyPictureDisableGravatar, "true"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureDisableGravatar, err)
}
}
if GetSettingBool(db.DefaultContext, KeyPictureEnableFederatedAvatar) {
if err := SetSettingNoVersion(db.DefaultContext, KeyPictureEnableFederatedAvatar, "false"); err != nil {
if GetSettingBool(ctx, KeyPictureEnableFederatedAvatar) {
if err := SetSettingNoVersion(ctx, KeyPictureEnableFederatedAvatar, "false"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureEnableFederatedAvatar, err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions models/system/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ func TestSettings(t *testing.T) {

value, err := system.GetSetting(db.DefaultContext, keyName)
assert.NoError(t, err)
assert.EqualValues(t, updatedSetting.SettingValue, value)
assert.EqualValues(t, updatedSetting.SettingValue, value.SettingValue)

// get all settings
settings, err = system.GetAllSettings()
settings, err = system.GetAllSettings(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, settings, 3)
assert.EqualValues(t, updatedSetting.SettingValue, settings[strings.ToLower(updatedSetting.SettingKey)].SettingValue)

// delete setting
err = system.DeleteSetting(db.DefaultContext, &system.Setting{SettingKey: strings.ToLower(keyName)})
assert.NoError(t, err)
settings, err = system.GetAllSettings()
settings, err = system.GetAllSettings(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, settings, 2)
}
2 changes: 1 addition & 1 deletion models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err)
}
if err = system_model.Init(); err != nil {
if err = system_model.Init(db.DefaultContext); err != nil {
fatalTestError("models.Init: %v\n", err)
}

Expand Down
2 changes: 1 addition & 1 deletion models/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
useLocalAvatar := false
autoGenerateAvatar := false

disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)

switch {
case u.UseCustomAvatar:
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func enableGravatar(t *testing.T) {
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
assert.NoError(t, err)
setting.GravatarSource = "https://secure.gravatar.com/avatar"
err = system_model.Init()
err = system_model.Init(db.DefaultContext)
assert.NoError(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewFuncMap() []template.FuncMap {
return setting.AssetVersion
},
"DisableGravatar": func(ctx context.Context) bool {
return system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar)
return system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
},
"DefaultShowFullName": func() bool {
return setting.UI.DefaultShowFullName
Expand Down
2 changes: 1 addition & 1 deletion routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func GlobalInitInstalled(ctx context.Context) {
mustInit(system.Init)
mustInit(oauth2.Init)

mustInit(models.Init)
mustInitCtx(ctx, models.Init)
mustInit(repo_service.Init)

// Booting long running goroutines.
Expand Down
2 changes: 1 addition & 1 deletion routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Config(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminConfig"] = true

systemSettings, err := system_model.GetAllSettings()
systemSettings, err := system_model.GetAllSettings(ctx)
if err != nil {
ctx.ServerError("system_model.GetAllSettings", err)
return
Expand Down