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:
parent
462306e263
commit
a4bfef265d
335 changed files with 4191 additions and 3654 deletions
|
@ -7,6 +7,8 @@ package models
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
|
@ -20,7 +22,11 @@ type ProjectIssue struct {
|
|||
ProjectBoardID int64 `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
func deleteProjectIssuesByProjectID(e Engine, projectID int64) error {
|
||||
func init() {
|
||||
db.RegisterModel(new(ProjectIssue))
|
||||
}
|
||||
|
||||
func deleteProjectIssuesByProjectID(e db.Engine, projectID int64) error {
|
||||
_, err := e.Where("project_id=?", projectID).Delete(&ProjectIssue{})
|
||||
return err
|
||||
}
|
||||
|
@ -33,10 +39,10 @@ func deleteProjectIssuesByProjectID(e Engine, projectID int64) error {
|
|||
|
||||
// LoadProject load the project the issue was assigned to
|
||||
func (i *Issue) LoadProject() (err error) {
|
||||
return i.loadProject(x)
|
||||
return i.loadProject(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
func (i *Issue) loadProject(e Engine) (err error) {
|
||||
func (i *Issue) loadProject(e db.Engine) (err error) {
|
||||
if i.Project == nil {
|
||||
var p Project
|
||||
if _, err = e.Table("project").
|
||||
|
@ -52,10 +58,10 @@ func (i *Issue) loadProject(e Engine) (err error) {
|
|||
|
||||
// ProjectID return project id if issue was assigned to one
|
||||
func (i *Issue) ProjectID() int64 {
|
||||
return i.projectID(x)
|
||||
return i.projectID(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
func (i *Issue) projectID(e Engine) int64 {
|
||||
func (i *Issue) projectID(e db.Engine) int64 {
|
||||
var ip ProjectIssue
|
||||
has, err := e.Where("issue_id=?", i.ID).Get(&ip)
|
||||
if err != nil || !has {
|
||||
|
@ -66,10 +72,10 @@ func (i *Issue) projectID(e Engine) int64 {
|
|||
|
||||
// ProjectBoardID return project board id if issue was assigned to one
|
||||
func (i *Issue) ProjectBoardID() int64 {
|
||||
return i.projectBoardID(x)
|
||||
return i.projectBoardID(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
func (i *Issue) projectBoardID(e Engine) int64 {
|
||||
func (i *Issue) projectBoardID(e db.Engine) int64 {
|
||||
var ip ProjectIssue
|
||||
has, err := e.Where("issue_id=?", i.ID).Get(&ip)
|
||||
if err != nil || !has {
|
||||
|
@ -87,7 +93,7 @@ func (i *Issue) projectBoardID(e Engine) int64 {
|
|||
|
||||
// NumIssues return counter of all issues assigned to a project
|
||||
func (p *Project) NumIssues() int {
|
||||
c, err := x.Table("project_issue").
|
||||
c, err := db.DefaultContext().Engine().Table("project_issue").
|
||||
Where("project_id=?", p.ID).
|
||||
GroupBy("issue_id").
|
||||
Cols("issue_id").
|
||||
|
@ -100,7 +106,7 @@ func (p *Project) NumIssues() int {
|
|||
|
||||
// NumClosedIssues return counter of closed issues assigned to a project
|
||||
func (p *Project) NumClosedIssues() int {
|
||||
c, err := x.Table("project_issue").
|
||||
c, err := db.DefaultContext().Engine().Table("project_issue").
|
||||
Join("INNER", "issue", "project_issue.issue_id=issue.id").
|
||||
Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, true).
|
||||
Cols("issue_id").
|
||||
|
@ -113,7 +119,7 @@ func (p *Project) NumClosedIssues() int {
|
|||
|
||||
// NumOpenIssues return counter of open issues assigned to a project
|
||||
func (p *Project) NumOpenIssues() int {
|
||||
c, err := x.Table("project_issue").
|
||||
c, err := db.DefaultContext().Engine().Table("project_issue").
|
||||
Join("INNER", "issue", "project_issue.issue_id=issue.id").
|
||||
Where("project_issue.project_id=? AND issue.is_closed=?", p.ID, false).Count("issue.id")
|
||||
if err != nil {
|
||||
|
@ -124,7 +130,7 @@ func (p *Project) NumOpenIssues() int {
|
|||
|
||||
// ChangeProjectAssign changes the project associated with an issue
|
||||
func ChangeProjectAssign(issue *Issue, doer *User, newProjectID int64) error {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
|
@ -177,7 +183,7 @@ func addUpdateIssueProject(e *xorm.Session, issue *Issue, doer *User, newProject
|
|||
|
||||
// MoveIssueAcrossProjectBoards move a card from one board to another
|
||||
func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
|
@ -201,7 +207,7 @@ func MoveIssueAcrossProjectBoards(issue *Issue, board *ProjectBoard) error {
|
|||
return sess.Commit()
|
||||
}
|
||||
|
||||
func (pb *ProjectBoard) removeIssues(e Engine) error {
|
||||
func (pb *ProjectBoard) removeIssues(e db.Engine) error {
|
||||
_, err := e.Exec("UPDATE `project_issue` SET project_board_id = 0 WHERE project_board_id = ? ", pb.ID)
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue