aboutsummaryrefslogtreecommitdiff
path: root/internal/db/two_factors.go
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-09-06 10:11:08 +0800
committerGitHub <noreply@github.com>2020-09-06 10:11:08 +0800
commit519e59b5778571ace3f681b81a21b92a38ede890 (patch)
tree373a2021f3adcf8b2c44e6f28282733e00909d88 /internal/db/two_factors.go
parent771d3673f5dc1ba96ed9bf2ebe22c7f3066ec31d (diff)
db: migrate to GORM v2 (#6309)
Diffstat (limited to 'internal/db/two_factors.go')
-rw-r--r--internal/db/two_factors.go22
1 files changed, 10 insertions, 12 deletions
diff --git a/internal/db/two_factors.go b/internal/db/two_factors.go
index aa90c2ff..7692e5d5 100644
--- a/internal/db/two_factors.go
+++ b/internal/db/two_factors.go
@@ -10,9 +10,8 @@ import (
"strings"
"time"
- "github.com/jinzhu/gorm"
"github.com/pkg/errors"
- "github.com/t-tiger/gorm-bulk-insert"
+ "gorm.io/gorm"
log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/cryptoutil"
@@ -39,13 +38,17 @@ type TwoFactorsStore interface {
var TwoFactors TwoFactorsStore
// NOTE: This is a GORM create hook.
-func (t *TwoFactor) BeforeCreate() {
- t.CreatedUnix = gorm.NowFunc().Unix()
+func (t *TwoFactor) BeforeCreate(tx *gorm.DB) error {
+ if t.CreatedUnix == 0 {
+ t.CreatedUnix = tx.NowFunc().Unix()
+ }
+ return nil
}
// NOTE: This is a GORM query hook.
-func (t *TwoFactor) AfterFind() {
+func (t *TwoFactor) AfterFind(tx *gorm.DB) error {
t.Created = time.Unix(t.CreatedUnix, 0).Local()
+ return nil
}
var _ TwoFactorsStore = (*twoFactors)(nil)
@@ -69,18 +72,13 @@ func (db *twoFactors) Create(userID int64, key, secret string) error {
return errors.Wrap(err, "generate recovery codes")
}
- records := make([]interface{}, 0, len(recoveryCodes))
- for _, code := range recoveryCodes {
- records = append(records, code)
- }
-
return db.Transaction(func(tx *gorm.DB) error {
err := tx.Create(tf).Error
if err != nil {
return err
}
- return gormbulk.BulkInsert(tx, records, 3000)
+ return tx.Create(&recoveryCodes).Error
})
}
@@ -107,7 +105,7 @@ func (db *twoFactors) GetByUserID(userID int64) (*TwoFactor, error) {
tf := new(TwoFactor)
err := db.Where("user_id = ?", userID).First(tf).Error
if err != nil {
- if gorm.IsRecordNotFoundError(err) {
+ if err == gorm.ErrRecordNotFound {
return nil, ErrTwoFactorNotFound{args: errutil.Args{"userID": userID}}
}
return nil, err