Move almost all functions' parameter db.Engine to context.Context (#19748)

* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
This commit is contained in:
Lunny Xiao 2022-05-20 22:08:52 +08:00 committed by GitHub
parent d81e31ad78
commit fd7d83ace6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
232 changed files with 1463 additions and 2108 deletions

View file

@ -289,7 +289,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
return nil
}
repo.Units, err = getUnitsByRepoID(db.GetEngine(ctx), repo.ID)
repo.Units, err = getUnitsByRepoID(ctx, repo.ID)
if log.IsTrace() {
unitTypeStrings := make([]string, len(repo.Units))
for i, unit := range repo.Units {
@ -383,7 +383,7 @@ func (repo *Repository) GetOwner(ctx context.Context) (err error) {
return nil
}
repo.Owner, err = user_model.GetUserByIDEngine(db.GetEngine(ctx), repo.OwnerID)
repo.Owner, err = user_model.GetUserByIDCtx(ctx, repo.OwnerID)
return err
}
@ -454,15 +454,15 @@ func (repo *Repository) ComposeDocumentMetas() map[string]string {
// returns an error on failure (NOTE: no error is returned for
// non-fork repositories, and BaseRepo will be left untouched)
func (repo *Repository) GetBaseRepo() (err error) {
return repo.getBaseRepo(db.GetEngine(db.DefaultContext))
return repo.getBaseRepo(db.DefaultContext)
}
func (repo *Repository) getBaseRepo(e db.Engine) (err error) {
func (repo *Repository) getBaseRepo(ctx context.Context) (err error) {
if !repo.IsFork {
return nil
}
repo.BaseRepo, err = getRepositoryByID(e, repo.ForkID)
repo.BaseRepo, err = GetRepositoryByIDCtx(ctx, repo.ForkID)
return err
}
@ -481,16 +481,6 @@ func (repo *Repository) RepoPath() string {
return RepoPath(repo.OwnerName, repo.Name)
}
// GitConfigPath returns the path to a repository's git config/ directory
func GitConfigPath(repoPath string) string {
return filepath.Join(repoPath, "config")
}
// GitConfigPath returns the repository git config path
func (repo *Repository) GitConfigPath() string {
return GitConfigPath(repo.RepoPath())
}
// Link returns the repository link
func (repo *Repository) Link() string {
return setting.AppSubURL + "/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
@ -669,9 +659,10 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) {
return repo, err
}
func getRepositoryByID(e db.Engine, id int64) (*Repository, error) {
// GetRepositoryByIDCtx returns the repository by given id if exists.
func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
repo := new(Repository)
has, err := e.ID(id).Get(repo)
has, err := db.GetEngine(ctx).ID(id).Get(repo)
if err != nil {
return nil, err
} else if !has {
@ -682,12 +673,7 @@ func getRepositoryByID(e db.Engine, id int64) (*Repository, error) {
// GetRepositoryByID returns the repository by given id if exists.
func GetRepositoryByID(id int64) (*Repository, error) {
return getRepositoryByID(db.GetEngine(db.DefaultContext), id)
}
// GetRepositoryByIDCtx returns the repository by given id if exists.
func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
return getRepositoryByID(db.GetEngine(ctx), id)
return GetRepositoryByIDCtx(db.DefaultContext, id)
}
// GetRepositoriesMapByIDs returns the repositories by given id slice.
@ -696,8 +682,8 @@ func GetRepositoriesMapByIDs(ids []int64) (map[int64]*Repository, error) {
return repos, db.GetEngine(db.DefaultContext).In("id", ids).Find(&repos)
}
// IsRepositoryExistCtx returns true if the repository with given name under user has already existed.
func IsRepositoryExistCtx(ctx context.Context, u *user_model.User, repoName string) (bool, error) {
// IsRepositoryExist returns true if the repository with given name under user has already existed.
func IsRepositoryExist(ctx context.Context, u *user_model.User, repoName string) (bool, error) {
has, err := db.GetEngine(ctx).Get(&Repository{
OwnerID: u.ID,
LowerName: strings.ToLower(repoName),
@ -709,29 +695,20 @@ func IsRepositoryExistCtx(ctx context.Context, u *user_model.User, repoName stri
return has && isDir, err
}
// IsRepositoryExist returns true if the repository with given name under user has already existed.
func IsRepositoryExist(u *user_model.User, repoName string) (bool, error) {
return IsRepositoryExistCtx(db.DefaultContext, u, repoName)
}
// GetTemplateRepo populates repo.TemplateRepo for a generated repository and
// returns an error on failure (NOTE: no error is returned for
// non-generated repositories, and TemplateRepo will be left untouched)
func GetTemplateRepo(repo *Repository) (*Repository, error) {
return getTemplateRepo(db.GetEngine(db.DefaultContext), repo)
}
func getTemplateRepo(e db.Engine, repo *Repository) (*Repository, error) {
func GetTemplateRepo(ctx context.Context, repo *Repository) (*Repository, error) {
if !repo.IsGenerated() {
return nil, nil
}
return getRepositoryByID(e, repo.TemplateID)
return GetRepositoryByIDCtx(ctx, repo.TemplateID)
}
// TemplateRepo returns the repository, which is template of this repository
func (repo *Repository) TemplateRepo() *Repository {
repo, err := GetTemplateRepo(repo)
repo, err := GetTemplateRepo(db.DefaultContext, repo)
if err != nil {
log.Error("TemplateRepo: %v", err)
return nil
@ -739,26 +716,27 @@ func (repo *Repository) TemplateRepo() *Repository {
return repo
}
func countRepositories(userID int64, private bool) int64 {
sess := db.GetEngine(db.DefaultContext).Where("id > 0")
if userID > 0 {
sess.And("owner_id = ?", userID)
}
if !private {
sess.And("is_private=?", false)
}
count, err := sess.Count(new(Repository))
if err != nil {
log.Error("countRepositories: %v", err)
}
return count
type CountRepositoryOptions struct {
OwnerID int64
Private util.OptionalBool
}
// CountRepositories returns number of repositories.
// Argument private only takes effect when it is false,
// set it true to count all repositories.
func CountRepositories(private bool) int64 {
return countRepositories(-1, private)
func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64, error) {
sess := db.GetEngine(ctx).Where("id > 0")
if opts.OwnerID > 0 {
sess.And("owner_id = ?", opts.OwnerID)
}
if !opts.Private.IsNone() {
sess.And("is_private=?", opts.Private.IsTrue())
}
count, err := sess.Count(new(Repository))
if err != nil {
return 0, fmt.Errorf("countRepositories: %v", err)
}
return count, nil
}