diff options
Diffstat (limited to 'models/system')
-rw-r--r-- | models/system/appstate.go | 56 | ||||
-rw-r--r-- | models/system/main_test.go | 19 | ||||
-rw-r--r-- | models/system/notice.go | 128 | ||||
-rw-r--r-- | models/system/notice_test.go | 110 | ||||
-rw-r--r-- | models/system/setting.go | 152 | ||||
-rw-r--r-- | models/system/setting_test.go | 52 |
6 files changed, 517 insertions, 0 deletions
diff --git a/models/system/appstate.go b/models/system/appstate.go new file mode 100644 index 00000000..01faa1a5 --- /dev/null +++ b/models/system/appstate.go @@ -0,0 +1,56 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system + +import ( + "context" + + "code.gitea.io/gitea/models/db" +) + +// AppState represents a state record in database +// if one day we would make Gitea run as a cluster, +// we can introduce a new field `Scope` here to store different states for different nodes +type AppState struct { + ID string `xorm:"pk varchar(200)"` + Revision int64 + Content string `xorm:"LONGTEXT"` +} + +func init() { + db.RegisterModel(new(AppState)) +} + +// SaveAppStateContent saves the app state item to database +func SaveAppStateContent(ctx context.Context, key, content string) error { + return db.WithTx(ctx, func(ctx context.Context) error { + eng := db.GetEngine(ctx) + // try to update existing row + res, err := eng.Exec("UPDATE app_state SET revision=revision+1, content=? WHERE id=?", content, key) + if err != nil { + return err + } + rows, _ := res.RowsAffected() + if rows != 0 { + // the existing row is updated, so we can return + return nil + } + // if no existing row, insert a new row + _, err = eng.Insert(&AppState{ID: key, Content: content}) + return err + }) +} + +// GetAppStateContent gets an app state from database +func GetAppStateContent(ctx context.Context, key string) (content string, err error) { + e := db.GetEngine(ctx) + appState := &AppState{ID: key} + has, err := e.Get(appState) + if err != nil { + return "", err + } else if !has { + return "", nil + } + return appState.Content, nil +} diff --git a/models/system/main_test.go b/models/system/main_test.go new file mode 100644 index 00000000..6bc27a7c --- /dev/null +++ b/models/system/main_test.go @@ -0,0 +1,19 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system_test + +import ( + "testing" + + "code.gitea.io/gitea/models/unittest" + + _ "code.gitea.io/gitea/models" // register models + _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/system" // register models of system +) + +func TestMain(m *testing.M) { + unittest.MainTest(m) +} diff --git a/models/system/notice.go b/models/system/notice.go new file mode 100644 index 00000000..e7ec6a96 --- /dev/null +++ b/models/system/notice.go @@ -0,0 +1,128 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system + +import ( + "context" + "fmt" + "time" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/storage" + "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" +) + +// NoticeType describes the notice type +type NoticeType int + +const ( + // NoticeRepository type + NoticeRepository NoticeType = iota + 1 + // NoticeTask type + NoticeTask +) + +// Notice represents a system notice for admin. +type Notice struct { + ID int64 `xorm:"pk autoincr"` + Type NoticeType + Description string `xorm:"TEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` +} + +func init() { + db.RegisterModel(new(Notice)) +} + +// TrStr returns a translation format string. +func (n *Notice) TrStr() string { + return fmt.Sprintf("admin.notices.type_%d", n.Type) +} + +// CreateNotice creates new system notice. +func CreateNotice(ctx context.Context, tp NoticeType, desc string, args ...any) error { + if len(args) > 0 { + desc = fmt.Sprintf(desc, args...) + } + n := &Notice{ + Type: tp, + Description: desc, + } + return db.Insert(ctx, n) +} + +// CreateRepositoryNotice creates new system notice with type NoticeRepository. +func CreateRepositoryNotice(desc string, args ...any) error { + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + return CreateNotice(db.DefaultContext, NoticeRepository, desc, args...) +} + +// RemoveAllWithNotice removes all directories in given path and +// creates a system notice when error occurs. +func RemoveAllWithNotice(ctx context.Context, title, path string) { + if err := util.RemoveAll(path); err != nil { + desc := fmt.Sprintf("%s [%s]: %v", title, path, err) + log.Warn(title+" [%s]: %v", path, err) + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { + log.Error("CreateRepositoryNotice: %v", err) + } + } +} + +// RemoveStorageWithNotice removes a file from the storage and +// creates a system notice when error occurs. +func RemoveStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) { + if err := bucket.Delete(path); err != nil { + desc := fmt.Sprintf("%s [%s]: %v", title, path, err) + log.Warn(title+" [%s]: %v", path, err) + + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { + log.Error("CreateRepositoryNotice: %v", err) + } + } +} + +// CountNotices returns number of notices. +func CountNotices(ctx context.Context) int64 { + count, _ := db.GetEngine(ctx).Count(new(Notice)) + return count +} + +// Notices returns notices in given page. +func Notices(ctx context.Context, page, pageSize int) ([]*Notice, error) { + notices := make([]*Notice, 0, pageSize) + return notices, db.GetEngine(ctx). + Limit(pageSize, (page-1)*pageSize). + Desc("created_unix"). + Find(¬ices) +} + +// DeleteNotices deletes all notices with ID from start to end (inclusive). +func DeleteNotices(ctx context.Context, start, end int64) error { + if start == 0 && end == 0 { + _, err := db.GetEngine(ctx).Exec("DELETE FROM notice") + return err + } + + sess := db.GetEngine(ctx).Where("id >= ?", start) + if end > 0 { + sess.And("id <= ?", end) + } + _, err := sess.Delete(new(Notice)) + return err +} + +// DeleteOldSystemNotices deletes all old system notices from database. +func DeleteOldSystemNotices(ctx context.Context, olderThan time.Duration) (err error) { + if olderThan <= 0 { + return nil + } + + _, err = db.GetEngine(ctx).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Notice{}) + return err +} diff --git a/models/system/notice_test.go b/models/system/notice_test.go new file mode 100644 index 00000000..bfb7862f --- /dev/null +++ b/models/system/notice_test.go @@ -0,0 +1,110 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system_test + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/system" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNotice_TrStr(t *testing.T) { + notice := &system.Notice{ + Type: system.NoticeRepository, + Description: "test description", + } + assert.Equal(t, "admin.notices.type_1", notice.TrStr()) +} + +func TestCreateNotice(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + noticeBean := &system.Notice{ + Type: system.NoticeRepository, + Description: "test description", + } + unittest.AssertNotExistsBean(t, noticeBean) + require.NoError(t, system.CreateNotice(db.DefaultContext, noticeBean.Type, noticeBean.Description)) + unittest.AssertExistsAndLoadBean(t, noticeBean) +} + +func TestCreateRepositoryNotice(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + noticeBean := &system.Notice{ + Type: system.NoticeRepository, + Description: "test description", + } + unittest.AssertNotExistsBean(t, noticeBean) + require.NoError(t, system.CreateRepositoryNotice(noticeBean.Description)) + unittest.AssertExistsAndLoadBean(t, noticeBean) +} + +// TODO TestRemoveAllWithNotice + +func TestCountNotices(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + assert.Equal(t, int64(3), system.CountNotices(db.DefaultContext)) +} + +func TestNotices(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + notices, err := system.Notices(db.DefaultContext, 1, 2) + require.NoError(t, err) + if assert.Len(t, notices, 2) { + assert.Equal(t, int64(3), notices[0].ID) + assert.Equal(t, int64(2), notices[1].ID) + } + + notices, err = system.Notices(db.DefaultContext, 2, 2) + require.NoError(t, err) + if assert.Len(t, notices, 1) { + assert.Equal(t, int64(1), notices[0].ID) + } +} + +func TestDeleteNotices(t *testing.T) { + // delete a non-empty range + require.NoError(t, unittest.PrepareTestDatabase()) + + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) + require.NoError(t, system.DeleteNotices(db.DefaultContext, 1, 2)) + unittest.AssertNotExistsBean(t, &system.Notice{ID: 1}) + unittest.AssertNotExistsBean(t, &system.Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) +} + +func TestDeleteNotices2(t *testing.T) { + // delete an empty range + require.NoError(t, unittest.PrepareTestDatabase()) + + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) + require.NoError(t, system.DeleteNotices(db.DefaultContext, 3, 2)) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) +} + +func TestDeleteNoticesByIDs(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 3}) + err := db.DeleteByIDs[system.Notice](db.DefaultContext, 1, 3) + require.NoError(t, err) + unittest.AssertNotExistsBean(t, &system.Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &system.Notice{ID: 2}) + unittest.AssertNotExistsBean(t, &system.Notice{ID: 3}) +} diff --git a/models/system/setting.go b/models/system/setting.go new file mode 100644 index 00000000..4472b4c2 --- /dev/null +++ b/models/system/setting.go @@ -0,0 +1,152 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system + +import ( + "context" + "math" + "sync" + "time" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting/config" + "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/builder" +) + +type Setting struct { + ID int64 `xorm:"pk autoincr"` + SettingKey string `xorm:"varchar(255) unique"` // key should be lowercase + SettingValue string `xorm:"text"` + Version int `xorm:"version"` + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` +} + +// TableName sets the table name for the settings struct +func (s *Setting) TableName() string { + return "system_setting" +} + +func init() { + db.RegisterModel(new(Setting)) +} + +const keyRevision = "revision" + +func GetRevision(ctx context.Context) int { + revision, exist, err := db.Get[Setting](ctx, builder.Eq{"setting_key": keyRevision}) + if err != nil { + return 0 + } else if !exist { + err = db.Insert(ctx, &Setting{SettingKey: keyRevision, Version: 1}) + if err != nil { + return 0 + } + return 1 + } + if revision.Version <= 0 || revision.Version >= math.MaxInt-1 { + _, err = db.Exec(ctx, "UPDATE system_setting SET version=1 WHERE setting_key=?", keyRevision) + if err != nil { + return 0 + } + return 1 + } + return revision.Version +} + +func GetAllSettings(ctx context.Context) (revision int, res map[string]string, err error) { + _ = GetRevision(ctx) // prepare the "revision" key ahead + var settings []*Setting + if err := db.GetEngine(ctx). + Find(&settings); err != nil { + return 0, nil, err + } + res = make(map[string]string) + for _, s := range settings { + if s.SettingKey == keyRevision { + revision = s.Version + } + res[s.SettingKey] = s.SettingValue + } + return revision, res, nil +} + +func SetSettings(ctx context.Context, settings map[string]string) error { + _ = GetRevision(ctx) // prepare the "revision" key ahead + return db.WithTx(ctx, func(ctx context.Context) error { + e := db.GetEngine(ctx) + _, err := db.Exec(ctx, "UPDATE system_setting SET version=version+1 WHERE setting_key=?", keyRevision) + if err != nil { + return err + } + for k, v := range settings { + res, err := e.Exec("UPDATE system_setting SET version=version+1, setting_value=? WHERE setting_key=?", v, k) + if err != nil { + return err + } + rows, _ := res.RowsAffected() + if rows == 0 { // if no existing row, insert a new row + if _, err = e.Insert(&Setting{SettingKey: k, SettingValue: v}); err != nil { + return err + } + } + } + return nil + }) +} + +type dbConfigCachedGetter struct { + mu sync.RWMutex + + cacheTime time.Time + revision int + settings map[string]string +} + +var _ config.DynKeyGetter = (*dbConfigCachedGetter)(nil) + +func (d *dbConfigCachedGetter) GetValue(ctx context.Context, key string) (v string, has bool) { + d.mu.RLock() + defer d.mu.RUnlock() + v, has = d.settings[key] + return v, has +} + +func (d *dbConfigCachedGetter) GetRevision(ctx context.Context) int { + d.mu.RLock() + cachedDuration := time.Since(d.cacheTime) + cachedRevision := d.revision + d.mu.RUnlock() + + if cachedDuration < time.Second { + return cachedRevision + } + + d.mu.Lock() + defer d.mu.Unlock() + if GetRevision(ctx) != d.revision { + rev, set, err := GetAllSettings(ctx) + if err != nil { + log.Error("Unable to get all settings: %v", err) + } else { + d.revision = rev + d.settings = set + } + } + d.cacheTime = time.Now() + return d.revision +} + +func (d *dbConfigCachedGetter) InvalidateCache() { + d.mu.Lock() + d.cacheTime = time.Time{} + d.mu.Unlock() +} + +func NewDatabaseDynKeyGetter() config.DynKeyGetter { + return &dbConfigCachedGetter{} +} diff --git a/models/system/setting_test.go b/models/system/setting_test.go new file mode 100644 index 00000000..7a7fa02b --- /dev/null +++ b/models/system/setting_test.go @@ -0,0 +1,52 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package system_test + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/system" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSettings(t *testing.T) { + keyName := "test.key" + require.NoError(t, unittest.PrepareTestDatabase()) + + require.NoError(t, db.TruncateBeans(db.DefaultContext, &system.Setting{})) + + rev, settings, err := system.GetAllSettings(db.DefaultContext) + require.NoError(t, err) + assert.EqualValues(t, 1, rev) + assert.Len(t, settings, 1) // there is only one "revision" key + + err = system.SetSettings(db.DefaultContext, map[string]string{keyName: "true"}) + require.NoError(t, err) + rev, settings, err = system.GetAllSettings(db.DefaultContext) + require.NoError(t, err) + assert.EqualValues(t, 2, rev) + assert.Len(t, settings, 2) + assert.EqualValues(t, "true", settings[keyName]) + + err = system.SetSettings(db.DefaultContext, map[string]string{keyName: "false"}) + require.NoError(t, err) + rev, settings, err = system.GetAllSettings(db.DefaultContext) + require.NoError(t, err) + 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 + err = system.SetSettings(db.DefaultContext, map[string]string{keyName: "false"}) + require.NoError(t, err) + + rev, settings, err = system.GetAllSettings(db.DefaultContext) + require.NoError(t, err) + assert.Len(t, settings, 2) + assert.EqualValues(t, 4, rev) +} |