aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations/v1_18/v227.go
blob: 7dc468108c3a77591c89d028d19c8ebd973f8291 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 v1_18 // nolint

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: %w", 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)
}