Skip to content

Commit

Permalink
Update issue_index to finish migration (go-gitea#16685) (go-gitea#16687)
Browse files Browse the repository at this point in the history
* update issue_index to finish migration

* One Func to RecalculateIssueIndexForRepo
  • Loading branch information
6543 authored and Gitea committed Aug 24, 2021
1 parent 54edf67 commit 530a672
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,31 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
return opts.Issue.addCrossReferences(e, doer, false)
}

// RecalculateIssueIndexForRepo create issue_index for repo if not exist and
// update it based on highest index of existing issues assigned to a repo
func RecalculateIssueIndexForRepo(repoID int64) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

if err := upsertResourceIndex(sess, "issue_index", repoID); err != nil {
return err
}

var max int64
if _, err := sess.Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `issue_index` SET max_index=? WHERE group_id=?", max, repoID); err != nil {
return err
}

return sess.Commit()
}

// NewIssue creates new issue with labels for repository.
func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
idx, err := GetNextResourceIndex("issue_index", repo.ID)
Expand Down
5 changes: 5 additions & 0 deletions modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ func (g *GiteaLocalUploader) Finish() error {
return ErrRepoNotCreated
}

// update issue_index
if err := models.RecalculateIssueIndexForRepo(g.repo.ID); err != nil {
return err
}

g.repo.Status = models.RepositoryReady
return models.UpdateRepositoryCols(g.repo, "status")
}

0 comments on commit 530a672

Please sign in to comment.