Skip to content

Commit 63e5c99

Browse files
committed
duplicate the possible result and add tests
1 parent 690bcfa commit 63e5c99

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-
2+
id: 1
3+
uid: 1
4+
issue_id: 1
5+
is_read: true
6+
is_mentioned: false
7+
8+
-
9+
id: 2
10+
uid: 2
11+
issue_id: 1
12+
is_read: true
13+
is_mentioned: false
14+
15+
-
16+
id: 3
17+
uid: 2
18+
issue_id: 1 # duplicated with id 2
19+
is_read: false
20+
is_mentioned: true

models/migrations/v1_22/main_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_22 //nolint
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
base.MainTest(m)
14+
}

models/migrations/v1_22/v283.go

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ import (
88
)
99

1010
func AddCombinedIndexToIssueUser(x *xorm.Engine) error {
11+
type OldIssueUser struct {
12+
IssueID int64
13+
UID int64
14+
Cnt int64
15+
}
16+
17+
var duplicatedIssueUsers []OldIssueUser
18+
if err := x.SQL("select * from (select issue_id, uid, count(1) as cnt from issue_user group by issue_id, uid) a where a.cnt > 1").
19+
Find(&duplicatedIssueUsers); err != nil {
20+
return err
21+
}
22+
for _, issueUser := range duplicatedIssueUsers {
23+
if _, err := x.Exec("delete from issue_user where id in (SELECT id FROM issue_user WHERE issue_id = ? and uid = ? limit ?)", issueUser.IssueID, issueUser.UID, issueUser.Cnt-1); err != nil {
24+
return err
25+
}
26+
}
27+
1128
type IssueUser struct {
1229
UID int64 `xorm:"INDEX unique(uid_to_issue)"` // User ID.
1330
IssueID int64 `xorm:"INDEX unique(uid_to_issue)"`

models/migrations/v1_22/v283_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_22 //nolint
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
)
11+
12+
func Test_AddCombinedIndexToIssueUser(t *testing.T) {
13+
type IssueUser struct {
14+
UID int64 `xorm:"INDEX unique(uid_to_issue)"` // User ID.
15+
IssueID int64 `xorm:"INDEX unique(uid_to_issue)"`
16+
}
17+
18+
// Prepare and load the testing database
19+
x, deferable := base.PrepareTestEnv(t, 0, new(IssueUser))
20+
defer deferable()
21+
if x == nil || t.Failed() {
22+
return
23+
}
24+
25+
if err := AddCombinedIndexToIssueUser(x); err != nil {
26+
t.Fatal(err)
27+
}
28+
}

0 commit comments

Comments
 (0)