Skip to content

Commit 542d0f6

Browse files
committed
Add new table and migrations
1 parent e1f0598 commit 542d0f6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

models/activities/user_feed.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package activities
5+
6+
import "code.gitea.io/gitea/modules/timeutil"
7+
8+
type UserFeed struct {
9+
ID int64 `xorm:"pk autoincr"`
10+
UserID int64 `xorm:"UNIQUE(s)"` // Receiver user id.
11+
ActionID int64 `xorm:"UNIQUE(s)"` // refer to action table
12+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
13+
}

models/migrations/v1_23/v305.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/timeutil"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
// SplitActionTableAsTwoTables splits the action table as two tables.
13+
func SplitActionTableAsTwoTables(x *xorm.Engine) error {
14+
// 1 create new table
15+
type UserFeed struct {
16+
ID int64 `xorm:"pk autoincr"`
17+
UserID int64 `xorm:"UNIQUE(s)"` // Receiver user id.
18+
ActionID int64 `xorm:"UNIQUE(s)"` // refer to action table
19+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
20+
}
21+
22+
if err := x.Sync(new(UserFeed)); err != nil {
23+
return err
24+
}
25+
26+
// 2 copy data from action table to new table
27+
if _, err := x.Exec("INSERT INTO `user_feed` (`user_id`, `action_id`, `created_unix`) SELECT `user_id`, `id`, `created_unix` FROM `action`"); err != nil {
28+
return err
29+
}
30+
31+
// 3 merge records from action table
32+
type result struct {
33+
IssueID int64
34+
ProjectID int64
35+
Cnt int
36+
}
37+
var results []result
38+
if err := x.Select("op_type, act_user_id, repo_id, count(*) as cnt").
39+
Table("project_issue").
40+
GroupBy("issue_id, project_id").
41+
Having("count(*) > 1").
42+
Find(&results); err != nil {
43+
return err
44+
}
45+
46+
// 4 update user_feed table to update action_id because of the merge records
47+
48+
// 5 drop column from action table
49+
}

0 commit comments

Comments
 (0)