aboutsummaryrefslogtreecommitdiffstats
path: root/models/issues
diff options
context:
space:
mode:
Diffstat (limited to 'models/issues')
-rw-r--r--models/issues/content_history.go13
-rw-r--r--models/issues/content_history_test.go21
-rw-r--r--models/issues/milestone.go26
-rw-r--r--models/issues/milestone_test.go4
4 files changed, 31 insertions, 33 deletions
diff --git a/models/issues/content_history.go b/models/issues/content_history.go
index 13aadcb1ea..4c5af13db7 100644
--- a/models/issues/content_history.go
+++ b/models/issues/content_history.go
@@ -38,7 +38,7 @@ func init() {
}
// SaveIssueContentHistory save history
-func SaveIssueContentHistory(e db.Engine, posterID, issueID, commentID int64, editTime timeutil.TimeStamp, contentText string, isFirstCreated bool) error {
+func SaveIssueContentHistory(ctx context.Context, posterID, issueID, commentID int64, editTime timeutil.TimeStamp, contentText string, isFirstCreated bool) error {
ch := &ContentHistory{
PosterID: posterID,
IssueID: issueID,
@@ -47,27 +47,26 @@ func SaveIssueContentHistory(e db.Engine, posterID, issueID, commentID int64, ed
EditedUnix: editTime,
IsFirstCreated: isFirstCreated,
}
- _, err := e.Insert(ch)
- if err != nil {
+ if err := db.Insert(ctx, ch); err != nil {
log.Error("can not save issue content history. err=%v", err)
return err
}
// We only keep at most 20 history revisions now. It is enough in most cases.
// If there is a special requirement to keep more, we can consider introducing a new setting option then, but not now.
- keepLimitedContentHistory(e, issueID, commentID, 20)
+ keepLimitedContentHistory(ctx, issueID, commentID, 20)
return nil
}
// keepLimitedContentHistory keeps at most `limit` history revisions, it will hard delete out-dated revisions, sorting by revision interval
// we can ignore all errors in this function, so we just log them
-func keepLimitedContentHistory(e db.Engine, issueID, commentID int64, limit int) {
+func keepLimitedContentHistory(ctx context.Context, issueID, commentID int64, limit int) {
type IDEditTime struct {
ID int64
EditedUnix timeutil.TimeStamp
}
var res []*IDEditTime
- err := e.Select("id, edited_unix").Table("issue_content_history").
+ err := db.GetEngine(ctx).Select("id, edited_unix").Table("issue_content_history").
Where(builder.Eq{"issue_id": issueID, "comment_id": commentID}).
OrderBy("edited_unix ASC").
Find(&res)
@@ -96,7 +95,7 @@ func keepLimitedContentHistory(e db.Engine, issueID, commentID int64, limit int)
}
// hard delete the found one
- _, err = e.Delete(&ContentHistory{ID: res[indexToDelete].ID})
+ _, err = db.GetEngine(ctx).Delete(&ContentHistory{ID: res[indexToDelete].ID})
if err != nil {
log.Error("can not delete out-dated content history, err=%v", err)
break
diff --git a/models/issues/content_history_test.go b/models/issues/content_history_test.go
index 71ccc6e6a7..3cbc0ad5e0 100644
--- a/models/issues/content_history_test.go
+++ b/models/issues/content_history_test.go
@@ -18,18 +18,17 @@ func TestContentHistory(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
dbCtx := db.DefaultContext
- dbEngine := db.GetEngine(dbCtx)
timeStampNow := timeutil.TimeStampNow()
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 0, timeStampNow, "i-a", true)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 0, timeStampNow.Add(2), "i-b", false)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 0, timeStampNow.Add(7), "i-c", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow, "i-a", true)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(2), "i-b", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(7), "i-c", false)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 100, timeStampNow, "c-a", true)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 100, timeStampNow.Add(5), "c-b", false)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 100, timeStampNow.Add(20), "c-c", false)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 100, timeStampNow.Add(50), "c-d", false)
- _ = SaveIssueContentHistory(dbEngine, 1, 10, 100, timeStampNow.Add(51), "c-e", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow, "c-a", true)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(5), "c-b", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(20), "c-c", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(50), "c-d", false)
+ _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(51), "c-e", false)
h1, _ := GetIssueContentHistoryByID(dbCtx, 1)
assert.EqualValues(t, 1, h1.ID)
@@ -47,7 +46,7 @@ func TestContentHistory(t *testing.T) {
Name string
FullName string
}
- _ = dbEngine.Sync2(&User{})
+ _ = db.GetEngine(dbCtx).Sync2(&User{})
list1, _ := FetchIssueContentHistoryList(dbCtx, 10, 0)
assert.Len(t, list1, 3)
@@ -70,7 +69,7 @@ func TestContentHistory(t *testing.T) {
assert.EqualValues(t, 4, h6Prev.ID)
// only keep 3 history revisions for comment_id=100, the first and the last should never be deleted
- keepLimitedContentHistory(dbEngine, 10, 100, 3)
+ keepLimitedContentHistory(dbCtx, 10, 100, 3)
list1, _ = FetchIssueContentHistoryList(dbCtx, 10, 0)
assert.Len(t, list1, 3)
list2, _ = FetchIssueContentHistoryList(dbCtx, 10, 100)
diff --git a/models/issues/milestone.go b/models/issues/milestone.go
index 07c38754d4..f7172f6448 100644
--- a/models/issues/milestone.go
+++ b/models/issues/milestone.go
@@ -292,11 +292,11 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
return err
}
- numMilestones, err := countRepoMilestones(sess, repo.ID)
+ numMilestones, err := countRepoMilestones(ctx, repo.ID)
if err != nil {
return err
}
- numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
+ numClosedMilestones, err := countRepoClosedMilestones(ctx, repo.ID)
if err != nil {
return err
}
@@ -503,21 +503,21 @@ func GetMilestonesStatsByRepoCondAndKw(repoCond builder.Cond, keyword string) (*
return stats, nil
}
-func countRepoMilestones(e db.Engine, repoID int64) (int64, error) {
- return e.
+func countRepoMilestones(ctx context.Context, repoID int64) (int64, error) {
+ return db.GetEngine(ctx).
Where("repo_id=?", repoID).
Count(new(Milestone))
}
-func countRepoClosedMilestones(e db.Engine, repoID int64) (int64, error) {
- return e.
+func countRepoClosedMilestones(ctx context.Context, repoID int64) (int64, error) {
+ return db.GetEngine(ctx).
Where("repo_id=? AND is_closed=?", repoID, true).
Count(new(Milestone))
}
// CountRepoClosedMilestones returns number of closed milestones in given repository.
func CountRepoClosedMilestones(repoID int64) (int64, error) {
- return countRepoClosedMilestones(db.GetEngine(db.DefaultContext), repoID)
+ return countRepoClosedMilestones(db.DefaultContext, repoID)
}
// CountMilestonesByRepoCond map from repo conditions to number of milestones matching the options`
@@ -590,7 +590,7 @@ func updateRepoMilestoneNum(ctx context.Context, repoID int64) error {
// |_||_| \__,_|\___|_|\_\___|\__,_| |_| |_|_| |_| |_|\___||___/
//
-func (milestones MilestoneList) loadTotalTrackedTimes(e db.Engine) error {
+func (milestones MilestoneList) loadTotalTrackedTimes(ctx context.Context) error {
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
@@ -601,7 +601,7 @@ func (milestones MilestoneList) loadTotalTrackedTimes(e db.Engine) error {
trackedTimes := make(map[int64]int64, len(milestones))
// Get total tracked time by milestone_id
- rows, err := e.Table("issue").
+ rows, err := db.GetEngine(ctx).Table("issue").
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
Where("tracked_time.deleted = ?", false).
@@ -630,13 +630,13 @@ func (milestones MilestoneList) loadTotalTrackedTimes(e db.Engine) error {
return nil
}
-func (m *Milestone) loadTotalTrackedTime(e db.Engine) error {
+func (m *Milestone) loadTotalTrackedTime(ctx context.Context) error {
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
}
totalTime := &totalTimesByMilestone{MilestoneID: m.ID}
- has, err := e.Table("issue").
+ has, err := db.GetEngine(ctx).Table("issue").
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
Where("tracked_time.deleted = ?", false).
@@ -655,10 +655,10 @@ func (m *Milestone) loadTotalTrackedTime(e db.Engine) error {
// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
func (milestones MilestoneList) LoadTotalTrackedTimes() error {
- return milestones.loadTotalTrackedTimes(db.GetEngine(db.DefaultContext))
+ return milestones.loadTotalTrackedTimes(db.DefaultContext)
}
// LoadTotalTrackedTime loads the tracked time for the milestone
func (m *Milestone) LoadTotalTrackedTime() error {
- return m.loadTotalTrackedTime(db.GetEngine(db.DefaultContext))
+ return m.loadTotalTrackedTime(db.DefaultContext)
}
diff --git a/models/issues/milestone_test.go b/models/issues/milestone_test.go
index 09f51de45c..e087318320 100644
--- a/models/issues/milestone_test.go
+++ b/models/issues/milestone_test.go
@@ -149,7 +149,7 @@ func TestCountRepoMilestones(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(repoID int64) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
- count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), repoID)
+ count, err := countRepoMilestones(db.DefaultContext, repoID)
assert.NoError(t, err)
assert.EqualValues(t, repo.NumMilestones, count)
}
@@ -157,7 +157,7 @@ func TestCountRepoMilestones(t *testing.T) {
test(2)
test(3)
- count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), unittest.NonexistentID)
+ count, err := countRepoMilestones(db.DefaultContext, unittest.NonexistentID)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
}