Skip to content

Commit 1986269

Browse files
mporratotechknowlogick
authored andcommitted
Override xorm type mapping for U2F counter (#6232)
1 parent 141c58f commit 1986269

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ var migrations = []Migration{
215215
NewMigration("add can close issues via commit in any branch", addCanCloseIssuesViaCommitInAnyBranch),
216216
// v80 -> v81
217217
NewMigration("add is locked to issues", addIsLockedToIssues),
218+
// v81 -> v82
219+
NewMigration("update U2F counter type", changeU2FCounterType),
218220
}
219221

220222
// Migrate database to current version

models/migrations/v81.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func changeU2FCounterType(x *xorm.Engine) error {
14+
var err error
15+
16+
switch x.Dialect().DriverName() {
17+
case "tidb":
18+
fallthrough
19+
case "mysql":
20+
_, err = x.Exec("ALTER TABLE `u2f_registration` MODIFY `counter` BIGINT")
21+
case "postgres":
22+
_, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` SET DATA TYPE bigint")
23+
case "mssql":
24+
_, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` BIGINT")
25+
}
26+
27+
if err != nil {
28+
return fmt.Errorf("Error changing u2f_registration counter column type: %v", err)
29+
}
30+
31+
return nil
32+
}

models/u2f.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type U2FRegistration struct {
1717
Name string
1818
UserID int64 `xorm:"INDEX"`
1919
Raw []byte
20-
Counter uint32
20+
Counter uint32 `xorm:"BIGINT"`
2121
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
2222
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
2323
}

models/u2f_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ func TestU2FRegistration_UpdateCounter(t *testing.T) {
4040
AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1})
4141
}
4242

43+
func TestU2FRegistration_UpdateLargeCounter(t *testing.T) {
44+
assert.NoError(t, PrepareTestDatabase())
45+
reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
46+
reg.Counter = 0xffffffff
47+
assert.NoError(t, reg.UpdateCounter())
48+
AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff})
49+
}
50+
4351
func TestCreateRegistration(t *testing.T) {
4452
assert.NoError(t, PrepareTestDatabase())
4553
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)

0 commit comments

Comments
 (0)