Skip to content

Commit f7da8f6

Browse files
committed
Remove num_watches column from table repository
1 parent 5eb0ee4 commit f7da8f6

File tree

16 files changed

+109
-120
lines changed

16 files changed

+109
-120
lines changed

models/fixtures/repository.yml

Lines changed: 0 additions & 57 deletions
Large diffs are not rendered by default.

models/migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func prepareMigrationTasks() []*migration {
367367
newMigration(307, "Fix milestone deadline_unix when there is no due date", v1_23.FixMilestoneNoDueDate),
368368
newMigration(308, "Add index(user_id, is_deleted) for action table", v1_23.AddNewIndexForUserDashboard),
369369
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
370+
newMigration(310, "Remove repository column num_watches", v1_23.RemoveRepoNumWatches),
370371
}
371372
return preparedMigrations
372373
}

models/migrations/v1_23/v310.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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/models/migrations/base"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
func RemoveRepoNumWatches(x *xorm.Engine) error {
13+
sess := x.NewSession()
14+
defer sess.Close()
15+
return base.DropTableColumns(sess, "repository", "num_watches")
16+
}

models/repo.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ func StatsCorrectSQL(ctx context.Context, sql string, id int64) error {
5959
return err
6060
}
6161

62-
func repoStatsCorrectNumWatches(ctx context.Context, id int64) error {
63-
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id)
64-
}
65-
6662
func repoStatsCorrectNumStars(ctx context.Context, id int64) error {
6763
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id)
6864
}
@@ -139,12 +135,6 @@ func CheckRepoStats(ctx context.Context) error {
139135
log.Trace("Doing: CheckRepoStats")
140136

141137
checkers := []*repoChecker{
142-
// Repository.NumWatches
143-
{
144-
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id AND mode<>2)"),
145-
repoStatsCorrectNumWatches,
146-
"repository count 'num_watches'",
147-
},
148138
// Repository.NumStars
149139
{
150140
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)"),
@@ -259,7 +249,6 @@ func UpdateRepoStats(ctx context.Context, id int64) error {
259249
var err error
260250

261251
for _, f := range []func(ctx context.Context, id int64) error{
262-
repoStatsCorrectNumWatches,
263252
repoStatsCorrectNumStars,
264253
repoStatsCorrectNumIssues,
265254
repoStatsCorrectNumPulls,

models/repo/repo.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type Repository struct {
140140
DefaultBranch string
141141
DefaultWikiBranch string
142142

143-
NumWatches int
143+
NumWatches int `xorm:"-"`
144144
NumStars int
145145
NumForks int
146146
NumIssues int
@@ -317,6 +317,18 @@ func (repo *Repository) LoadAttributes(ctx context.Context) error {
317317
return nil
318318
}
319319

320+
func (repo *Repository) LoadNumWatchers(ctx context.Context) error {
321+
if repo.NumWatches > 0 {
322+
return nil
323+
}
324+
cnt, err := CountRepoWatchers(ctx, repo.ID)
325+
if err != nil {
326+
return err
327+
}
328+
repo.NumWatches = int(cnt)
329+
return nil
330+
}
331+
320332
// FullName returns the repository full name
321333
func (repo *Repository) FullName() string {
322334
return repo.OwnerName + "/" + repo.Name

models/repo/repo_list.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,38 @@ func (repos RepositoryList) LoadLanguageStats(ctx context.Context) error {
146146
return nil
147147
}
148148

149+
func (repos RepositoryList) LoadNumWatchers(ctx context.Context) error {
150+
if len(repos) == 0 {
151+
return nil
152+
}
153+
154+
type results struct {
155+
RepoID int64
156+
NumWatchers int64
157+
}
158+
// Load watchers.
159+
watches := make([]results, 0, len(repos))
160+
if err := db.GetEngine(ctx).
161+
Select("repo_id, COUNT(*) as num_watchers").
162+
Table("watch").
163+
In("repo_id", repos.IDs()).
164+
And("`mode` <> ?", WatchModeDont).
165+
GroupBy("repo_id").
166+
Find(&watches); err != nil {
167+
return fmt.Errorf("find watchers: %w", err)
168+
}
169+
170+
watchesMap := make(map[int64]int, len(repos))
171+
for _, watch := range watches {
172+
watchesMap[watch.RepoID] = int(watch.NumWatchers)
173+
}
174+
175+
for i := range repos {
176+
repos[i].NumWatches = watchesMap[repos[i].ID]
177+
}
178+
return nil
179+
}
180+
149181
// LoadAttributes loads the attributes for the given RepositoryList
150182
func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
151183
if err := repos.LoadOwners(ctx); err != nil {

models/repo/watch.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error)
7575

7676
hadrec := watch.Mode != WatchModeNone
7777
needsrec := mode != WatchModeNone
78-
repodiff := 0
79-
80-
if IsWatchMode(mode) && !IsWatchMode(watch.Mode) {
81-
repodiff = 1
82-
} else if !IsWatchMode(mode) && IsWatchMode(watch.Mode) {
83-
repodiff = -1
84-
}
8578

8679
watch.Mode = mode
8780

@@ -98,9 +91,6 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error)
9891
} else if _, err = db.DeleteByID[Watch](ctx, watch.ID); err != nil {
9992
return err
10093
}
101-
if repodiff != 0 {
102-
_, err = db.GetEngine(ctx).Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
103-
}
10494
return err
10595
}
10696

@@ -162,6 +152,12 @@ func GetRepoWatchers(ctx context.Context, repoID int64, opts db.ListOptions) ([]
162152
return users, sess.Find(&users)
163153
}
164154

155+
func CountRepoWatchers(ctx context.Context, repoID int64) (int64, error) {
156+
return db.GetEngine(ctx).Where("repo_id=?", repoID).
157+
And("mode<>?", WatchModeDont).
158+
Count(new(Watch))
159+
}
160+
165161
// WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
166162
func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) error {
167163
if !isWrite || !setting.Service.AutoWatchOnChanges {

models/unittest/consistency.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ func init() {
8686
AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": repo.int("ForkID")})
8787
}
8888

89-
actual := GetCountByCond(t, "watch", builder.Eq{"repo_id": repo.int("ID")}.
90-
And(builder.Neq{"mode": modelsRepoWatchModeDont}))
91-
assert.EqualValues(t, repo.int("NumWatches"), actual,
92-
"Unexpected number of watches for repo id: %d", repo.int("ID"))
93-
94-
actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")})
89+
actual := GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")})
9590
assert.EqualValues(t, repo.int("NumIssues"), actual,
9691
"Unexpected number of issues for repo id: %d", repo.int("ID"))
9792

routers/api/v1/repo/subscriber.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func ListSubscribers(ctx *context.APIContext) {
5555
users[i] = convert.ToUser(ctx, subscriber, ctx.Doer)
5656
}
5757

58-
ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumWatches))
58+
total, err := repo_model.CountRepoWatchers(ctx, ctx.Repo.Repository.ID)
59+
if err != nil {
60+
ctx.Error(http.StatusInternalServerError, "CountRepoWatchers", err)
61+
return
62+
}
63+
64+
ctx.SetTotalCountHeader(total)
5965
ctx.JSON(http.StatusOK, users)
6066
}

routers/web/explore/repo.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
5050
opts.PageSize = setting.UI.SitemapPagingNum
5151
}
5252

53-
var (
54-
repos []*repo_model.Repository
55-
count int64
56-
err error
57-
orderBy db.SearchOrderBy
58-
)
53+
var orderBy db.SearchOrderBy
5954

6055
sortOrder := ctx.FormString("sort")
6156
if sortOrder == "" {
@@ -95,7 +90,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
9590
private := ctx.FormOptionalBool("private")
9691
ctx.Data["IsPrivate"] = private
9792

98-
repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
93+
repos, count, err := repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
9994
ListOptions: db.ListOptions{
10095
Page: page,
10196
PageSize: opts.PageSize,
@@ -133,6 +128,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
133128
return
134129
}
135130

131+
if err := repos.LoadNumWatchers(ctx); err != nil {
132+
ctx.ServerError("LoadNumWatchers", err)
133+
return
134+
}
135+
136136
ctx.Data["Keyword"] = keyword
137137
ctx.Data["Total"] = count
138138
ctx.Data["Repos"] = repos

0 commit comments

Comments
 (0)