diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /models/project | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'models/project')
-rw-r--r-- | models/project/board.go | 41 | ||||
-rw-r--r-- | models/project/issue.go | 8 | ||||
-rw-r--r-- | models/project/project.go | 45 | ||||
-rw-r--r-- | models/project/project_test.go | 13 |
4 files changed, 40 insertions, 67 deletions
diff --git a/models/project/board.go b/models/project/board.go index f770a18f59..be7119ee4d 100644 --- a/models/project/board.go +++ b/models/project/board.go @@ -147,8 +147,7 @@ func DeleteBoardByID(boardID int64) error { } func deleteBoardByID(ctx context.Context, boardID int64) error { - e := db.GetEngine(ctx) - board, err := getBoard(e, boardID) + board, err := GetBoard(ctx, boardID) if err != nil { if IsErrProjectBoardNotExist(err) { return nil @@ -157,30 +156,26 @@ func deleteBoardByID(ctx context.Context, boardID int64) error { return err } - if err = board.removeIssues(e); err != nil { + if err = board.removeIssues(ctx); err != nil { return err } - if _, err := e.ID(board.ID).Delete(board); err != nil { + if _, err := db.GetEngine(ctx).ID(board.ID).NoAutoCondition().Delete(board); err != nil { return err } return nil } -func deleteBoardByProjectID(e db.Engine, projectID int64) error { - _, err := e.Where("project_id=?", projectID).Delete(&Board{}) +func deleteBoardByProjectID(ctx context.Context, projectID int64) error { + _, err := db.GetEngine(ctx).Where("project_id=?", projectID).Delete(&Board{}) return err } // GetBoard fetches the current board of a project -func GetBoard(boardID int64) (*Board, error) { - return getBoard(db.GetEngine(db.DefaultContext), boardID) -} - -func getBoard(e db.Engine, boardID int64) (*Board, error) { +func GetBoard(ctx context.Context, boardID int64) (*Board, error) { board := new(Board) - has, err := e.ID(boardID).Get(board) + has, err := db.GetEngine(ctx).ID(boardID).Get(board) if err != nil { return nil, err } else if !has { @@ -191,11 +186,7 @@ func getBoard(e db.Engine, boardID int64) (*Board, error) { } // UpdateBoard updates a project board -func UpdateBoard(board *Board) error { - return updateBoard(db.GetEngine(db.DefaultContext), board) -} - -func updateBoard(e db.Engine, board *Board) error { +func UpdateBoard(ctx context.Context, board *Board) error { var fieldToUpdate []string if board.Sorting != 0 { @@ -211,25 +202,21 @@ func updateBoard(e db.Engine, board *Board) error { } fieldToUpdate = append(fieldToUpdate, "color") - _, err := e.ID(board.ID).Cols(fieldToUpdate...).Update(board) + _, err := db.GetEngine(ctx).ID(board.ID).Cols(fieldToUpdate...).Update(board) return err } // GetBoards fetches all boards related to a project // if no default board set, first board is a temporary "Uncategorized" board -func GetBoards(projectID int64) (BoardList, error) { - return getBoards(db.GetEngine(db.DefaultContext), projectID) -} - -func getBoards(e db.Engine, projectID int64) ([]*Board, error) { +func GetBoards(ctx context.Context, projectID int64) (BoardList, error) { boards := make([]*Board, 0, 5) - if err := e.Where("project_id=? AND `default`=?", projectID, false).OrderBy("Sorting").Find(&boards); err != nil { + if err := db.GetEngine(ctx).Where("project_id=? AND `default`=?", projectID, false).OrderBy("Sorting").Find(&boards); err != nil { return nil, err } - defaultB, err := getDefaultBoard(e, projectID) + defaultB, err := getDefaultBoard(ctx, projectID) if err != nil { return nil, err } @@ -238,9 +225,9 @@ func getBoards(e db.Engine, projectID int64) ([]*Board, error) { } // getDefaultBoard return default board and create a dummy if none exist -func getDefaultBoard(e db.Engine, projectID int64) (*Board, error) { +func getDefaultBoard(ctx context.Context, projectID int64) (*Board, error) { var board Board - exist, err := e.Where("project_id=? AND `default`=?", projectID, true).Get(&board) + exist, err := db.GetEngine(ctx).Where("project_id=? AND `default`=?", projectID, true).Get(&board) if err != nil { return nil, err } diff --git a/models/project/issue.go b/models/project/issue.go index 6bde91668f..04efc0e749 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -28,8 +28,8 @@ func init() { db.RegisterModel(new(ProjectIssue)) } -func deleteProjectIssuesByProjectID(e db.Engine, projectID int64) error { - _, err := e.Where("project_id=?", projectID).Delete(&ProjectIssue{}) +func deleteProjectIssuesByProjectID(ctx context.Context, projectID int64) error { + _, err := db.GetEngine(ctx).Where("project_id=?", projectID).Delete(&ProjectIssue{}) return err } @@ -97,7 +97,7 @@ func MoveIssuesOnProjectBoard(board *Board, sortedIssueIDs map[int64]int64) erro }) } -func (pb *Board) removeIssues(e db.Engine) error { - _, err := e.Exec("UPDATE `project_issue` SET project_board_id = 0 WHERE project_board_id = ? ", pb.ID) +func (pb *Board) removeIssues(ctx context.Context) error { + _, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id = 0 WHERE project_board_id = ? ", pb.ID) return err } diff --git a/models/project/project.go b/models/project/project.go index a639879e78..0aa37cc5c9 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -121,12 +121,7 @@ type SearchOptions struct { } // GetProjects returns a list of all projects that have been created in the repository -func GetProjects(opts SearchOptions) ([]*Project, int64, error) { - return GetProjectsCtx(db.DefaultContext, opts) -} - -// GetProjectsCtx returns a list of all projects that have been created in the repository -func GetProjectsCtx(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { +func GetProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { e := db.GetEngine(ctx) projects := make([]*Project, 0, setting.UI.IssuePagingNum) @@ -199,14 +194,10 @@ func NewProject(p *Project) error { } // GetProjectByID returns the projects in a repository -func GetProjectByID(id int64) (*Project, error) { - return getProjectByID(db.GetEngine(db.DefaultContext), id) -} - -func getProjectByID(e db.Engine, id int64) (*Project, error) { +func GetProjectByID(ctx context.Context, id int64) (*Project, error) { p := new(Project) - has, err := e.ID(id).Get(p) + has, err := db.GetEngine(ctx).ID(id).Get(p) if err != nil { return nil, err } else if !has { @@ -217,20 +208,16 @@ func getProjectByID(e db.Engine, id int64) (*Project, error) { } // UpdateProject updates project properties -func UpdateProject(p *Project) error { - return updateProject(db.GetEngine(db.DefaultContext), p) -} - -func updateProject(e db.Engine, p *Project) error { - _, err := e.ID(p.ID).Cols( +func UpdateProject(ctx context.Context, p *Project) error { + _, err := db.GetEngine(ctx).ID(p.ID).Cols( "title", "description", ).Update(p) return err } -func updateRepositoryProjectCount(e db.Engine, repoID int64) error { - if _, err := e.Exec(builder.Update( +func updateRepositoryProjectCount(ctx context.Context, repoID int64) error { + if _, err := db.GetEngine(ctx).Exec(builder.Update( builder.Eq{ "`num_projects`": builder.Select("count(*)").From("`project`"). Where(builder.Eq{"`project`.`repo_id`": repoID}. @@ -239,7 +226,7 @@ func updateRepositoryProjectCount(e db.Engine, repoID int64) error { return err } - if _, err := e.Exec(builder.Update( + if _, err := db.GetEngine(ctx).Exec(builder.Update( builder.Eq{ "`num_closed_projects`": builder.Select("count(*)").From("`project`"). Where(builder.Eq{"`project`.`repo_id`": repoID}. @@ -293,8 +280,7 @@ func ChangeProjectStatus(p *Project, isClosed bool) error { func changeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { p.IsClosed = isClosed p.ClosedDateUnix = timeutil.TimeStampNow() - e := db.GetEngine(ctx) - count, err := e.ID(p.ID).Where("repo_id = ? AND is_closed = ?", p.RepoID, !isClosed).Cols("is_closed", "closed_date_unix").Update(p) + count, err := db.GetEngine(ctx).ID(p.ID).Where("repo_id = ? AND is_closed = ?", p.RepoID, !isClosed).Cols("is_closed", "closed_date_unix").Update(p) if err != nil { return err } @@ -302,7 +288,7 @@ func changeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { return nil } - return updateRepositoryProjectCount(e, p.RepoID) + return updateRepositoryProjectCount(ctx, p.RepoID) } // DeleteProjectByID deletes a project from a repository. @@ -322,8 +308,7 @@ func DeleteProjectByID(id int64) error { // DeleteProjectByIDCtx deletes a project from a repository. func DeleteProjectByIDCtx(ctx context.Context, id int64) error { - e := db.GetEngine(ctx) - p, err := getProjectByID(e, id) + p, err := GetProjectByID(ctx, id) if err != nil { if IsErrProjectNotExist(err) { return nil @@ -331,17 +316,17 @@ func DeleteProjectByIDCtx(ctx context.Context, id int64) error { return err } - if err := deleteProjectIssuesByProjectID(e, id); err != nil { + if err := deleteProjectIssuesByProjectID(ctx, id); err != nil { return err } - if err := deleteBoardByProjectID(e, id); err != nil { + if err := deleteBoardByProjectID(ctx, id); err != nil { return err } - if _, err = e.ID(p.ID).Delete(new(Project)); err != nil { + if _, err = db.GetEngine(ctx).ID(p.ID).Delete(new(Project)); err != nil { return err } - return updateRepositoryProjectCount(e, p.RepoID) + return updateRepositoryProjectCount(ctx, p.RepoID) } diff --git a/models/project/project_test.go b/models/project/project_test.go index 211a890874..f33fb3351a 100644 --- a/models/project/project_test.go +++ b/models/project/project_test.go @@ -7,6 +7,7 @@ package project import ( "testing" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" @@ -34,13 +35,13 @@ func TestIsProjectTypeValid(t *testing.T) { func TestGetProjects(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - projects, _, err := GetProjects(SearchOptions{RepoID: 1}) + projects, _, err := GetProjects(db.DefaultContext, SearchOptions{RepoID: 1}) assert.NoError(t, err) // 1 value for this repo exists in the fixtures assert.Len(t, projects, 1) - projects, _, err = GetProjects(SearchOptions{RepoID: 3}) + projects, _, err = GetProjects(db.DefaultContext, SearchOptions{RepoID: 3}) assert.NoError(t, err) // 1 value for this repo exists in the fixtures @@ -61,14 +62,14 @@ func TestProject(t *testing.T) { assert.NoError(t, NewProject(project)) - _, err := GetProjectByID(project.ID) + _, err := GetProjectByID(db.DefaultContext, project.ID) assert.NoError(t, err) // Update project project.Title = "Updated title" - assert.NoError(t, UpdateProject(project)) + assert.NoError(t, UpdateProject(db.DefaultContext, project)) - projectFromDB, err := GetProjectByID(project.ID) + projectFromDB, err := GetProjectByID(db.DefaultContext, project.ID) assert.NoError(t, err) assert.Equal(t, project.Title, projectFromDB.Title) @@ -76,7 +77,7 @@ func TestProject(t *testing.T) { assert.NoError(t, ChangeProjectStatus(project, true)) // Retrieve from DB afresh to check if it is truly closed - projectFromDB, err = GetProjectByID(project.ID) + projectFromDB, err = GetProjectByID(db.DefaultContext, project.ID) assert.NoError(t, err) assert.True(t, projectFromDB.IsClosed) |