diff options
Diffstat (limited to 'models/migrate.go')
-rw-r--r-- | models/migrate.go | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/models/migrate.go b/models/migrate.go index 18b1b11e46..efe4bd0c5e 100644 --- a/models/migrate.go +++ b/models/migrate.go @@ -9,7 +9,6 @@ import ( "code.gitea.io/gitea/modules/structs" "xorm.io/builder" - "xorm.io/xorm" ) // InsertMilestones creates milestones of repository. @@ -18,11 +17,12 @@ func InsertMilestones(ms ...*Milestone) (err error) { return nil } - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err = sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() + sess := db.GetEngine(ctx) // to return the id, so we should not use batch insert for _, m := range ms { @@ -31,29 +31,29 @@ func InsertMilestones(ms ...*Milestone) (err error) { } } - if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil { + if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil { return err } - return sess.Commit() + return committer.Commit() } // InsertIssues insert issues to database func InsertIssues(issues ...*Issue) error { - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err := sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() for _, issue := range issues { - if err := insertIssue(sess, issue); err != nil { + if err := insertIssue(db.GetEngine(ctx), issue); err != nil { return err } } - return sess.Commit() + return committer.Commit() } -func insertIssue(sess *xorm.Session, issue *Issue) error { +func insertIssue(sess db.Engine, issue *Issue) error { if _, err := sess.NoAutoTime().Insert(issue); err != nil { return err } @@ -144,13 +144,13 @@ func InsertIssueComments(comments []*Comment) error { issueIDs[comment.IssueID] = true } - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err := sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() for _, comment := range comments { - if _, err := sess.NoAutoTime().Insert(comment); err != nil { + if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil { return err } @@ -159,27 +159,28 @@ func InsertIssueComments(comments []*Comment) error { reaction.CommentID = comment.ID } if len(comment.Reactions) > 0 { - if _, err := sess.Insert(comment.Reactions); err != nil { + if err := db.Insert(ctx, comment.Reactions); err != nil { return err } } } for issueID := range issueIDs { - if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil { + if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil { return err } } - return sess.Commit() + return committer.Commit() } // InsertPullRequests inserted pull requests func InsertPullRequests(prs ...*PullRequest) error { - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err := sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() + sess := db.GetEngine(ctx) for _, pr := range prs { if err := insertIssue(sess, pr.Issue); err != nil { return err @@ -190,16 +191,17 @@ func InsertPullRequests(prs ...*PullRequest) error { } } - return sess.Commit() + return committer.Commit() } // InsertReleases migrates release func InsertReleases(rels ...*Release) error { - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err := sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() + sess := db.GetEngine(ctx) for _, rel := range rels { if _, err := sess.NoAutoTime().Insert(rel); err != nil { @@ -217,7 +219,7 @@ func InsertReleases(rels ...*Release) error { } } - return sess.Commit() + return committer.Commit() } func migratedIssueCond(tp structs.GitServiceType) builder.Cond { |