Skip to content

Commit 3fd060e

Browse files
authored
Include OriginalAuthor in Reaction constraint (#13505)
When migrating repositories with reactions with deleted users, the original author id may be -1. This means that it is possible to end up attempting to create multiple reactions with the same [ Type, IssueID, CommentID, UserID, OriginalAuthorID ] thus breaking the constraints. On SQLite this appears to cause a deadlock but on other dbs this will cause the migration to fail. This PR extends the constraint to include the original author username in the constraint. Fix #13271 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 3400928 commit 3fd060e

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

models/issue_reaction.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717

1818
// Reaction represents a reactions on issues and comments.
1919
type Reaction struct {
20-
ID int64 `xorm:"pk autoincr"`
21-
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
22-
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
23-
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
24-
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
25-
OriginalAuthorID int64 `xorm:"INDEX UNIQUE(s) NOT NULL DEFAULT(0)"`
26-
OriginalAuthor string
20+
ID int64 `xorm:"pk autoincr"`
21+
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
22+
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
23+
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
24+
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
25+
OriginalAuthorID int64 `xorm:"INDEX UNIQUE(s) NOT NULL DEFAULT(0)"`
26+
OriginalAuthor string `xorm:"INDEX UNIQUE(s)"`
2727
User *User `xorm:"-"`
2828
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
2929
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ var migrations = []Migration{
252252
NewMigration("ensure repo topics are up-to-date", fixRepoTopics),
253253
// v158 -> v159
254254
NewMigration("code comment replies should have the commitID of the review they are replying to", updateCodeCommentReplies),
255+
// v159 -> v160
256+
NewMigration("update reactions constraint", updateReactionConstraint),
255257
}
256258

257259
// GetCurrentDBVersion returns the current db version

models/migrations/v159.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 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+
"code.gitea.io/gitea/modules/timeutil"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func updateReactionConstraint(x *xorm.Engine) error {
14+
// Reaction represents a reactions on issues and comments.
15+
type Reaction struct {
16+
ID int64 `xorm:"pk autoincr"`
17+
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
18+
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
19+
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
20+
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
21+
OriginalAuthorID int64 `xorm:"INDEX UNIQUE(s) NOT NULL DEFAULT(0)"`
22+
OriginalAuthor string `xorm:"INDEX UNIQUE(s)"`
23+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
24+
}
25+
26+
sess := x.NewSession()
27+
defer sess.Close()
28+
29+
if err := sess.Begin(); err != nil {
30+
return err
31+
}
32+
33+
if err := recreateTable(sess, &Reaction{}); err != nil {
34+
return err
35+
}
36+
37+
return sess.Commit()
38+
}

0 commit comments

Comments
 (0)