From f3d41c61eb0e66aacc269508d27716425673509b Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 24 Jul 2023 05:47:27 +0200 Subject: [PATCH] Remove `db.DefaultContext` in `routers/` and `cmd/` (#26076) Now, the only remaining usages of `models.db.DefaultContext` are in - `modules` - `models` - `services` --- cmd/admin.go | 9 +- routers/api/packages/composer/composer.go | 2 +- routers/api/packages/conan/conan.go | 108 ++++++++++---------- routers/api/packages/container/blob.go | 20 ++-- routers/api/packages/container/container.go | 10 +- routers/api/packages/container/manifest.go | 24 ++--- routers/api/packages/npm/npm.go | 68 ++++++------ routers/api/v1/user/key.go | 3 +- routers/web/admin/packages.go | 2 +- routers/web/healthcheck/check.go | 7 +- routers/web/repo/view.go | 8 +- 11 files changed, 127 insertions(+), 134 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index add3d67d74..ea62075093 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -5,6 +5,7 @@ package cmd import ( + "context" "errors" "fmt" "net/url" @@ -373,7 +374,7 @@ func runRepoSyncReleases(_ *cli.Context) error { continue } - oldnum, err := getReleaseCount(repo.ID) + oldnum, err := getReleaseCount(ctx, repo.ID) if err != nil { log.Warn(" GetReleaseCountByRepoID: %v", err) } @@ -385,7 +386,7 @@ func runRepoSyncReleases(_ *cli.Context) error { continue } - count, err = getReleaseCount(repo.ID) + count, err = getReleaseCount(ctx, repo.ID) if err != nil { log.Warn(" GetReleaseCountByRepoID: %v", err) gitRepo.Close() @@ -401,9 +402,9 @@ func runRepoSyncReleases(_ *cli.Context) error { return nil } -func getReleaseCount(id int64) (int64, error) { +func getReleaseCount(ctx context.Context, id int64) (int64, error) { return repo_model.GetReleaseCountByRepoID( - db.DefaultContext, + ctx, id, repo_model.FindReleasesOptions{ IncludeTags: true, diff --git a/routers/api/packages/composer/composer.go b/routers/api/packages/composer/composer.go index bf5bda743f..75bbfdf4d3 100644 --- a/routers/api/packages/composer/composer.go +++ b/routers/api/packages/composer/composer.go @@ -114,7 +114,7 @@ func SearchPackages(ctx *context.Context) { // EnumeratePackages lists all package names // https://packagist.org/apidoc#list-packages func EnumeratePackages(ctx *context.Context) { - ps, err := packages_model.GetPackagesByType(db.DefaultContext, ctx.Package.Owner.ID, packages_model.TypeComposer) + ps, err := packages_model.GetPackagesByType(ctx, ctx.Package.Owner.ID, packages_model.TypeComposer) if err != nil { apiError(ctx, http.StatusInternalServerError, err) return diff --git a/routers/api/packages/conan/conan.go b/routers/api/packages/conan/conan.go index 6e1727fd76..d7349a84b2 100644 --- a/routers/api/packages/conan/conan.go +++ b/routers/api/packages/conan/conan.go @@ -4,6 +4,7 @@ package conan import ( + std_ctx "context" "fmt" "io" "net/http" @@ -602,69 +603,64 @@ func DeletePackageV2(ctx *context.Context) { } func deleteRecipeOrPackage(apictx *context.Context, rref *conan_module.RecipeReference, ignoreRecipeRevision bool, pref *conan_module.PackageReference, ignorePackageRevision bool) error { - ctx, committer, err := db.TxContext(db.DefaultContext) - if err != nil { - return err - } - defer committer.Close() + var pd *packages_model.PackageDescriptor + versionDeleted := false - pv, err := packages_model.GetVersionByNameAndVersion(ctx, apictx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version) - if err != nil { - return err - } - - pd, err := packages_model.GetPackageDescriptor(ctx, pv) - if err != nil { - return err - } - - filter := map[string]string{ - conan_module.PropertyRecipeUser: rref.User, - conan_module.PropertyRecipeChannel: rref.Channel, - } - if !ignoreRecipeRevision { - filter[conan_module.PropertyRecipeRevision] = rref.RevisionOrDefault() - } - if pref != nil { - filter[conan_module.PropertyPackageReference] = pref.Reference - if !ignorePackageRevision { - filter[conan_module.PropertyPackageRevision] = pref.RevisionOrDefault() + err := db.WithTx(apictx, func(ctx std_ctx.Context) error { + pv, err := packages_model.GetVersionByNameAndVersion(ctx, apictx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version) + if err != nil { + return err } - } - pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{ - VersionID: pv.ID, - Properties: filter, + pd, err = packages_model.GetPackageDescriptor(ctx, pv) + if err != nil { + return err + } + + filter := map[string]string{ + conan_module.PropertyRecipeUser: rref.User, + conan_module.PropertyRecipeChannel: rref.Channel, + } + if !ignoreRecipeRevision { + filter[conan_module.PropertyRecipeRevision] = rref.RevisionOrDefault() + } + if pref != nil { + filter[conan_module.PropertyPackageReference] = pref.Reference + if !ignorePackageRevision { + filter[conan_module.PropertyPackageRevision] = pref.RevisionOrDefault() + } + } + + pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{ + VersionID: pv.ID, + Properties: filter, + }) + if err != nil { + return err + } + if len(pfs) == 0 { + return conan_model.ErrPackageReferenceNotExist + } + + for _, pf := range pfs { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { + return err + } + } + has, err := packages_model.HasVersionFileReferences(ctx, pv.ID) + if err != nil { + return err + } + if !has { + versionDeleted = true + + return packages_service.DeletePackageVersionAndReferences(ctx, pv) + } + return nil }) if err != nil { return err } - if len(pfs) == 0 { - return conan_model.ErrPackageReferenceNotExist - } - - for _, pf := range pfs { - if err := packages_service.DeletePackageFile(ctx, pf); err != nil { - return err - } - } - - versionDeleted := false - has, err := packages_model.HasVersionFileReferences(ctx, pv.ID) - if err != nil { - return err - } - if !has { - versionDeleted = true - - if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil { - return err - } - } - - if err := committer.Commit(); err != nil { - return err - } if versionDeleted { notification.NotifyPackageDelete(apictx, apictx.Doer, pd) diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go index c8e8dd0545..f2d63297c1 100644 --- a/routers/api/packages/container/blob.go +++ b/routers/api/packages/container/blob.go @@ -26,19 +26,19 @@ var uploadVersionMutex sync.Mutex // saveAsPackageBlob creates a package blob from an upload // The uploaded blob gets stored in a special upload version to link them to the package/image -func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pci *packages_service.PackageCreationInfo) (*packages_model.PackageBlob, error) { +func saveAsPackageBlob(ctx context.Context, hsr packages_module.HashedSizeReader, pci *packages_service.PackageCreationInfo) (*packages_model.PackageBlob, error) { pb := packages_service.NewPackageBlob(hsr) exists := false contentStore := packages_module.NewContentStore() - uploadVersion, err := getOrCreateUploadVersion(&pci.PackageInfo) + uploadVersion, err := getOrCreateUploadVersion(ctx, &pci.PackageInfo) if err != nil { return nil, err } - err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { + err = db.WithTx(ctx, func(ctx context.Context) error { if err := packages_service.CheckSizeQuotaExceeded(ctx, pci.Creator, pci.Owner, packages_model.TypeContainer, hsr.Size()); err != nil { return err } @@ -79,24 +79,24 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pci *packages_servi } // mountBlob mounts the specific blob to a different package -func mountBlob(pi *packages_service.PackageInfo, pb *packages_model.PackageBlob) error { - uploadVersion, err := getOrCreateUploadVersion(pi) +func mountBlob(ctx context.Context, pi *packages_service.PackageInfo, pb *packages_model.PackageBlob) error { + uploadVersion, err := getOrCreateUploadVersion(ctx, pi) if err != nil { return err } - return db.WithTx(db.DefaultContext, func(ctx context.Context) error { + return db.WithTx(ctx, func(ctx context.Context) error { return createFileForBlob(ctx, uploadVersion, pb) }) } -func getOrCreateUploadVersion(pi *packages_service.PackageInfo) (*packages_model.PackageVersion, error) { +func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageInfo) (*packages_model.PackageVersion, error) { var uploadVersion *packages_model.PackageVersion // FIXME: Replace usage of mutex with database transaction // https://github.com/go-gitea/gitea/pull/21862 uploadVersionMutex.Lock() - err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { + err := db.WithTx(ctx, func(ctx context.Context) error { created := true p := &packages_model.Package{ OwnerID: pi.Owner.ID, @@ -172,8 +172,8 @@ func createFileForBlob(ctx context.Context, pv *packages_model.PackageVersion, p return nil } -func deleteBlob(ownerID int64, image, digest string) error { - return db.WithTx(db.DefaultContext, func(ctx context.Context) error { +func deleteBlob(ctx context.Context, ownerID int64, image, digest string) error { + return db.WithTx(ctx, func(ctx context.Context) error { pfds, err := container_model.GetContainerBlobs(ctx, &container_model.BlobSearchOptions{ OwnerID: ownerID, Image: image, diff --git a/routers/api/packages/container/container.go b/routers/api/packages/container/container.go index 8f79805cc8..7bd5cadaaf 100644 --- a/routers/api/packages/container/container.go +++ b/routers/api/packages/container/container.go @@ -210,7 +210,7 @@ func InitiateUploadBlob(ctx *context.Context) { } if accessible { - if err := mountBlob(&packages_service.PackageInfo{Owner: ctx.Package.Owner, Name: image}, blob.Blob); err != nil { + if err := mountBlob(ctx, &packages_service.PackageInfo{Owner: ctx.Package.Owner, Name: image}, blob.Blob); err != nil { apiError(ctx, http.StatusInternalServerError, err) return } @@ -239,7 +239,7 @@ func InitiateUploadBlob(ctx *context.Context) { return } - if _, err := saveAsPackageBlob( + if _, err := saveAsPackageBlob(ctx, buf, &packages_service.PackageCreationInfo{ PackageInfo: packages_service.PackageInfo{ @@ -384,7 +384,7 @@ func EndUploadBlob(ctx *context.Context) { return } - if _, err := saveAsPackageBlob( + if _, err := saveAsPackageBlob(ctx, uploader, &packages_service.PackageCreationInfo{ PackageInfo: packages_service.PackageInfo{ @@ -502,7 +502,7 @@ func DeleteBlob(ctx *context.Context) { return } - if err := deleteBlob(ctx.Package.Owner.ID, ctx.Params("image"), d); err != nil { + if err := deleteBlob(ctx, ctx.Package.Owner.ID, ctx.Params("image"), d); err != nil { apiError(ctx, http.StatusInternalServerError, err) return } @@ -543,7 +543,7 @@ func UploadManifest(ctx *context.Context) { return } - digest, err := processManifest(mci, buf) + digest, err := processManifest(ctx, mci, buf) if err != nil { var namedError *namedError if errors.As(err, &namedError) { diff --git a/routers/api/packages/container/manifest.go b/routers/api/packages/container/manifest.go index 1dbd058d6b..6678ed20bc 100644 --- a/routers/api/packages/container/manifest.go +++ b/routers/api/packages/container/manifest.go @@ -50,7 +50,7 @@ type manifestCreationInfo struct { Properties map[string]string } -func processManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { +func processManifest(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { var index oci.Index if err := json.NewDecoder(buf).Decode(&index); err != nil { return "", err @@ -72,14 +72,14 @@ func processManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffe } if isImageManifestMediaType(mci.MediaType) { - return processImageManifest(mci, buf) + return processImageManifest(ctx, mci, buf) } else if isImageIndexMediaType(mci.MediaType) { - return processImageManifestIndex(mci, buf) + return processImageManifestIndex(ctx, mci, buf) } return "", errManifestInvalid } -func processImageManifest(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { +func processImageManifest(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { manifestDigest := "" err := func() error { @@ -92,7 +92,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed return err } - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } @@ -181,7 +181,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed return err } - if err := notifyPackageCreate(mci.Creator, pv); err != nil { + if err := notifyPackageCreate(ctx, mci.Creator, pv); err != nil { return err } @@ -196,7 +196,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed return manifestDigest, nil } -func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { +func processImageManifestIndex(ctx context.Context, mci *manifestCreationInfo, buf *packages_module.HashedBuffer) (string, error) { manifestDigest := "" err := func() error { @@ -209,7 +209,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H return err } - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } @@ -285,7 +285,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H return err } - if err := notifyPackageCreate(mci.Creator, pv); err != nil { + if err := notifyPackageCreate(ctx, mci.Creator, pv); err != nil { return err } @@ -300,13 +300,13 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H return manifestDigest, nil } -func notifyPackageCreate(doer *user_model.User, pv *packages_model.PackageVersion) error { - pd, err := packages_model.GetPackageDescriptor(db.DefaultContext, pv) +func notifyPackageCreate(ctx context.Context, doer *user_model.User, pv *packages_model.PackageVersion) error { + pd, err := packages_model.GetPackageDescriptor(ctx, pv) if err != nil { return err } - notification.NotifyPackageCreate(db.DefaultContext, doer, pd) + notification.NotifyPackageCreate(ctx, doer, pd) return nil } diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go index 3616211d61..d1e271f23f 100644 --- a/routers/api/packages/npm/npm.go +++ b/routers/api/packages/npm/npm.go @@ -5,6 +5,7 @@ package npm import ( "bytes" + std_ctx "context" "errors" "fmt" "io" @@ -222,7 +223,7 @@ func UploadPackage(ctx *context.Context) { } for _, tag := range npmPackage.DistTags { - if err := setPackageTag(tag, pv, false); err != nil { + if err := setPackageTag(ctx, tag, pv, false); err != nil { if err == errInvalidTagName { apiError(ctx, http.StatusBadRequest, err) return @@ -345,7 +346,7 @@ func AddPackageTag(ctx *context.Context) { return } - if err := setPackageTag(ctx.Params("tag"), pv, false); err != nil { + if err := setPackageTag(ctx, ctx.Params("tag"), pv, false); err != nil { if err == errInvalidTagName { apiError(ctx, http.StatusBadRequest, err) return @@ -366,7 +367,7 @@ func DeletePackageTag(ctx *context.Context) { } if len(pvs) != 0 { - if err := setPackageTag(ctx.Params("tag"), pvs[0], true); err != nil { + if err := setPackageTag(ctx, ctx.Params("tag"), pvs[0], true); err != nil { if err == errInvalidTagName { apiError(ctx, http.StatusBadRequest, err) return @@ -377,7 +378,7 @@ func DeletePackageTag(ctx *context.Context) { } } -func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly bool) error { +func setPackageTag(ctx std_ctx.Context, tag string, pv *packages_model.PackageVersion, deleteOnly bool) error { if tag == "" { return errInvalidTagName } @@ -386,47 +387,42 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo return errInvalidTagName } - ctx, committer, err := db.TxContext(db.DefaultContext) - if err != nil { - return err - } - defer committer.Close() - - pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{ - PackageID: pv.PackageID, - Properties: map[string]string{ - npm_module.TagProperty: tag, - }, - IsInternal: util.OptionalBoolFalse, - }) - if err != nil { - return err - } - - if len(pvs) == 1 { - pvps, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeVersion, pvs[0].ID, npm_module.TagProperty) + return db.WithTx(ctx, func(ctx std_ctx.Context) error { + pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{ + PackageID: pv.PackageID, + Properties: map[string]string{ + npm_module.TagProperty: tag, + }, + IsInternal: util.OptionalBoolFalse, + }) if err != nil { return err } - for _, pvp := range pvps { - if pvp.Value == tag { - if err := packages_model.DeletePropertyByID(ctx, pvp.ID); err != nil { - return err + if len(pvs) == 1 { + pvps, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeVersion, pvs[0].ID, npm_module.TagProperty) + if err != nil { + return err + } + + for _, pvp := range pvps { + if pvp.Value == tag { + if err := packages_model.DeletePropertyByID(ctx, pvp.ID); err != nil { + return err + } + break } - break } } - } - if !deleteOnly { - _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, npm_module.TagProperty, tag) - if err != nil { - return err + if !deleteOnly { + _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, npm_module.TagProperty, tag) + if err != nil { + return err + } } - } - - return committer.Commit() + return nil + }) } func PackageSearch(ctx *context.Context) { diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index f90c65817d..6c04d0943a 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -8,7 +8,6 @@ import ( "net/http" asymkey_model "code.gitea.io/gitea/models/asymkey" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -31,7 +30,7 @@ func appendPrivateInformation(ctx std_ctx.Context, apiKey *api.PublicKey, key *a if defaultUser.ID == key.OwnerID { apiKey.Owner = convert.ToUser(ctx, defaultUser, defaultUser) } else { - user, err := user_model.GetUserByID(db.DefaultContext, key.OwnerID) + user, err := user_model.GetUserByID(ctx, key.OwnerID) if err != nil { return apiKey, err } diff --git a/routers/web/admin/packages.go b/routers/web/admin/packages.go index ace54fc0d8..731a1d6ac2 100644 --- a/routers/web/admin/packages.go +++ b/routers/web/admin/packages.go @@ -85,7 +85,7 @@ func Packages(ctx *context.Context) { // DeletePackageVersion deletes a package version func DeletePackageVersion(ctx *context.Context) { - pv, err := packages_model.GetVersionByID(db.DefaultContext, ctx.FormInt64("id")) + pv, err := packages_model.GetVersionByID(ctx, ctx.FormInt64("id")) if err != nil { ctx.ServerError("GetRepositoryByID", err) return diff --git a/routers/web/healthcheck/check.go b/routers/web/healthcheck/check.go index e11dd2aca2..ecb73a928f 100644 --- a/routers/web/healthcheck/check.go +++ b/routers/web/healthcheck/check.go @@ -4,6 +4,7 @@ package healthcheck import ( + "context" "net/http" "os" "time" @@ -72,7 +73,7 @@ func Check(w http.ResponseWriter, r *http.Request) { statuses := make([]status, 0) if setting.InstallLock { - statuses = append(statuses, checkDatabase(rsp.Checks)) + statuses = append(statuses, checkDatabase(r.Context(), rsp.Checks)) statuses = append(statuses, checkCache(rsp.Checks)) } for _, s := range statuses { @@ -89,9 +90,9 @@ func Check(w http.ResponseWriter, r *http.Request) { } // database checks gitea database status -func checkDatabase(checks checks) status { +func checkDatabase(ctx context.Context, checks checks) status { st := componentStatus{} - if err := db.GetEngine(db.DefaultContext).Ping(); err != nil { + if err := db.GetEngine(ctx).Ping(); err != nil { st.Status = fail st.Time = getCheckTime() log.Error("database ping failed with error: %v", err) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index a1c82f5395..221c1f4c4f 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -198,7 +198,7 @@ type fileInfo struct { st typesniffer.SniffedType } -func getFileReader(repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileInfo, error) { +func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileInfo, error) { dataRc, err := blob.DataAsync() if err != nil { return nil, nil, nil, err @@ -221,7 +221,7 @@ func getFileReader(repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileIn return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil } - meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repoID, pointer.Oid) + meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid) if err != nil && err != git_model.ErrLFSObjectNotExist { // fallback to plain file return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil } @@ -265,7 +265,7 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr ctx.Data["ReadmeExist"] = true ctx.Data["FileIsSymlink"] = readmeFile.IsLink() - buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, target.Blob()) + buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, target.Blob()) if err != nil { ctx.ServerError("getFileReader", err) return @@ -328,7 +328,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["IsViewFile"] = true ctx.Data["HideRepoInfo"] = true blob := entry.Blob() - buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, blob) + buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, blob) if err != nil { ctx.ServerError("getFileReader", err) return