1
0
Fork 0
forked from forgejo/forgejo

Move db related basic functions to models/db (#17075)

* Move db related basic functions to models/db

* Fix lint

* Fix lint

* Fix test

* Fix lint

* Fix lint

* revert unnecessary change

* Fix test

* Fix wrong replace string

* Use *Context

* Correct committer spelling and fix wrong replaced words

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2021-09-19 19:49:59 +08:00 committed by GitHub
parent 462306e263
commit a4bfef265d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
335 changed files with 4191 additions and 3654 deletions

View file

@ -8,6 +8,7 @@ package models
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/timeutil"
@ -32,6 +33,10 @@ type Notice struct {
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)
@ -39,10 +44,10 @@ func (n *Notice) TrStr() string {
// CreateNotice creates new system notice.
func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
return createNotice(x, tp, desc, args...)
return createNotice(db.DefaultContext().Engine(), tp, desc, args...)
}
func createNotice(e Engine, tp NoticeType, desc string, args ...interface{}) error {
func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{}) error {
if len(args) > 0 {
desc = fmt.Sprintf(desc, args...)
}
@ -56,22 +61,22 @@ func createNotice(e Engine, tp NoticeType, desc string, args ...interface{}) err
// CreateRepositoryNotice creates new system notice with type NoticeRepository.
func CreateRepositoryNotice(desc string, args ...interface{}) error {
return createNotice(x, NoticeRepository, desc, args...)
return createNotice(db.DefaultContext().Engine(), NoticeRepository, desc, args...)
}
// RemoveAllWithNotice removes all directories in given path and
// creates a system notice when error occurs.
func RemoveAllWithNotice(title, path string) {
removeAllWithNotice(x, title, path)
removeAllWithNotice(db.DefaultContext().Engine(), 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(x, bucket, title, path)
removeStorageWithNotice(db.DefaultContext().Engine(), bucket, title, path)
}
func removeStorageWithNotice(e Engine, bucket storage.ObjectStorage, title, path string) {
func removeStorageWithNotice(e db.Engine, 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)
@ -81,7 +86,7 @@ func removeStorageWithNotice(e Engine, bucket storage.ObjectStorage, title, path
}
}
func removeAllWithNotice(e Engine, title, path string) {
func removeAllWithNotice(e db.Engine, 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)
@ -93,14 +98,14 @@ func removeAllWithNotice(e Engine, title, path string) {
// CountNotices returns number of notices.
func CountNotices() int64 {
count, _ := x.Count(new(Notice))
count, _ := db.DefaultContext().Engine().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, x.
return notices, db.DefaultContext().Engine().
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
@ -108,18 +113,18 @@ func Notices(page, pageSize int) ([]*Notice, error) {
// DeleteNotice deletes a system notice by given ID.
func DeleteNotice(id int64) error {
_, err := x.ID(id).Delete(new(Notice))
_, err := db.DefaultContext().Engine().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 := x.Exec("DELETE FROM notice")
_, err := db.DefaultContext().Engine().Exec("DELETE FROM notice")
return err
}
sess := x.Where("id >= ?", start)
sess := db.DefaultContext().Engine().Where("id >= ?", start)
if end > 0 {
sess.And("id <= ?", end)
}
@ -132,7 +137,7 @@ func DeleteNoticesByIDs(ids []int64) error {
if len(ids) == 0 {
return nil
}
_, err := x.
_, err := db.DefaultContext().Engine().
In("id", ids).
Delete(new(Notice))
return err
@ -141,7 +146,7 @@ func DeleteNoticesByIDs(ids []int64) error {
// GetAdminUser returns the first administrator
func GetAdminUser() (*User, error) {
var admin User
has, err := x.Where("is_admin=?", true).Get(&admin)
has, err := db.DefaultContext().Engine().Where("is_admin=?", true).Get(&admin)
if err != nil {
return nil, err
} else if !has {