diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /services | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'services')
32 files changed, 144 insertions, 119 deletions
diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 7ae0a94d7e..6d4d46e4e0 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -14,6 +14,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" @@ -97,11 +98,11 @@ func (aReq *ArchiveRequest) GetArchiveName() string { } func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { - ctx, commiter, err := models.TxDBContext() + ctx, committer, err := db.TxContext() if err != nil { return nil, err } - defer commiter.Close() + defer committer.Close() archiver, err := models.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID) if err != nil { @@ -206,7 +207,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { } } - return archiver, commiter.Commit() + return archiver, committer.Commit() } // ArchiveRepository satisfies the ArchiveRequest being passed in. Processing diff --git a/services/archiver/archiver_test.go b/services/archiver/archiver_test.go index 3f3f369987..94b0423d9b 100644 --- a/services/archiver/archiver_test.go +++ b/services/archiver/archiver_test.go @@ -9,14 +9,14 @@ import ( "testing" "time" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func waitForCount(t *testing.T, num int) { @@ -24,7 +24,7 @@ func waitForCount(t *testing.T, num int) { } func TestArchive_Basic(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) ctx := test.MockContext(t, "user27/repo49") firstCommit, secondCommit := "51f84af23134", "aacbdfe9e1c4" diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index 4c356cd079..06f79be01b 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -10,6 +10,7 @@ import ( "io" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/upload" @@ -22,7 +23,7 @@ func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachmen return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name) } - err := models.WithTx(func(ctx models.DBContext) error { + err := db.WithTx(func(ctx *db.Context) error { attach.UUID = uuid.New().String() size, err := storage.Attachments.Save(attach.RelativePath(), file, -1) if err != nil { @@ -30,7 +31,7 @@ func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachmen } attach.Size = size - return models.Insert(ctx, attach) + return db.Insert(ctx, attach) }) return attach, err diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index c11204b221..3e9e55a8f2 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -10,18 +10,19 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func TestUploadAttachment(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) fPath := "./attachment_test.go" f, err := os.Open(fPath) diff --git a/services/auth/group.go b/services/auth/group.go index fb885b818a..c396ae046b 100644 --- a/services/auth/group.go +++ b/services/auth/group.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) // Ensure the struct implements the interface. @@ -60,7 +61,7 @@ func (b *Group) Free() error { // Verify extracts and validates func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User { - if !models.HasEngine { + if !db.HasEngine { return nil } diff --git a/services/auth/oauth2.go b/services/auth/oauth2.go index 665e5232cc..e79b640ce4 100644 --- a/services/auth/oauth2.go +++ b/services/auth/oauth2.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/web/middleware" @@ -109,7 +110,7 @@ func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 { // If verification is successful returns an existing user object. // Returns nil if verification fails. func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User { - if !models.HasEngine { + if !db.HasEngine { return nil } diff --git a/services/auth/signin.go b/services/auth/signin.go index 0ac2634c80..a7acb95ba2 100644 --- a/services/auth/signin.go +++ b/services/auth/signin.go @@ -8,6 +8,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" // Register the sources @@ -25,7 +26,7 @@ func UserSignIn(username, password string) (*models.User, *models.LoginSource, e if strings.Contains(username, "@") { user = &models.User{Email: strings.ToLower(strings.TrimSpace(username))} // check same email - cnt, err := models.Count(user) + cnt, err := db.Count(user) if err != nil { return nil, nil, err } diff --git a/services/auth/source/oauth2/init.go b/services/auth/source/oauth2/init.go index 64328aa381..be31503eef 100644 --- a/services/auth/source/oauth2/init.go +++ b/services/auth/source/oauth2/init.go @@ -9,6 +9,7 @@ import ( "sync" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -33,7 +34,7 @@ func Init() error { return err } - store, err := models.CreateStore(SessionTableName, UsersStoreKey) + store, err := db.CreateStore(SessionTableName, UsersStoreKey) if err != nil { return err } diff --git a/services/comments/comments.go b/services/comments/comments.go index f8bdc8153b..901b82e380 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -6,6 +6,7 @@ package comments import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/notification" ) @@ -22,7 +23,7 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model if err != nil { return nil, err } - mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content) + mentions, err := issue.FindAndUpdateIssueMentions(db.DefaultContext(), doer, comment.Content) if err != nil { return nil, err } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 65d0dab4ca..239358789a 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -23,6 +23,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" @@ -1122,7 +1123,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio oid := strings.TrimPrefix(line[1:], lfs.MetaFileOidPrefix) if len(oid) == 64 { m := &models.LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}} - count, err := models.Count(m) + count, err := db.Count(m) if err == nil && count > 0 { curFile.IsBin = true diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 57364ace3e..0d8d60d94f 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -13,6 +13,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/json" @@ -492,10 +493,10 @@ func setupDefaultDiff() *Diff { } } func TestDiff_LoadComments(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) diff := setupDefaultDiff() assert.NoError(t, diff.LoadComments(issue, user)) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) diff --git a/services/gitdiff/main_test.go b/services/gitdiff/main_test.go index 5ed3c75b70..1b83cbd684 100644 --- a/services/gitdiff/main_test.go +++ b/services/gitdiff/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index 2d96368ec7..5684ed6d89 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestDeleteNotPassedAssignee(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) // Fake issue with assignees issue, err := models.GetIssueWithAttrsByID(1) diff --git a/services/issue/issue.go b/services/issue/issue.go index 90f689a55c..b2ac24e088 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -6,6 +6,7 @@ package issue import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/util" @@ -23,7 +24,7 @@ func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uu } } - mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), issue.Poster, issue.Content) + mentions, err := issue.FindAndUpdateIssueMentions(db.DefaultContext(), issue.Poster, issue.Content) if err != nil { return err } diff --git a/services/issue/label_test.go b/services/issue/label_test.go index 0652028943..8a3a77ecb0 100644 --- a/services/issue/label_test.go +++ b/services/issue/label_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) @@ -23,16 +24,16 @@ func TestIssue_AddLabels(t *testing.T) { {2, []int64{}, 1}, // pull-request, empty } for _, test := range tests { - assert.NoError(t, models.PrepareTestDatabase()) - issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) + assert.NoError(t, db.PrepareTestDatabase()) + issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) labels := make([]*models.Label, len(test.labelIDs)) for i, labelID := range test.labelIDs { - labels[i] = models.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label) + labels[i] = db.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label) } - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) assert.NoError(t, AddLabels(issue, doer, labels)) for _, labelID := range test.labelIDs { - models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID}) + db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID}) } } } @@ -49,11 +50,11 @@ func TestIssue_AddLabel(t *testing.T) { {2, 1, 2}, // pull-request, already-added label } for _, test := range tests { - assert.NoError(t, models.PrepareTestDatabase()) - issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) - label := models.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) + label := db.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) assert.NoError(t, AddLabel(issue, doer, label)) - models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) + db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) } } diff --git a/services/issue/main_test.go b/services/issue/main_test.go index b056678a42..1349837949 100644 --- a/services/issue/main_test.go +++ b/services/issue/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index 0a9112f3be..cbac7912b6 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -11,6 +11,7 @@ import ( texttmpl "text/template" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -40,7 +41,7 @@ const bodyTpl = ` ` func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository, issue *models.Issue, comment *models.Comment) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) var mailService = setting.Mailer{ From: "test@gitea.com", } @@ -48,11 +49,11 @@ func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository setting.MailService = &mailService setting.Domain = "localhost" - doer = models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) - issue = models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) + doer = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) + issue = db.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) assert.NoError(t, issue.LoadRepo()) - comment = models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment) + comment = db.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment) return } @@ -139,8 +140,8 @@ func TestTemplateSelection(t *testing.T) { Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection") expect(t, msg, "issue/default/subject", "issue/default/body") - pull := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue) - comment = models.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment) + pull := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue) + comment = db.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment) msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: pull, Doer: doer, ActionType: models.ActionCommentPull, Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection") expect(t, msg, "pull/comment/subject", "pull/comment/body") diff --git a/services/mailer/main_test.go b/services/mailer/main_test.go index bbd121024e..2fbe9c54a9 100644 --- a/services/mailer/main_test.go +++ b/services/mailer/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 5bd08fa9bf..1c0bf2b595 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/lfs" @@ -203,7 +204,7 @@ func runSync(ctx context.Context, m *models.Mirror) ([]*mirrorSyncResult, bool) gitRepo.Close() log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo) - if err := m.Repo.UpdateSize(models.DefaultDBContext()); err != nil { + if err := m.Repo.UpdateSize(db.DefaultContext()); err != nil { log.Error("Failed to update size for mirror repository: %v", err) } diff --git a/services/pull/check_test.go b/services/pull/check_test.go index f6614ea0ad..8beea3d56d 100644 --- a/services/pull/check_test.go +++ b/services/pull/check_test.go @@ -11,13 +11,14 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/queue" "github.com/stretchr/testify/assert" ) func TestPullRequest_AddToTaskQueue(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) idChan := make(chan int64, 10) @@ -41,11 +42,11 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) { prQueue = q.(queue.UniqueQueue) - pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) AddToTaskQueue(pr) assert.Eventually(t, func() bool { - pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) return pr.Status == models.PullRequestStatusChecking }, 1*time.Second, 100*time.Millisecond) @@ -70,7 +71,7 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) { assert.False(t, has) assert.NoError(t, err) - pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) assert.Equal(t, models.PullRequestStatusChecking, pr.Status) for _, callback := range queueShutdown { diff --git a/services/pull/main_test.go b/services/pull/main_test.go index 6c49e8fbf2..c8d3394e8e 100644 --- a/services/pull/main_test.go +++ b/services/pull/main_test.go @@ -9,9 +9,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/pull/pull.go b/services/pull/pull.go index 23407ea67b..d78d9b1bd0 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -14,6 +14,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" @@ -58,7 +59,7 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 return err } - mentions, err := pull.FindAndUpdateIssueMentions(models.DefaultDBContext(), pull.Poster, pull.Content) + mentions, err := pull.FindAndUpdateIssueMentions(db.DefaultContext(), pull.Poster, pull.Content) if err != nil { return err } diff --git a/services/pull/review.go b/services/pull/review.go index 3aa4570620..ce34cc59df 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -12,6 +12,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" @@ -58,7 +59,7 @@ func CreateCodeComment(doer *models.User, gitRepo *git.Repository, issue *models return nil, err } - mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content) + mentions, err := issue.FindAndUpdateIssueMentions(db.DefaultContext(), doer, comment.Content) if err != nil { return nil, err } @@ -245,7 +246,7 @@ func SubmitReview(doer *models.User, gitRepo *git.Repository, issue *models.Issu return nil, nil, err } - ctx := models.DefaultDBContext() + ctx := db.DefaultContext() mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, comm.Content) if err != nil { return nil, nil, err diff --git a/services/release/release.go b/services/release/release.go index 4a55f73a3a..a9181398b5 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" @@ -122,7 +123,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs return err } - if err = models.AddReleaseAttachments(models.DefaultDBContext(), rel.ID, attachmentUUIDs); err != nil { + if err = models.AddReleaseAttachments(db.DefaultContext(), rel.ID, attachmentUUIDs); err != nil { return err } @@ -188,11 +189,11 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea } rel.LowerTagName = strings.ToLower(rel.TagName) - ctx, commiter, err := models.TxDBContext() + ctx, committer, err := db.TxContext() if err != nil { return err } - defer commiter.Close() + defer committer.Close() if err = models.UpdateRelease(ctx, rel); err != nil { return err @@ -249,7 +250,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea } } - if err = commiter.Commit(); err != nil { + if err = committer.Commit(); err != nil { return } @@ -310,7 +311,7 @@ func DeleteReleaseByID(id int64, doer *models.User, delTag bool) error { } else { rel.IsTag = true - if err = models.UpdateRelease(models.DefaultDBContext(), rel); err != nil { + if err = models.UpdateRelease(db.DefaultContext(), rel); err != nil { return fmt.Errorf("Update: %v", err) } } diff --git a/services/release/release_test.go b/services/release/release_test.go index 936f2ab71c..e53e4c935b 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/services/attachment" @@ -18,14 +19,14 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func TestRelease_Create(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -126,10 +127,10 @@ func TestRelease_Create(t *testing.T) { } func TestRelease_Update(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -268,10 +269,10 @@ func TestRelease_Update(t *testing.T) { } func TestRelease_createTag(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -351,9 +352,9 @@ func TestRelease_createTag(t *testing.T) { } func TestCreateNewTag(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.NoError(t, CreateNewTag(user, repo, "master", "v2.0", "v2.0 is released \n\n BUGFIX: .... \n\n 123")) diff --git a/services/repository/generate.go b/services/repository/generate.go index 43cbb45a41..5a9f6aa658 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -6,6 +6,7 @@ package repository import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" repo_module "code.gitea.io/gitea/modules/repository" @@ -20,7 +21,7 @@ func GenerateRepository(doer, owner *models.User, templateRepo *models.Repositor } var generateRepo *models.Repository - if err = models.WithTx(func(ctx models.DBContext) error { + if err = db.WithTx(func(ctx *db.Context) error { generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts) if err != nil { return err diff --git a/services/repository/main_test.go b/services/repository/main_test.go index f13f358635..91d0d36ca0 100644 --- a/services/repository/main_test.go +++ b/services/repository/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/repository/push.go b/services/repository/push.go index cf7060adef..f7590d5787 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" @@ -82,7 +83,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } defer gitRepo.Close() - if err = repo.UpdateSize(models.DefaultDBContext()); err != nil { + if err = repo.UpdateSize(db.DefaultContext()); err != nil { log.Error("Failed to update size for repository: %v", err) } diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go index c92844674c..40ccfdfb52 100644 --- a/services/repository/transfer_test.go +++ b/services/repository/transfer_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification/action" "code.gitea.io/gitea/modules/util" @@ -27,14 +28,14 @@ func registerNotifier() { func TestTransferOwnership(t *testing.T) { registerNotifier() - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) - repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo.Owner = db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) assert.NoError(t, TransferOwnership(doer, doer, repo, nil)) - transferredRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + transferredRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) assert.EqualValues(t, 2, transferredRepo.OwnerID) exist, err := util.IsExist(models.RepoPath("user3", "repo3")) @@ -43,7 +44,7 @@ func TestTransferOwnership(t *testing.T) { exist, err = util.IsExist(models.RepoPath("user2", "repo3")) assert.NoError(t, err) assert.True(t, exist) - models.AssertExistsAndLoadBean(t, &models.Action{ + db.AssertExistsAndLoadBean(t, &models.Action{ OpType: models.ActionTransferRepo, ActUserID: 2, RepoID: 3, @@ -54,12 +55,12 @@ func TestTransferOwnership(t *testing.T) { } func TestStartRepositoryTransferSetPermission(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) - recipient := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) - repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) + recipient := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo.Owner = db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) hasAccess, err := models.HasAccess(recipient.ID, repo) assert.NoError(t, err) diff --git a/services/webhook/main_test.go b/services/webhook/main_test.go index 6cb0cffe49..7aef4b3a51 100644 --- a/services/webhook/main_test.go +++ b/services/webhook/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 10c32a9485..095b30713c 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) @@ -25,52 +26,52 @@ func TestWebhook_GetSlackHook(t *testing.T) { } func TestPrepareWebhooks(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) hookTasks := []*models.HookTask{ {RepoID: repo.ID, HookID: 1, EventType: models.HookEventPush}, } for _, hookTask := range hookTasks { - models.AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { - models.AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } } func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*models.HookTask{ {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, } for _, hookTask := range hookTasks { - models.AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } // this test also ensures that * doesn't handle / in any special way (like shell would) assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { - models.AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } } func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*models.HookTask{ {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, } for _, hookTask := range hookTasks { - models.AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) for _, hookTask := range hookTasks { - models.AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } } diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 6c861d556a..ff4dcf334b 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -11,6 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" @@ -18,7 +19,7 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func TestWikiNameToSubURL(t *testing.T) { @@ -110,23 +111,23 @@ func TestWikiNameToFilenameToName(t *testing.T) { } func TestRepository_InitWiki(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) // repo1 already has a wiki - repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.NoError(t, InitWiki(repo1)) // repo2 does not already have a wiki - repo2 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo2 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) assert.NoError(t, InitWiki(repo2)) assert.True(t, repo2.HasWiki()) } func TestRepository_AddWikiPage(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) const wikiContent = "This is the wiki content" const commitMsg = "Commit message" - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) for _, wikiName := range []string{ "Another page", "Here's a <tag> and a/slash", @@ -166,18 +167,18 @@ func TestRepository_AddWikiPage(t *testing.T) { } func TestRepository_EditWikiPage(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) const newWikiContent = "This is the new content" const commitMsg = "Commit message" - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) for _, newWikiName := range []string{ "Home", // same name as before "New home", "New/name/with/slashes", } { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg)) // Now need to show that the page has been added: @@ -199,9 +200,9 @@ func TestRepository_EditWikiPage(t *testing.T) { } func TestRepository_DeleteWikiPage(t *testing.T) { - models.PrepareTestEnv(t) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + db.PrepareTestEnv(t) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) assert.NoError(t, DeleteWikiPage(doer, repo, "Home")) // Now need to show that the page has been added: @@ -216,8 +217,8 @@ func TestRepository_DeleteWikiPage(t *testing.T) { } func TestPrepareWikiFileName(t *testing.T) { - models.PrepareTestEnv(t) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + db.PrepareTestEnv(t) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) gitRepo, err := git.OpenRepository(repo.WikiPath()) defer gitRepo.Close() assert.NoError(t, err) @@ -267,7 +268,7 @@ func TestPrepareWikiFileName(t *testing.T) { } func TestPrepareWikiFileName_FirstPage(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) // Now create a temporaryDirectory tmpDir, err := ioutil.TempDir("", "empty-wiki") |