Skip to content

Commit

Permalink
Remove db.DefaultContext in routers/ and cmd/ (go-gitea#26076)
Browse files Browse the repository at this point in the history
Now, the only remaining usages of `models.db.DefaultContext` are in
  - `modules`
  - `models`
  - `services`
  • Loading branch information
delvh authored Jul 24, 2023
1 parent 24c3bb9 commit f3d41c6
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 128 deletions.
9 changes: 5 additions & 4 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion routers/api/packages/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 46 additions & 50 deletions routers/api/packages/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package conan

import (
std_ctx "context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -602,67 +603,62 @@ 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()

pv, err := packages_model.GetVersionByNameAndVersion(ctx, apictx.Package.Owner.ID, packages_model.TypeConan, rref.Name, rref.Version)
if err != nil {
return err
}
var pd *packages_model.PackageDescriptor
versionDeleted := false

pd, err := packages_model.GetPackageDescriptor(ctx, pv)
if err != nil {
return err
}
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
}

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()
pd, err = packages_model.GetPackageDescriptor(ctx, pv)
if err != nil {
return err
}
}

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
}
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()
}
}

for _, pf := range pfs {
if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{
VersionID: pv.ID,
Properties: filter,
})
if err != nil {
return err
}
}

versionDeleted := false
has, err := packages_model.HasVersionFileReferences(ctx, pv.ID)
if err != nil {
return err
}
if !has {
versionDeleted = true
if len(pfs) == 0 {
return conan_model.ErrPackageReferenceNotExist
}

if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
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

if err := committer.Commit(); err != nil {
return packages_service.DeletePackageVersionAndReferences(ctx, pv)
}
return nil
})
if err != nil {
return err
}

Expand Down
20 changes: 10 additions & 10 deletions routers/api/packages/container/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions routers/api/packages/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 12 additions & 12 deletions routers/api/packages/container/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
Loading

0 comments on commit f3d41c6

Please sign in to comment.