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

@ -12,6 +12,7 @@ import (
"os"
"path"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@ -34,6 +35,10 @@ type Upload struct {
Name string
}
func init() {
db.RegisterModel(new(Upload))
}
// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
func UploadLocalPath(uuid string) string {
return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
@ -68,7 +73,7 @@ func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err err
return nil, fmt.Errorf("Copy: %v", err)
}
if _, err := x.Insert(upload); err != nil {
if _, err := db.DefaultContext().Engine().Insert(upload); err != nil {
return nil, err
}
@ -78,7 +83,7 @@ func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err err
// GetUploadByUUID returns the Upload by UUID
func GetUploadByUUID(uuid string) (*Upload, error) {
upload := &Upload{}
has, err := x.Where("uuid=?", uuid).Get(upload)
has, err := db.DefaultContext().Engine().Where("uuid=?", uuid).Get(upload)
if err != nil {
return nil, err
} else if !has {
@ -95,7 +100,7 @@ func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
// Silently drop invalid uuids.
uploads := make([]*Upload, 0, len(uuids))
return uploads, x.In("uuid", uuids).Find(&uploads)
return uploads, db.DefaultContext().Engine().In("uuid", uuids).Find(&uploads)
}
// DeleteUploads deletes multiple uploads
@ -104,7 +109,7 @@ func DeleteUploads(uploads ...*Upload) (err error) {
return nil
}
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err