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 (
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
@ -37,6 +38,10 @@ type Mirror struct {
Address string `xorm:"-"`
}
func init() {
db.RegisterModel(new(Mirror))
}
// BeforeInsert will be invoked by XORM before inserting a record
func (m *Mirror) BeforeInsert() {
if m != nil {
@ -77,7 +82,7 @@ func (m *Mirror) ScheduleNextUpdate() {
}
}
func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
func getMirrorByRepoID(e db.Engine, repoID int64) (*Mirror, error) {
m := &Mirror{RepoID: repoID}
has, err := e.Get(m)
if err != nil {
@ -90,28 +95,28 @@ func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
// GetMirrorByRepoID returns mirror information of a repository.
func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
return getMirrorByRepoID(x, repoID)
return getMirrorByRepoID(db.DefaultContext().Engine(), repoID)
}
func updateMirror(e Engine, m *Mirror) error {
func updateMirror(e db.Engine, m *Mirror) error {
_, err := e.ID(m.ID).AllCols().Update(m)
return err
}
// UpdateMirror updates the mirror
func UpdateMirror(m *Mirror) error {
return updateMirror(x, m)
return updateMirror(db.DefaultContext().Engine(), m)
}
// DeleteMirrorByRepoID deletes a mirror by repoID
func DeleteMirrorByRepoID(repoID int64) error {
_, err := x.Delete(&Mirror{RepoID: repoID})
_, err := db.DefaultContext().Engine().Delete(&Mirror{RepoID: repoID})
return err
}
// MirrorsIterate iterates all mirror repositories.
func MirrorsIterate(f func(idx int, bean interface{}) error) error {
return x.
return db.DefaultContext().Engine().
Where("next_update_unix<=?", time.Now().Unix()).
And("next_update_unix!=0").
Iterate(new(Mirror), f)
@ -119,6 +124,6 @@ func MirrorsIterate(f func(idx int, bean interface{}) error) error {
// InsertMirror inserts a mirror to database
func InsertMirror(mirror *Mirror) error {
_, err := x.Insert(mirror)
_, err := db.DefaultContext().Engine().Insert(mirror)
return err
}