diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-06-05 03:18:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-04 20:18:50 +0100 |
commit | 12c742f8dc25e4148c44d1265d119c35f161bf74 (patch) | |
tree | 0c35f1de4cf7bdea1dfc8b03468f3616d0c82796 /models | |
parent | 449ea6005fb613212102126ff267f5c16f7c40b8 (diff) | |
download | gitea-12c742f8dc25e4148c44d1265d119c35f161bf74.tar.gz gitea-12c742f8dc25e4148c44d1265d119c35f161bf74.zip |
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>
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 4 | ||||
-rw-r--r-- | models/asymkey/ssh_key.go | 4 | ||||
-rw-r--r-- | models/asymkey/ssh_key_deploy.go | 2 | ||||
-rw-r--r-- | models/asymkey/ssh_key_fingerprint.go | 3 | ||||
-rw-r--r-- | models/consistency.go | 7 | ||||
-rwxr-xr-x | models/db/engine.go | 5 | ||||
-rw-r--r-- | models/issue.go | 10 | ||||
-rw-r--r-- | models/migrations/v151.go | 23 | ||||
-rw-r--r-- | models/migrations/v165.go | 10 | ||||
-rw-r--r-- | models/migrations/v179.go | 3 | ||||
-rw-r--r-- | models/migrations/v205.go | 8 | ||||
-rw-r--r-- | models/repo_collaboration.go | 5 | ||||
-rw-r--r-- | models/repo_list.go | 9 |
13 files changed, 52 insertions, 41 deletions
diff --git a/models/action.go b/models/action.go index 29e2ea47bd..87bfcbbbad 100644 --- a/models/action.go +++ b/models/action.go @@ -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) } } diff --git a/models/asymkey/ssh_key.go b/models/asymkey/ssh_key.go index 10220ea937..107a29e985 100644 --- a/models/asymkey/ssh_key.go +++ b/models/asymkey/ssh_key.go @@ -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 } diff --git a/models/asymkey/ssh_key_deploy.go b/models/asymkey/ssh_key_deploy.go index 9a97d37f93..22fcefff69 100644 --- a/models/asymkey/ssh_key_deploy.go +++ b/models/asymkey/ssh_key_deploy.go @@ -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 } diff --git a/models/asymkey/ssh_key_fingerprint.go b/models/asymkey/ssh_key_fingerprint.go index 283b3d3b64..747a7e6473 100644 --- a/models/asymkey/ssh_key_fingerprint.go +++ b/models/asymkey/ssh_key_fingerprint.go @@ -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 diff --git a/models/consistency.go b/models/consistency.go index abef7243f1..e817b69176 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -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 diff --git a/models/db/engine.go b/models/db/engine.go index 9f38af3c67..23eb59dcf5 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -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) diff --git a/models/issue.go b/models/issue.go index 1a66e5e95b..4150d66a65 100644 --- a/models/issue.go +++ b/models/issue.go @@ -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 { diff --git a/models/migrations/v151.go b/models/migrations/v151.go index ba6eee3440..50314d8162 100644 --- a/models/migrations/v151.go +++ b/models/migrations/v151.go @@ -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 { diff --git a/models/migrations/v165.go b/models/migrations/v165.go index d7df0f07a9..87e1a24f28 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -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 } diff --git a/models/migrations/v179.go b/models/migrations/v179.go index 735e6b62dd..e6dddef273 100644 --- a/models/migrations/v179.go +++ b/models/migrations/v179.go @@ -21,6 +21,7 @@ func convertAvatarURLToText(x *xorm.Engine) error { SQLType: schemas.SQLType{ Name: schemas.Text, }, - Nullable: true, + Nullable: true, + DefaultIsEmpty: true, }) } diff --git a/models/migrations/v205.go b/models/migrations/v205.go index 755cb10245..7aefa0431a 100644 --- a/models/migrations/v205.go +++ b/models/migrations/v205.go @@ -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, }) } diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index e20e96815c..8cbd836a42 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -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 } diff --git a/models/repo_list.go b/models/repo_list.go index 906b7548d4..45fb10c364 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -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) } |