diff --git a/models/fixtures/project_board.yml b/models/fixtures/project_board.yml index 1bbb81baa5..3293dea6ed 100644 --- a/models/fixtures/project_board.yml +++ b/models/fixtures/project_board.yml @@ -4,7 +4,6 @@ title: To Do creator_id: 2 default: true - sorting: 0 created_unix: 1588117528 updated_unix: 1588117528 @@ -13,7 +12,6 @@ project_id: 1 title: In Progress creator_id: 2 - sorting: 1 created_unix: 1588117528 updated_unix: 1588117528 @@ -22,7 +20,6 @@ project_id: 1 title: Done creator_id: 2 - sorting: 2 created_unix: 1588117528 updated_unix: 1588117528 @@ -31,7 +28,6 @@ project_id: 4 title: Done creator_id: 2 - sorting: 0 created_unix: 1588117528 updated_unix: 1588117528 @@ -50,7 +46,6 @@ title: Backlog creator_id: 2 default: true - sorting: 1 created_unix: 1588117528 updated_unix: 1588117528 @@ -69,7 +64,6 @@ title: Backlog creator_id: 2 default: true - sorting: 0 created_unix: 1588117528 updated_unix: 1588117528 @@ -79,6 +73,5 @@ title: Uncategorized creator_id: 2 default: true - sorting: 1 created_unix: 1588117528 updated_unix: 1588117528 diff --git a/models/issues/issue_project.go b/models/issues/issue_project.go index a9b9286a47..3ae3358209 100644 --- a/models/issues/issue_project.go +++ b/models/issues/issue_project.go @@ -5,12 +5,12 @@ package issues import ( "context" - "database/sql" "fmt" "code.gitea.io/gitea/models/db" project_model "code.gitea.io/gitea/models/project" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/util" ) // LoadProject load the project the issue was assigned to @@ -132,22 +132,22 @@ func IssueAssignOrRemoveProject(ctx context.Context, issue *Issue, doer *user_mo return nil } - var maxSorting sql.NullInt64 - if _, err := db.GetEngine(ctx).Select("Max(sorting)").Table("project_issue"). + res := struct { + MaxSorting int64 + IssueCount int64 + }{} + if _, err := db.GetEngine(ctx).Select("max(sorting) as MaxSorting, count(*) as IssueCount").Table("project_issue"). Where("project_id=?", newProjectID). And("project_board_id=?", newColumnID). - Get(&maxSorting); err != nil { + Get(&res); err != nil { return err } - if maxSorting.Valid { - maxSorting.Int64++ - } - + newSorting := util.Iif(res.IssueCount > 0, res.MaxSorting+1, 0) return db.Insert(ctx, &project_model.ProjectIssue{ IssueID: issue.ID, ProjectID: newProjectID, ProjectBoardID: newColumnID, - Sorting: maxSorting.Int64, + Sorting: newSorting, }) }) }