mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
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:
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
@@ -32,6 +33,10 @@ type PushMirror struct {
|
||||
LastError string `xorm:"text"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(PushMirror))
|
||||
}
|
||||
|
||||
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
||||
func (m *PushMirror) AfterLoad(session *xorm.Session) {
|
||||
if m == nil {
|
||||
@@ -57,32 +62,32 @@ func (m *PushMirror) GetRemoteName() string {
|
||||
|
||||
// InsertPushMirror inserts a push-mirror to database
|
||||
func InsertPushMirror(m *PushMirror) error {
|
||||
_, err := x.Insert(m)
|
||||
_, err := db.DefaultContext().Engine().Insert(m)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdatePushMirror updates the push-mirror
|
||||
func UpdatePushMirror(m *PushMirror) error {
|
||||
_, err := x.ID(m.ID).AllCols().Update(m)
|
||||
_, err := db.DefaultContext().Engine().ID(m.ID).AllCols().Update(m)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeletePushMirrorByID deletes a push-mirrors by ID
|
||||
func DeletePushMirrorByID(ID int64) error {
|
||||
_, err := x.ID(ID).Delete(&PushMirror{})
|
||||
_, err := db.DefaultContext().Engine().ID(ID).Delete(&PushMirror{})
|
||||
return err
|
||||
}
|
||||
|
||||
// DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
|
||||
func DeletePushMirrorsByRepoID(repoID int64) error {
|
||||
_, err := x.Delete(&PushMirror{RepoID: repoID})
|
||||
_, err := db.DefaultContext().Engine().Delete(&PushMirror{RepoID: repoID})
|
||||
return err
|
||||
}
|
||||
|
||||
// GetPushMirrorByID returns push-mirror information.
|
||||
func GetPushMirrorByID(ID int64) (*PushMirror, error) {
|
||||
m := &PushMirror{}
|
||||
has, err := x.ID(ID).Get(m)
|
||||
has, err := db.DefaultContext().Engine().ID(ID).Get(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
@@ -94,12 +99,12 @@ func GetPushMirrorByID(ID int64) (*PushMirror, error) {
|
||||
// GetPushMirrorsByRepoID returns push-mirror information of a repository.
|
||||
func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
|
||||
mirrors := make([]*PushMirror, 0, 10)
|
||||
return mirrors, x.Where("repo_id=?", repoID).Find(&mirrors)
|
||||
return mirrors, db.DefaultContext().Engine().Where("repo_id=?", repoID).Find(&mirrors)
|
||||
}
|
||||
|
||||
// PushMirrorsIterate iterates all push-mirror repositories.
|
||||
func PushMirrorsIterate(f func(idx int, bean interface{}) error) error {
|
||||
return x.
|
||||
return db.DefaultContext().Engine().
|
||||
Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
|
||||
And("`interval` != 0").
|
||||
Iterate(new(PushMirror), f)
|
||||
|
Reference in New Issue
Block a user