Skip to content

Commit f4ffe8e

Browse files
authored
Save TimeStamps for Star, Label, Follow, Watch and Collaboration to Database (#13124)
* Add timestamps to Star, Label, LanguageStat, Follow, Watch and Collaboration * Star do not need updated * LanguageStat do not need update (they wont change) * fix unit-test
1 parent f285860 commit f4ffe8e

File tree

8 files changed

+108
-25
lines changed

8 files changed

+108
-25
lines changed

models/issue_label.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
"code.gitea.io/gitea/modules/timeutil"
16+
1517
"xorm.io/builder"
1618
"xorm.io/xorm"
1719
)
@@ -21,14 +23,17 @@ var LabelColorPattern = regexp.MustCompile("^#[0-9a-fA-F]{6}$")
2123

2224
// Label represents a label of repository for issues.
2325
type Label struct {
24-
ID int64 `xorm:"pk autoincr"`
25-
RepoID int64 `xorm:"INDEX"`
26-
OrgID int64 `xorm:"INDEX"`
27-
Name string
28-
Description string
29-
Color string `xorm:"VARCHAR(7)"`
30-
NumIssues int
31-
NumClosedIssues int
26+
ID int64 `xorm:"pk autoincr"`
27+
RepoID int64 `xorm:"INDEX"`
28+
OrgID int64 `xorm:"INDEX"`
29+
Name string
30+
Description string
31+
Color string `xorm:"VARCHAR(7)"`
32+
NumIssues int
33+
NumClosedIssues int
34+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
35+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
36+
3237
NumOpenIssues int `xorm:"-"`
3338
NumOpenRepoIssues int64 `xorm:"-"`
3439
IsChecked bool `xorm:"-"`

models/issue_label_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ func TestUpdateLabel(t *testing.T) {
263263
label.Name = update.Name
264264
assert.NoError(t, UpdateLabel(update))
265265
newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
266-
assert.Equal(t, *label, *newLabel)
266+
assert.EqualValues(t, label.ID, newLabel.ID)
267+
assert.EqualValues(t, label.Color, newLabel.Color)
268+
assert.EqualValues(t, label.Name, newLabel.Name)
269+
assert.EqualValues(t, label.Description, newLabel.Description)
267270
CheckConsistencyFor(t, &Label{}, &Repository{})
268271
}
269272

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ var migrations = []Migration{
242242
NewMigration("add TrustModel field to Repository", addTrustModelToRepository),
243243
// v153 > v154
244244
NewMigration("add Team review request support", addTeamReviewRequestSupport),
245+
// v154 > v155
246+
NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps),
245247
}
246248

247249
// GetCurrentDBVersion returns the current db version

models/migrations/v154.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 addTimeStamps(x *xorm.Engine) error {
14+
// this will add timestamps where it is useful to have
15+
16+
// Star represents a starred repo by an user.
17+
type Star struct {
18+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
19+
}
20+
if err := x.Sync2(new(Star)); err != nil {
21+
return err
22+
}
23+
24+
// Label represents a label of repository for issues.
25+
type Label struct {
26+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
27+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
28+
}
29+
if err := x.Sync2(new(Label)); err != nil {
30+
return err
31+
}
32+
33+
// Follow represents relations of user and his/her followers.
34+
type Follow struct {
35+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
36+
}
37+
if err := x.Sync2(new(Follow)); err != nil {
38+
return err
39+
}
40+
41+
// Watch is connection request for receiving repository notification.
42+
type Watch struct {
43+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
44+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
45+
}
46+
if err := x.Sync2(new(Watch)); err != nil {
47+
return err
48+
}
49+
50+
// Collaboration represent the relation between an individual and a repository.
51+
type Collaboration struct {
52+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
53+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
54+
}
55+
return x.Sync2(new(Collaboration))
56+
}

models/repo_collaboration.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ package models
88
import (
99
"fmt"
1010

11+
"code.gitea.io/gitea/modules/timeutil"
12+
1113
"xorm.io/builder"
1214
)
1315

1416
// Collaboration represent the relation between an individual and a repository.
1517
type Collaboration struct {
16-
ID int64 `xorm:"pk autoincr"`
17-
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
18-
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
19-
Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
18+
ID int64 `xorm:"pk autoincr"`
19+
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
20+
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
21+
Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
22+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
23+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
2024
}
2125

2226
func (repo *Repository) addCollaborator(e Engine, u *User) error {

models/repo_watch.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/timeutil"
1112
)
1213

1314
// RepoWatchMode specifies what kind of watch the user has on a repository
@@ -26,10 +27,12 @@ const (
2627

2728
// Watch is connection request for receiving repository notification.
2829
type Watch struct {
29-
ID int64 `xorm:"pk autoincr"`
30-
UserID int64 `xorm:"UNIQUE(watch)"`
31-
RepoID int64 `xorm:"UNIQUE(watch)"`
32-
Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
30+
ID int64 `xorm:"pk autoincr"`
31+
UserID int64 `xorm:"UNIQUE(watch)"`
32+
RepoID int64 `xorm:"UNIQUE(watch)"`
33+
Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
34+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
35+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
3336
}
3437

3538
// getWatch gets what kind of subscription a user has on a given repository; returns dummy record if none found

models/star.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
package models
66

7+
import (
8+
"code.gitea.io/gitea/modules/timeutil"
9+
)
10+
711
// Star represents a starred repo by an user.
812
type Star struct {
9-
ID int64 `xorm:"pk autoincr"`
10-
UID int64 `xorm:"UNIQUE(s)"`
11-
RepoID int64 `xorm:"UNIQUE(s)"`
13+
ID int64 `xorm:"pk autoincr"`
14+
UID int64 `xorm:"UNIQUE(s)"`
15+
RepoID int64 `xorm:"UNIQUE(s)"`
16+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
1217
}
1318

1419
// StarRepo or unstar repository.
@@ -39,7 +44,7 @@ func StarRepo(userID, repoID int64, star bool) error {
3944
return nil
4045
}
4146

42-
if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil {
47+
if _, err := sess.Delete(&Star{UID: userID, RepoID: repoID}); err != nil {
4348
return err
4449
}
4550
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
@@ -59,7 +64,7 @@ func IsStaring(userID, repoID int64) bool {
5964
}
6065

6166
func isStaring(e Engine, userID, repoID int64) bool {
62-
has, _ := e.Get(&Star{0, userID, repoID})
67+
has, _ := e.Get(&Star{UID: userID, RepoID: repoID})
6368
return has
6469
}
6570

models/user_follow.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
package models
66

7+
import (
8+
"code.gitea.io/gitea/modules/timeutil"
9+
)
10+
711
// Follow represents relations of user and his/her followers.
812
type Follow struct {
9-
ID int64 `xorm:"pk autoincr"`
10-
UserID int64 `xorm:"UNIQUE(follow)"`
11-
FollowID int64 `xorm:"UNIQUE(follow)"`
13+
ID int64 `xorm:"pk autoincr"`
14+
UserID int64 `xorm:"UNIQUE(follow)"`
15+
FollowID int64 `xorm:"UNIQUE(follow)"`
16+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
1217
}
1318

1419
// IsFollowing returns true if user is following followID.

0 commit comments

Comments
 (0)