Fix order by parameter (#19849)

Upgrade builder to v0.3.11
Upgrade xorm to v1.3.1 and fixed some hidden bugs.

Replace #19821
Replace #19834
Included #19850

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2022-06-05 03:18:50 +08:00 committed by GitHub
parent 449ea6005f
commit 12c742f8dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 175 additions and 72 deletions

View file

@ -492,7 +492,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
act.ID = 0
act.UserID = act.Repo.Owner.ID
if _, err = e.InsertOne(act); err != nil {
if err = db.Insert(ctx, act); err != nil {
return fmt.Errorf("insert new actioner: %v", err)
}
}
@ -545,7 +545,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
}
}
if _, err = e.InsertOne(act); err != nil {
if err = db.Insert(ctx, act); err != nil {
return fmt.Errorf("insert new action: %v", err)
}
}

View file

@ -77,7 +77,7 @@ func (key *PublicKey) AuthorizedString() string {
func addKey(ctx context.Context, key *PublicKey) (err error) {
if len(key.Fingerprint) == 0 {
key.Fingerprint, err = calcFingerprint(key.Content)
key.Fingerprint, err = CalcFingerprint(key.Content)
if err != nil {
return err
}
@ -95,7 +95,7 @@ func addKey(ctx context.Context, key *PublicKey) (err error) {
func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*PublicKey, error) {
log.Trace(content)
fingerprint, err := calcFingerprint(content)
fingerprint, err := CalcFingerprint(content)
if err != nil {
return nil, err
}

View file

@ -116,7 +116,7 @@ func HasDeployKey(keyID, repoID int64) bool {
// AddDeployKey add new deploy key to database and authorized_keys file.
func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey, error) {
fingerprint, err := calcFingerprint(content)
fingerprint, err := CalcFingerprint(content)
if err != nil {
return nil, err
}

View file

@ -76,7 +76,8 @@ func calcFingerprintNative(publicKeyContent string) (string, error) {
return ssh.FingerprintSHA256(pk), nil
}
func calcFingerprint(publicKeyContent string) (string, error) {
// CalcFingerprint calculate public key's fingerprint
func CalcFingerprint(publicKeyContent string) (string, error) {
// Call the method based on configuration
var (
fnName, fp string

View file

@ -86,7 +86,6 @@ func DeleteOrphanedIssueLabels() error {
_, err := db.GetEngine(db.DefaultContext).
NotIn("label_id", builder.Select("id").From("label")).
Delete(IssueLabel{})
return err
}
@ -95,7 +94,8 @@ func CountOrphanedIssues() (int64, error) {
return db.GetEngine(db.DefaultContext).Table("issue").
Join("LEFT", "repository", "issue.repo_id=repository.id").
Where(builder.IsNull{"repository.id"}).
Count("id")
Select("COUNT(`issue`.`id`)").
Count()
}
// DeleteOrphanedIssues delete issues without a repo
@ -141,7 +141,8 @@ func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
return db.GetEngine(db.DefaultContext).Table("`"+subject+"`").
Join("LEFT", "`"+refobject+"`", joinCond).
Where(builder.IsNull{"`" + refobject + "`.id"}).
Count("id")
Select("COUNT(`" + subject + "`.`id`)").
Count()
}
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore

View file

@ -41,12 +41,11 @@ type Engine interface {
Delete(...interface{}) (int64, error)
Exec(...interface{}) (sql.Result, error)
Find(interface{}, ...interface{}) error
Get(interface{}) (bool, error)
Get(beans ...interface{}) (bool, error)
ID(interface{}) *xorm.Session
In(string, ...interface{}) *xorm.Session
Incr(column string, arg ...interface{}) *xorm.Session
Insert(...interface{}) (int64, error)
InsertOne(interface{}) (int64, error)
Iterate(interface{}, xorm.IterFunc) error
Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *xorm.Session
SQL(interface{}, ...interface{}) *xorm.Session
@ -59,7 +58,7 @@ type Engine interface {
Sync2(...interface{}) error
Select(string) *xorm.Session
NotIn(string, ...interface{}) *xorm.Session
OrderBy(string) *xorm.Session
OrderBy(interface{}, ...interface{}) *xorm.Session
Exist(...interface{}) (bool, error)
Distinct(...string) *xorm.Session
Query(...interface{}) ([]map[string][]byte, error)

View file

@ -1220,9 +1220,9 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
Desc("issue.created_unix").
Desc("issue.id")
case "priorityrepo":
sess.OrderBy("CASE " +
"WHEN issue.repo_id = " + strconv.FormatInt(priorityRepoID, 10) + " THEN 1 " +
"ELSE 2 END ASC").
sess.OrderBy("CASE "+
"WHEN issue.repo_id = ? THEN 1 "+
"ELSE 2 END ASC", priorityRepoID).
Desc("issue.created_unix").
Desc("issue.id")
case "project-column-sorting":
@ -2124,7 +2124,7 @@ func (issue *Issue) BlockedByDependencies(ctx context.Context) (issueDeps []*Dep
Join("INNER", "issue_dependency", "issue_dependency.dependency_id = issue.id").
Where("issue_id = ?", issue.ID).
// sort by repo id then created date, with the issues of the same repo at the beginning of the list
OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
OrderBy("CASE WHEN issue.repo_id = ? THEN 0 ELSE issue.repo_id END, issue.created_unix DESC", issue.RepoID).
Find(&issueDeps)
for _, depInfo := range issueDeps {
@ -2142,7 +2142,7 @@ func (issue *Issue) BlockingDependencies(ctx context.Context) (issueDeps []*Depe
Join("INNER", "issue_dependency", "issue_dependency.issue_id = issue.id").
Where("dependency_id = ?", issue.ID).
// sort by repo id then created date, with the issues of the same repo at the beginning of the list
OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
OrderBy("CASE WHEN issue.repo_id = ? THEN 0 ELSE issue.repo_id END, issue.created_unix DESC", issue.RepoID).
Find(&issueDeps)
for _, depInfo := range issueDeps {

View file

@ -5,6 +5,7 @@
package migrations
import (
"context"
"fmt"
"strings"
@ -86,21 +87,23 @@ func setDefaultPasswordToArgon2(x *xorm.Engine) error {
}
return x.Sync2(new(User))
}
tempTableName := "tmp_recreate__user"
column.Default = "'argon2'"
createTableSQL, _, err := x.Dialect().CreateTableSQL(context.Background(), x.DB(), table, tempTableName)
if err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
tempTableName := "tmp_recreate__user"
column.Default = "'argon2'"
createTableSQL, _ := x.Dialect().CreateTableSQL(table, tempTableName)
for _, sql := range createTableSQL {
if _, err := sess.Exec(sql); err != nil {
log.Error("Unable to create table %s. Error: %v\n", tempTableName, err, createTableSQL)
return err
}
if _, err := sess.Exec(createTableSQL); err != nil {
log.Error("Unable to create table %s. Error: %v\n", tempTableName, err, createTableSQL)
return err
}
for _, index := range table.Indexes {
if _, err := sess.Exec(x.Dialect().CreateIndexSQL(tempTableName, index)); err != nil {

View file

@ -24,8 +24,9 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 16,
Nullable: true, // To keep compatible as nullable
Length: 16,
Nullable: true, // To keep compatible as nullable
DefaultIsEmpty: true,
}); err != nil {
return err
}
@ -49,8 +50,9 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 16,
Nullable: true, // To keep compatible as nullable
Length: 16,
Nullable: true, // To keep compatible as nullable
DefaultIsEmpty: true,
}); err != nil {
return err
}

View file

@ -21,6 +21,7 @@ func convertAvatarURLToText(x *xorm.Engine) error {
SQLType: schemas.SQLType{
Name: schemas.Text,
},
Nullable: true,
Nullable: true,
DefaultIsEmpty: true,
})
}

View file

@ -23,7 +23,8 @@ func migrateUserPasswordSalt(x *xorm.Engine) error {
},
Length: 32,
// MySQL will like us again.
Nullable: true,
Nullable: true,
DefaultIsEmpty: true,
}); err != nil {
return err
}
@ -33,7 +34,8 @@ func migrateUserPasswordSalt(x *xorm.Engine) error {
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 32,
Nullable: true,
Length: 32,
Nullable: true,
DefaultIsEmpty: true,
})
}

View file

@ -25,9 +25,8 @@ func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_m
RepoID: repo.ID,
UserID: u.ID,
}
e := db.GetEngine(ctx)
has, err := e.Get(collaboration)
has, err := db.GetByBean(ctx, collaboration)
if err != nil {
return err
} else if has {
@ -35,7 +34,7 @@ func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_m
}
collaboration.Mode = perm.AccessModeWrite
if _, err = e.InsertOne(collaboration); err != nil {
if err = db.Insert(ctx, collaboration); err != nil {
return err
}

View file

@ -556,12 +556,15 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c
opts.OrderBy = db.SearchOrderByAlphabetically
}
args := make([]interface{}, 0)
if opts.PriorityOwnerID > 0 {
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy))
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = ? THEN 0 ELSE owner_id END, %s", opts.OrderBy))
args = append(args, opts.PriorityOwnerID)
} else if strings.Count(opts.Keyword, "/") == 1 {
// With "owner/repo" search times, prioritise results which match the owner field
orgName := strings.Split(opts.Keyword, "/")[0]
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_name LIKE '%s' THEN 0 ELSE 1 END, %s", orgName, opts.OrderBy))
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_name LIKE ? THEN 0 ELSE 1 END, %s", opts.OrderBy))
args = append(args, orgName)
}
sess := db.GetEngine(ctx)
@ -577,7 +580,7 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c
}
}
sess = sess.Where(cond).OrderBy(opts.OrderBy.String())
sess = sess.Where(cond).OrderBy(opts.OrderBy.String(), args...)
if opts.PageSize > 0 {
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}