diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/repo_mirror.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
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>
Diffstat (limited to 'models/repo_mirror.go')
-rw-r--r-- | models/repo_mirror.go | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/models/repo_mirror.go b/models/repo_mirror.go index cd1f74cb24..b086b87cf1 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -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 } |