From a4bfef265d9e512830350635a0489c2cdcd6508f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 19 Sep 2021 19:49:59 +0800 Subject: 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 --- models/repo_transfer.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'models/repo_transfer.go') diff --git a/models/repo_transfer.go b/models/repo_transfer.go index 09b6290293..2ede0fbbe7 100644 --- a/models/repo_transfer.go +++ b/models/repo_transfer.go @@ -8,6 +8,7 @@ import ( "fmt" "os" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -28,6 +29,10 @@ type RepoTransfer struct { UpdatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL updated"` } +func init() { + db.RegisterModel(new(RepoTransfer)) +} + // LoadAttributes fetches the transfer recipient from the database func (r *RepoTransfer) LoadAttributes() error { if r.Recipient == nil { @@ -93,7 +98,7 @@ func (r *RepoTransfer) CanUserAcceptTransfer(u *User) bool { func GetPendingRepositoryTransfer(repo *Repository) (*RepoTransfer, error) { transfer := new(RepoTransfer) - has, err := x.Where("repo_id = ? ", repo.ID).Get(transfer) + has, err := db.DefaultContext().Engine().Where("repo_id = ? ", repo.ID).Get(transfer) if err != nil { return nil, err } @@ -105,7 +110,7 @@ func GetPendingRepositoryTransfer(repo *Repository) (*RepoTransfer, error) { return transfer, nil } -func deleteRepositoryTransfer(e Engine, repoID int64) error { +func deleteRepositoryTransfer(e db.Engine, repoID int64) error { _, err := e.Where("repo_id = ?", repoID).Delete(&RepoTransfer{}) return err } @@ -113,7 +118,7 @@ func deleteRepositoryTransfer(e Engine, repoID int64) error { // CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry, // thus cancel the transfer process. func CancelRepositoryTransfer(repo *Repository) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -145,7 +150,7 @@ func TestRepositoryReadyForTransfer(status RepositoryStatus) error { // CreatePendingRepositoryTransfer transfer a repo from one owner to a new one. // it marks the repository transfer as "pending" func CreatePendingRepositoryTransfer(doer, newOwner *User, repoID int64, teams []*Team) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -227,7 +232,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e } }() - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return fmt.Errorf("sess.Begin: %v", err) -- cgit v1.2.3