summaryrefslogtreecommitdiffstats
path: root/models/admin.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-09-23 16:45:36 +0100
committerGitHub <noreply@github.com>2021-09-23 23:45:36 +0800
commit9302eba971611601c3ebf6024e22a11c63f4e151 (patch)
treea3e5583986161ef62e7affc694098279ecf2217d /models/admin.go
parentb22be7f594401d7bd81196750456ce52185bd391 (diff)
downloadgitea-9302eba971611601c3ebf6024e22a11c63f4e151.tar.gz
gitea-9302eba971611601c3ebf6024e22a11c63f4e151.zip
DBContext is just a Context (#17100)
* DBContext is just a Context This PR removes some of the specialness from the DBContext and makes it context This allows us to simplify the GetEngine code to wrap around any context in future and means that we can change our loadRepo(e Engine) functions to simply take contexts. Signed-off-by: Andrew Thornton <art27@cantab.net> * fix unit tests Signed-off-by: Andrew Thornton <art27@cantab.net> * another place that needs to set the initial context Signed-off-by: Andrew Thornton <art27@cantab.net> * avoid race Signed-off-by: Andrew Thornton <art27@cantab.net> * change attachment error Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models/admin.go')
-rw-r--r--models/admin.go22
1 files changed, 11 insertions, 11 deletions
diff --git a/models/admin.go b/models/admin.go
index 084942ff5e..27a2032e2c 100644
--- a/models/admin.go
+++ b/models/admin.go
@@ -44,7 +44,7 @@ func (n *Notice) TrStr() string {
// CreateNotice creates new system notice.
func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
- return createNotice(db.DefaultContext().Engine(), tp, desc, args...)
+ return createNotice(db.GetEngine(db.DefaultContext), tp, desc, args...)
}
func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{}) error {
@@ -61,19 +61,19 @@ func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{})
// CreateRepositoryNotice creates new system notice with type NoticeRepository.
func CreateRepositoryNotice(desc string, args ...interface{}) error {
- return createNotice(db.DefaultContext().Engine(), NoticeRepository, desc, args...)
+ return createNotice(db.GetEngine(db.DefaultContext), NoticeRepository, desc, args...)
}
// RemoveAllWithNotice removes all directories in given path and
// creates a system notice when error occurs.
func RemoveAllWithNotice(title, path string) {
- removeAllWithNotice(db.DefaultContext().Engine(), title, path)
+ removeAllWithNotice(db.GetEngine(db.DefaultContext), title, path)
}
// RemoveStorageWithNotice removes a file from the storage and
// creates a system notice when error occurs.
func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) {
- removeStorageWithNotice(db.DefaultContext().Engine(), bucket, title, path)
+ removeStorageWithNotice(db.GetEngine(db.DefaultContext), bucket, title, path)
}
func removeStorageWithNotice(e db.Engine, bucket storage.ObjectStorage, title, path string) {
@@ -98,14 +98,14 @@ func removeAllWithNotice(e db.Engine, title, path string) {
// CountNotices returns number of notices.
func CountNotices() int64 {
- count, _ := db.DefaultContext().Engine().Count(new(Notice))
+ count, _ := db.GetEngine(db.DefaultContext).Count(new(Notice))
return count
}
// Notices returns notices in given page.
func Notices(page, pageSize int) ([]*Notice, error) {
notices := make([]*Notice, 0, pageSize)
- return notices, db.DefaultContext().Engine().
+ return notices, db.GetEngine(db.DefaultContext).
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
@@ -113,18 +113,18 @@ func Notices(page, pageSize int) ([]*Notice, error) {
// DeleteNotice deletes a system notice by given ID.
func DeleteNotice(id int64) error {
- _, err := db.DefaultContext().Engine().ID(id).Delete(new(Notice))
+ _, err := db.GetEngine(db.DefaultContext).ID(id).Delete(new(Notice))
return err
}
// DeleteNotices deletes all notices with ID from start to end (inclusive).
func DeleteNotices(start, end int64) error {
if start == 0 && end == 0 {
- _, err := db.DefaultContext().Engine().Exec("DELETE FROM notice")
+ _, err := db.GetEngine(db.DefaultContext).Exec("DELETE FROM notice")
return err
}
- sess := db.DefaultContext().Engine().Where("id >= ?", start)
+ sess := db.GetEngine(db.DefaultContext).Where("id >= ?", start)
if end > 0 {
sess.And("id <= ?", end)
}
@@ -137,7 +137,7 @@ func DeleteNoticesByIDs(ids []int64) error {
if len(ids) == 0 {
return nil
}
- _, err := db.DefaultContext().Engine().
+ _, err := db.GetEngine(db.DefaultContext).
In("id", ids).
Delete(new(Notice))
return err
@@ -146,7 +146,7 @@ func DeleteNoticesByIDs(ids []int64) error {
// GetAdminUser returns the first administrator
func GetAdminUser() (*User, error) {
var admin User
- has, err := db.DefaultContext().Engine().Where("is_admin=?", true).Get(&admin)
+ has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
if err != nil {
return nil, err
} else if !has {