1
0
Fork 0
forked from forgejo/forgejo

DBContext is just a Context (#17100)

* DBContext is just a Context

This PR removes some of the specialness from the DBContext and makes it context
This allows us to simplify the GetEngine code to wrap around any context in future
and means that we can change our loadRepo(e Engine) functions to simply take contexts.

Signed-off-by: Andrew Thornton <art27@cantab.net>

* fix unit tests

Signed-off-by: Andrew Thornton <art27@cantab.net>

* another place that needs to set the initial context

Signed-off-by: Andrew Thornton <art27@cantab.net>

* avoid race

Signed-off-by: Andrew Thornton <art27@cantab.net>

* change attachment error

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2021-09-23 16:45:36 +01:00 committed by GitHub
parent b22be7f594
commit 9302eba971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
129 changed files with 1112 additions and 1022 deletions

View file

@ -77,7 +77,7 @@ func TestGetParticipantIDsByIssue(t *testing.T) {
checkParticipants := func(issueID int64, userIDs []int) {
issue, err := GetIssueByID(issueID)
assert.NoError(t, err)
participants, err := issue.getParticipantIDsByIssue(db.DefaultContext().Engine())
participants, err := issue.getParticipantIDsByIssue(db.GetEngine(db.DefaultContext))
if assert.NoError(t, err) {
participantsIDs := make([]int, len(participants))
for i, uid := range participants {
@ -125,7 +125,7 @@ func TestUpdateIssueCols(t *testing.T) {
issue.Content = "This should have no effect"
now := time.Now().Unix()
assert.NoError(t, updateIssueCols(db.DefaultContext().Engine(), issue, "name"))
assert.NoError(t, updateIssueCols(db.GetEngine(db.DefaultContext), issue, "name"))
then := time.Now().Unix()
updatedIssue := db.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
@ -290,7 +290,7 @@ func TestIssue_loadTotalTimes(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
ms, err := GetIssueByID(2)
assert.NoError(t, err)
assert.NoError(t, ms.loadTotalTimes(db.DefaultContext().Engine()))
assert.NoError(t, ms.loadTotalTimes(db.GetEngine(db.DefaultContext)))
assert.Equal(t, int64(3682), ms.TotalTrackedTime)
}
@ -363,7 +363,7 @@ func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *Is
err := NewIssue(repo, &issue, nil, nil)
assert.NoError(t, err)
has, err := db.DefaultContext().Engine().ID(issue.ID).Get(&newIssue)
has, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Get(&newIssue)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, issue.Title, newIssue.Title)
@ -380,11 +380,11 @@ func TestIssue_InsertIssue(t *testing.T) {
// there are 5 issues and max index is 5 on repository 1, so this one should 6
issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6)
_, err := db.DefaultContext().Engine().ID(issue.ID).Delete(new(Issue))
_, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(Issue))
assert.NoError(t, err)
issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7)
_, err = db.DefaultContext().Engine().ID(issue.ID).Delete(new(Issue))
_, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(Issue))
assert.NoError(t, err)
}
@ -397,7 +397,7 @@ func TestIssue_ResolveMentions(t *testing.T) {
r := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository)
issue := &Issue{RepoID: r.ID}
d := db.AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User)
resolved, err := issue.ResolveMentionsByVisibility(db.DefaultContext(), d, mentions)
resolved, err := issue.ResolveMentionsByVisibility(db.DefaultContext, d, mentions)
assert.NoError(t, err)
ids := make([]int64, len(resolved))
for i, user := range resolved {