From 4a8169ce4674467e42ac248c919226c28f541cdd Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 13 Aug 2021 02:42:46 +0200 Subject: [PATCH 1/2] update issue_index to finish migration --- models/index.go | 7 +++++++ models/issue.go | 7 +++++++ modules/migrations/gitea_uploader.go | 14 ++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/models/index.go b/models/index.go index 18db13c490c7c..2408fd8b0567b 100644 --- a/models/index.go +++ b/models/index.go @@ -111,3 +111,10 @@ func getNextResourceIndex(tableName string, groupID int64) (int64, error) { } return curIdx, nil } + +// UpdateResourceIndex is used to set max_index to a specific value. +// !!! Only used to set max_index after repo migration !!! +func UpdateResourceIndex(tableName string, groupID, max int64) error { + _, err := x.Exec(fmt.Sprintf("UPDATE %s SET max_index=? WHERE group_id=?", tableName), max, groupID) + return err +} diff --git a/models/issue.go b/models/issue.go index 8b83612c2a4fd..22cd152410ce1 100644 --- a/models/issue.go +++ b/models/issue.go @@ -982,6 +982,13 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) { return opts.Issue.addCrossReferences(e, doer, false) } +// GetMaxIssueIndex return highest index of an issue based on repo id. +// !!! Only used it to calculate entry for issue_index on repo migration !!! +func GetMaxIssueIndex(repoID int64) (max int64, err error) { + _, err = x.Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max) + return +} + // 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) diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go index 2b18098b7f161..e3dfcf48f22f3 100644 --- a/modules/migrations/gitea_uploader.go +++ b/modules/migrations/gitea_uploader.go @@ -871,6 +871,20 @@ func (g *GiteaLocalUploader) Finish() error { return ErrRepoNotCreated } + // update issue_index + max, err := models.GetMaxIssueIndex(g.repo.ID) + if err != nil { + return err + } + + if _, err := models.GetNextResourceIndex("issue_index", g.repo.ID); err != nil { + return err + } + + if err := models.UpdateResourceIndex("issue_index", g.repo.ID, max); err != nil { + return err + } + g.repo.Status = models.RepositoryReady return models.UpdateRepositoryCols(g.repo, "status") } From a7bb4ed04eaa49671da28c4ed408047274894535 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 13 Aug 2021 14:25:34 +0200 Subject: [PATCH 2/2] One Func to RecalculateIssueIndexForRepo --- models/index.go | 7 ------- models/issue.go | 28 +++++++++++++++++++++++----- modules/migrations/gitea_uploader.go | 11 +---------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/models/index.go b/models/index.go index 2408fd8b0567b..18db13c490c7c 100644 --- a/models/index.go +++ b/models/index.go @@ -111,10 +111,3 @@ func getNextResourceIndex(tableName string, groupID int64) (int64, error) { } return curIdx, nil } - -// UpdateResourceIndex is used to set max_index to a specific value. -// !!! Only used to set max_index after repo migration !!! -func UpdateResourceIndex(tableName string, groupID, max int64) error { - _, err := x.Exec(fmt.Sprintf("UPDATE %s SET max_index=? WHERE group_id=?", tableName), max, groupID) - return err -} diff --git a/models/issue.go b/models/issue.go index 22cd152410ce1..804d7dc101bce 100644 --- a/models/issue.go +++ b/models/issue.go @@ -982,11 +982,29 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) { return opts.Issue.addCrossReferences(e, doer, false) } -// GetMaxIssueIndex return highest index of an issue based on repo id. -// !!! Only used it to calculate entry for issue_index on repo migration !!! -func GetMaxIssueIndex(repoID int64) (max int64, err error) { - _, err = x.Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max) - return +// 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. diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go index e3dfcf48f22f3..e852d0f88543b 100644 --- a/modules/migrations/gitea_uploader.go +++ b/modules/migrations/gitea_uploader.go @@ -872,16 +872,7 @@ func (g *GiteaLocalUploader) Finish() error { } // update issue_index - max, err := models.GetMaxIssueIndex(g.repo.ID) - if err != nil { - return err - } - - if _, err := models.GetNextResourceIndex("issue_index", g.repo.ID); err != nil { - return err - } - - if err := models.UpdateResourceIndex("issue_index", g.repo.ID, max); err != nil { + if err := models.RecalculateIssueIndexForRepo(g.repo.ID); err != nil { return err }