Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop column NumFollowers and NumFollowing #24676

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ var migrations = []Migration{
NewMigration("Add ArchivedUnix Column", v1_20.AddArchivedUnixToRepository),
// v256 -> v257
NewMigration("Add is_internal column to package", v1_20.AddIsInternalColumnToPackage),
// v257 -> v258
NewMigration("Drop column NumFollowers and NumFollowing on User", v1_20.DropColumnNumFollowersAndNumFollowingOnUser),
}

// GetCurrentDBVersion returns the current db version
Expand Down
22 changes: 22 additions & 0 deletions models/migrations/v1_20/v257.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_20 //nolint

import (
"code.gitea.io/gitea/models/migrations/base"

"xorm.io/xorm"
)

func DropColumnNumFollowersAndNumFollowingOnUser(x *xorm.Engine) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
if err := base.DropTableColumns(sess, "user", "num_followers", "num_following"); err != nil {
return err
}
return sess.Commit()
}
2 changes: 0 additions & 2 deletions models/unittest/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func init() {
AssertCountByCond(t, "star", builder.Eq{"uid": user.int("ID")}, user.int("NumStars"))
AssertCountByCond(t, "org_user", builder.Eq{"org_id": user.int("ID")}, user.int("NumMembers"))
AssertCountByCond(t, "team", builder.Eq{"org_id": user.int("ID")}, user.int("NumTeams"))
AssertCountByCond(t, "follow", builder.Eq{"user_id": user.int("ID")}, user.int("NumFollowing"))
AssertCountByCond(t, "follow", builder.Eq{"follow_id": user.int("ID")}, user.int("NumFollowers"))
if user.int("Type") != modelsUserTypeOrganization {
assert.EqualValues(t, 0, user.int("NumMembers"), "Unexpected number of members for user id: %d", user.int("ID"))
assert.EqualValues(t, 0, user.int("NumTeams"), "Unexpected number of teams for user id: %d", user.int("ID"))
Expand Down
16 changes: 0 additions & 16 deletions models/user/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ func FollowUser(userID, followID int64) (err error) {
if err = db.Insert(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
return err
}

if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
return err
}

if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
return err
}
return committer.Commit()
}

Expand All @@ -67,13 +59,5 @@ func UnfollowUser(userID, followID int64) (err error) {
if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
return err
}

if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
return err
}

if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
return err
}
return committer.Commit()
}
6 changes: 2 additions & 4 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ type User struct {
UseCustomAvatar bool

// Counters
NumFollowers int
NumFollowing int `xorm:"NOT NULL DEFAULT 0"`
NumStars int
NumRepos int
NumStars int
NumRepos int

// For organization
NumTeams int
Expand Down
2 changes: 0 additions & 2 deletions services/convert/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *ap
Website: user.Website,
Description: user.Description,
// counter's
Followers: user.NumFollowers,
Following: user.NumFollowing,
StarredRepos: user.NumStars,
}

Expand Down
18 changes: 0 additions & 18 deletions services/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,6 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
}
// ***** END: Star *****

// ***** START: Follow *****
followeeIDs, err := db.FindIDs(ctx, "follow", "follow.follow_id",
builder.Eq{"follow.user_id": u.ID})
if err != nil {
return fmt.Errorf("get all followees: %w", err)
} else if err = db.DecrByIDs(ctx, followeeIDs, "num_followers", new(user_model.User)); err != nil {
return fmt.Errorf("decrease user num_followers: %w", err)
}

followerIDs, err := db.FindIDs(ctx, "follow", "follow.user_id",
builder.Eq{"follow.follow_id": u.ID})
if err != nil {
return fmt.Errorf("get all followers: %w", err)
} else if err = db.DecrByIDs(ctx, followerIDs, "num_following", new(user_model.User)); err != nil {
return fmt.Errorf("decrease user num_following: %w", err)
}
// ***** END: Follow *****

if err = db.DeleteBeans(ctx,
&auth_model.AccessToken{UID: u.ID},
&repo_model.Collaboration{UserID: u.ID},
Expand Down