diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-14 10:03:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-14 10:03:30 +0800 |
commit | 844d2f49ce25b1f1fd29832a01b8b38810a8249a (patch) | |
tree | 82459e475ea08556198aa4a53ca6503ca6eda5de /internal/db | |
parent | cb439a126aa6a2728e423bcfd0d5e948337b8ddb (diff) |
db: use GORM bulk insert helper (#6100)
* Bulk insert permissions
* Bulk insert recovery codes
* Remove unnecessary import rename
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/perms.go | 18 | ||||
-rw-r--r-- | internal/db/two_factors.go | 10 |
2 files changed, 13 insertions, 15 deletions
diff --git a/internal/db/perms.go b/internal/db/perms.go index b0db295e..129f2187 100644 --- a/internal/db/perms.go +++ b/internal/db/perms.go @@ -5,9 +5,8 @@ package db import ( - "strings" - "github.com/jinzhu/gorm" + "github.com/t-tiger/gorm-bulk-insert" log "unknwon.dev/clog/v2" ) @@ -54,7 +53,7 @@ func (db *perms) AccessMode(userID int64, repo *Repository) (mode AccessMode) { access := new(Access) err := db.Where("user_id = ? AND repo_id = ?", userID, repo.ID).First(access).Error if err != nil { - if !gorm.IsRecordNotFoundError(err){ + if !gorm.IsRecordNotFoundError(err) { log.Error("Failed to get access [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err) } return mode @@ -67,11 +66,13 @@ func (db *perms) Authorize(userID int64, repo *Repository, desired AccessMode) b } func (db *perms) SetRepoPerms(repoID int64, accessMap map[int64]AccessMode) error { - vals := make([]string, 0, len(accessMap)) - items := make([]interface{}, 0, len(accessMap)*3) + records := make([]interface{}, 0, len(accessMap)) for userID, mode := range accessMap { - vals = append(vals, "(?, ?, ?)") - items = append(items, userID, repoID, mode) + records = append(records, &Access{ + UserID: userID, + RepoID: repoID, + Mode: mode, + }) } return db.Transaction(func(tx *gorm.DB) error { @@ -80,7 +81,6 @@ func (db *perms) SetRepoPerms(repoID int64, accessMap map[int64]AccessMode) erro return err } - sql := "INSERT INTO access (user_id, repo_id, mode) VALUES " + strings.Join(vals, ", ") - return tx.Exec(sql, items...).Error + return gormbulk.BulkInsert(tx, records, 3000) }) } diff --git a/internal/db/two_factors.go b/internal/db/two_factors.go index 671beb22..aa90c2ff 100644 --- a/internal/db/two_factors.go +++ b/internal/db/two_factors.go @@ -12,6 +12,7 @@ import ( "github.com/jinzhu/gorm" "github.com/pkg/errors" + "github.com/t-tiger/gorm-bulk-insert" log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/cryptoutil" @@ -68,11 +69,9 @@ func (db *twoFactors) Create(userID int64, key, secret string) error { return errors.Wrap(err, "generate recovery codes") } - vals := make([]string, 0, len(recoveryCodes)) - items := make([]interface{}, 0, len(recoveryCodes)*2) + records := make([]interface{}, 0, len(recoveryCodes)) for _, code := range recoveryCodes { - vals = append(vals, "(?, ?)") - items = append(items, code.UserID, code.Code) + records = append(records, code) } return db.Transaction(func(tx *gorm.DB) error { @@ -81,8 +80,7 @@ func (db *twoFactors) Create(userID int64, key, secret string) error { return err } - sql := "INSERT INTO two_factor_recovery_code (user_id, code) VALUES " + strings.Join(vals, ", ") - return tx.Exec(sql, items...).Error + return gormbulk.BulkInsert(tx, records, 3000) }) } |