diff options
author | zeripath <art27@cantab.net> | 2021-09-23 16:45:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-23 23:45:36 +0800 |
commit | 9302eba971611601c3ebf6024e22a11c63f4e151 (patch) | |
tree | a3e5583986161ef62e7affc694098279ecf2217d /models/release.go | |
parent | b22be7f594401d7bd81196750456ce52185bd391 (diff) | |
download | gitea-9302eba971611601c3ebf6024e22a11c63f4e151.tar.gz gitea-9302eba971611601c3ebf6024e22a11c63f4e151.zip |
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>
Diffstat (limited to 'models/release.go')
-rw-r--r-- | models/release.go | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/models/release.go b/models/release.go index 2a6e9352f3..d6b629cfe8 100644 --- a/models/release.go +++ b/models/release.go @@ -6,6 +6,7 @@ package models import ( + "context" "errors" "fmt" "sort" @@ -72,7 +73,7 @@ func (r *Release) loadAttributes(e db.Engine) error { // LoadAttributes load repo and publisher attributes for a release func (r *Release) LoadAttributes() error { - return r.loadAttributes(db.DefaultContext().Engine()) + return r.loadAttributes(db.GetEngine(db.DefaultContext)) } // APIURL the api url for a release. release must have attributes loaded @@ -102,31 +103,31 @@ func IsReleaseExist(repoID int64, tagName string) (bool, error) { return false, nil } - return db.DefaultContext().Engine().Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}) + return db.GetEngine(db.DefaultContext).Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}) } // InsertRelease inserts a release func InsertRelease(rel *Release) error { - _, err := db.DefaultContext().Engine().Insert(rel) + _, err := db.GetEngine(db.DefaultContext).Insert(rel) return err } // InsertReleasesContext insert releases -func InsertReleasesContext(ctx *db.Context, rels []*Release) error { - _, err := ctx.Engine().Insert(rels) +func InsertReleasesContext(ctx context.Context, rels []*Release) error { + _, err := db.GetEngine(ctx).Insert(rels) return err } // UpdateRelease updates all columns of a release -func UpdateRelease(ctx *db.Context, rel *Release) error { - _, err := ctx.Engine().ID(rel.ID).AllCols().Update(rel) +func UpdateRelease(ctx context.Context, rel *Release) error { + _, err := db.GetEngine(ctx).ID(rel.ID).AllCols().Update(rel) return err } // AddReleaseAttachments adds a release attachments -func AddReleaseAttachments(ctx *db.Context, releaseID int64, attachmentUUIDs []string) (err error) { +func AddReleaseAttachments(ctx context.Context, releaseID int64, attachmentUUIDs []string) (err error) { // Check attachments - attachments, err := getAttachmentsByUUIDs(ctx.Engine(), attachmentUUIDs) + attachments, err := getAttachmentsByUUIDs(db.GetEngine(ctx), attachmentUUIDs) if err != nil { return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err) } @@ -137,7 +138,7 @@ func AddReleaseAttachments(ctx *db.Context, releaseID int64, attachmentUUIDs []s } attachments[i].ReleaseID = releaseID // No assign value could be 0, so ignore AllCols(). - if _, err = ctx.Engine().ID(attachments[i].ID).Update(attachments[i]); err != nil { + if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Update(attachments[i]); err != nil { return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) } } @@ -155,14 +156,14 @@ func GetRelease(repoID int64, tagName string) (*Release, error) { } rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)} - _, err = db.DefaultContext().Engine().Get(rel) + _, err = db.GetEngine(db.DefaultContext).Get(rel) return rel, err } // GetReleaseByID returns release with given ID. func GetReleaseByID(id int64) (*Release, error) { rel := new(Release) - has, err := db.DefaultContext().Engine(). + has, err := db.GetEngine(db.DefaultContext). ID(id). Get(rel) if err != nil { @@ -208,7 +209,7 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { // GetReleasesByRepoID returns a list of releases of repository. func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions) ([]*Release, error) { - sess := db.DefaultContext().Engine(). + sess := db.GetEngine(db.DefaultContext). Desc("created_unix", "id"). Where(opts.toConds(repoID)) @@ -222,7 +223,7 @@ func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions) ([]*Release, er // CountReleasesByRepoID returns a number of releases matching FindReleaseOptions and RepoID. func CountReleasesByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) { - return db.DefaultContext().Engine().Where(opts.toConds(repoID)).Count(new(Release)) + return db.GetEngine(db.DefaultContext).Where(opts.toConds(repoID)).Count(new(Release)) } // GetLatestReleaseByRepoID returns the latest release for a repository @@ -234,7 +235,7 @@ func GetLatestReleaseByRepoID(repoID int64) (*Release, error) { And(builder.Eq{"is_tag": false}) rel := new(Release) - has, err := db.DefaultContext().Engine(). + has, err := db.GetEngine(db.DefaultContext). Desc("created_unix", "id"). Where(cond). Get(rel) @@ -248,8 +249,8 @@ func GetLatestReleaseByRepoID(repoID int64) (*Release, error) { } // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames. -func GetReleasesByRepoIDAndNames(ctx *db.Context, repoID int64, tagNames []string) (rels []*Release, err error) { - err = ctx.Engine(). +func GetReleasesByRepoIDAndNames(ctx context.Context, repoID int64, tagNames []string) (rels []*Release, err error) { + err = db.GetEngine(ctx). In("tag_name", tagNames). Desc("created_unix"). Find(&rels, Release{RepoID: repoID}) @@ -258,7 +259,7 @@ func GetReleasesByRepoIDAndNames(ctx *db.Context, repoID int64, tagNames []strin // GetReleaseCountByRepoID returns the count of releases of repository func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) { - return db.DefaultContext().Engine().Where(opts.toConds(repoID)).Count(&Release{}) + return db.GetEngine(db.DefaultContext).Where(opts.toConds(repoID)).Count(&Release{}) } type releaseMetaSearch struct { @@ -281,7 +282,7 @@ func (s releaseMetaSearch) Less(i, j int) bool { // GetReleaseAttachments retrieves the attachments for releases func GetReleaseAttachments(rels ...*Release) (err error) { - return getReleaseAttachments(db.DefaultContext().Engine(), rels...) + return getReleaseAttachments(db.GetEngine(db.DefaultContext), rels...) } func getReleaseAttachments(e db.Engine, rels ...*Release) (err error) { @@ -352,13 +353,13 @@ func SortReleases(rels []*Release) { // DeleteReleaseByID deletes a release from database by given ID. func DeleteReleaseByID(id int64) error { - _, err := db.DefaultContext().Engine().ID(id).Delete(new(Release)) + _, err := db.GetEngine(db.DefaultContext).ID(id).Delete(new(Release)) return err } // UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID func UpdateReleasesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error { - _, err := db.DefaultContext().Engine().Table("release"). + _, err := db.GetEngine(db.DefaultContext).Table("release"). Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType). And("original_author_id = ?", originalAuthorID). Update(map[string]interface{}{ |