From 95397f70354be5e71e284a578d523b91a89e81a4 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 27 Sep 2023 13:09:03 +0800 Subject: [PATCH 1/3] sqlx: fixed unfollow user incorrect and get friend error --- internal/dao/sakila/auto/cc/cc.go | 6 +++--- internal/dao/sakila/yesql/yesql_cc.sql | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/dao/sakila/auto/cc/cc.go b/internal/dao/sakila/auto/cc/cc.go index 56357923..2cedb6cf 100644 --- a/internal/dao/sakila/auto/cc/cc.go +++ b/internal/dao/sakila/auto/cc/cc.go @@ -27,7 +27,7 @@ const ( _Comment_GetCommentThumbs = `SELECT user_id, tweet_id, comment_id, reply_id, comment_type, is_thumbs_up, is_thumbs_down FROM @tweet_comment_thumbs WHERE user_id=? AND tweet_id=?` _Comment_GetCommmentRepliesByIds = `SELECT * FROM @comment_reply WHERE comment_id IN (?) ORDER BY id ASC` _Comment_GetDefaultComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY is_essence DESC, id ASC LIMIT ? OFFSET ?` - _Comment_GetHotsComments = `SELECT c.* FROM @comment c LEFT JOIN @comment_metric m ON c.id=m.comment_id WHERE c.post_id=? AND c.is_del=0 AND m.is_del=0 ORDER BY is_essence DESC, m.rank_score DESC, id DESC LIMIT ? OFFSET ?` + _Comment_GetHotsComments = `SELECT c.* FROM @comment c LEFT JOIN @comment_metric m ON c.id=m.comment_id WHERE c.post_id=? AND c.is_del=0 AND m.is_del=0 ORDER BY is_essence DESC, m.rank_score DESC, c.id DESC LIMIT ? OFFSET ?` _Comment_GetNewestComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY is_essence DESC, id DESC LIMIT ? OFFSET ?` _Comment_GetUsersByIds = `SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN (?)` _CommentManage_CreateComment = `INSERT INTO @comment (post_id, user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?)` @@ -65,7 +65,7 @@ const ( _FollowingManager_CountFollowings = `SELECT count(*) FROM @following WHERE follow_id=? AND is_del=0` _FollowingManager_CountFollows = `SELECT count(*) FROM @following WHERE user_id=? AND is_del=0` _FollowingManager_CreateFollowing = `INSERT INTO @following (user_id, follow_id, created_on) VALUES (?, ?, ?)` - _FollowingManager_DeleteFollowing = `UPDATE @following SET is_del=0, deleted_on=? WHERE user_id=? AND follow_id=? AND is_del=0` + _FollowingManager_DeleteFollowing = `UPDATE @following SET is_del=1, deleted_on=? WHERE user_id=? AND follow_id=? AND is_del=0` _FollowingManager_ExistFollowing = `SELECT 1 FROM @following WHERE user_id=? AND follow_id=? AND is_del=0` _FollowingManager_ListFollowings = `SELECT u.id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.created_on created_on FROM @following f JOIN @user u ON f.user_id=u.id WHERE f.follow_id=? AND f.is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?` _FollowingManager_ListFollows = `SELECT u.id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.created_on created_on FROM @following f JOIN @user u ON f.follow_id=u.id WHERE f.user_id=? AND f.is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?` @@ -190,7 +190,7 @@ const ( _UserMetrics_GetTweetsCount = `SELECT tweets_count FROM @user_metric WHERE user_id=? AND is_del=0` _UserMetrics_UpdateUserMetric = `UPDATE @user_metric SET tweets_count=?, modified_on=? WHERE user_id=? AND is_del=0` _UserRelation_MyFollowIds = `SELECT follow_id FROM @following WHERE user_id=? AND is_del=0` - _UserRelation_MyFriendIds = `SELECT friend_id FROM @contact WHERE user_id=? AND is_del=0` + _UserRelation_MyFriendIds = `SELECT friend_id FROM @contact WHERE user_id=? AND status=2 AND is_del=0` _Wallet_AddUserBalance = `UPDATE @user SET balance=balance+?, modified_on=? WHERE id=? AND is_del=0` _Wallet_CreateRecharge = `INSERT INTO @wallet_recharge (user_id, amount, created_on) VALUES (?, ?, ?)` _Wallet_CreateWalletStatement = `INSERT INTO @wallet_statement (user_id, change_amount, balance_snapshot, reason, created_on) VALUES (?, ?, ?, ?, ?)` diff --git a/internal/dao/sakila/yesql/yesql_cc.sql b/internal/dao/sakila/yesql/yesql_cc.sql index b2f59a6a..029cd3b2 100644 --- a/internal/dao/sakila/yesql/yesql_cc.sql +++ b/internal/dao/sakila/yesql/yesql_cc.sql @@ -38,7 +38,7 @@ FROM @comment c LEFT JOIN @comment_metric m ON c.id=m.comment_id WHERE c.post_id=? AND c.is_del=0 AND m.is_del=0 -ORDER BY is_essence DESC, m.rank_score DESC, id DESC +ORDER BY is_essence DESC, m.rank_score DESC, c.id DESC LIMIT ? OFFSET ?; -- name: get_default_comments@comment @@ -190,7 +190,7 @@ SELECT 1 FROM @following WHERE user_id=? AND follow_id=? AND is_del=0; -- name: delete_following@following_manager -- prepare: stmt UPDATE @following -SET is_del=0, deleted_on=? +SET is_del=1, deleted_on=? WHERE user_id=? AND follow_id=? AND is_del=0; -- name: list_follows@following_manager @@ -1440,7 +1440,7 @@ WHERE id=? AND is_del=0; -- name: my_friend_ids@user_relation -- prepare: stmt -SELECT friend_id FROM @contact WHERE user_id=? AND is_del=0; +SELECT friend_id FROM @contact WHERE user_id=? AND status=2 AND is_del=0; -- name: my_follow_ids@user_relation -- prepare: stmt From 5deae4fb892a994c8f6c2ecfb3438e88298d7cd8 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 27 Sep 2023 13:15:59 +0800 Subject: [PATCH 2/3] optimize get index trends data logic --- internal/dao/jinzhu/trends.go | 2 +- internal/dao/jinzhu/user.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/dao/jinzhu/trends.go b/internal/dao/jinzhu/trends.go index 8e1f1237..44e56c67 100644 --- a/internal/dao/jinzhu/trends.go +++ b/internal/dao/jinzhu/trends.go @@ -18,7 +18,7 @@ type trendsSrvA struct { func (s *trendsSrvA) GetIndexTrends(userId int64, limit int, offset int) (res []*cs.TrendsItem, total int64, err error) { db := s.db.Table(_user_). - Joins(fmt.Sprintf("JOIN %s c ON c.friend_id=%s.id AND c.user_id=? AND c.is_del=0", _contact_, _user_), userId). + Joins(fmt.Sprintf("JOIN %s c ON c.friend_id=%s.id AND c.user_id=? AND c.status=2 AND c.is_del=0", _contact_, _user_), userId). Joins(fmt.Sprintf("JOIN %s m ON c.friend_id=m.user_id AND m.tweets_count>0 AND m.is_del=0", _userMetric_)). Where(fmt.Sprintf("%s.is_del=0", _user_)) if err = db.Count(&total).Error; err != nil || total == 0 { diff --git a/internal/dao/jinzhu/user.go b/internal/dao/jinzhu/user.go index 9ffb3fe5..3c8bb251 100644 --- a/internal/dao/jinzhu/user.go +++ b/internal/dao/jinzhu/user.go @@ -28,7 +28,7 @@ type userRelationSrv struct { func newUserManageService(db *gorm.DB, ums core.UserMetricServantA) core.UserManageService { return &userManageSrv{ - db: db, + db: db, ums: ums, } } @@ -101,12 +101,12 @@ func (s *userManageSrv) GetRegisterUserCount() (res int64, err error) { } func (s *userRelationSrv) MyFriendIds(userId int64) (res []int64, err error) { - err = s.db.Table(_contact_).Where("user_id=?", userId).Select("friend_id").Find(&res).Error + err = s.db.Table(_contact_).Where("user_id=? AND status=2 AND is_del=0", userId).Select("friend_id").Find(&res).Error return } func (s *userRelationSrv) MyFollowIds(userId int64) (res []int64, err error) { - err = s.db.Table(_following_).Where("user_id=?", userId).Select("follow_id").Find(&res).Error + err = s.db.Table(_following_).Where("user_id=? AND is_del=0", userId).Select("follow_id").Find(&res).Error return } From 1a878e0f8e7a5866452bb40c00b40cab3d6a4056 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 27 Sep 2023 13:19:00 +0800 Subject: [PATCH 3/3] sqlx: optimize get index trends data logic --- internal/dao/sakila/auto/cc/cc.go | 2 +- internal/dao/sakila/yesql/yesql_cc.sql | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/dao/sakila/auto/cc/cc.go b/internal/dao/sakila/auto/cc/cc.go index 2cedb6cf..b5be15bc 100644 --- a/internal/dao/sakila/auto/cc/cc.go +++ b/internal/dao/sakila/auto/cc/cc.go @@ -96,7 +96,7 @@ const ( _SimpleIndex_Index = `SELECT * FROM @post WHERE visibility=90 ORDER BY is_top DESC, latest_replied_on DESC LIMIT ? OFFSET ?` _SimpleIndex_IndexCount = `SELECT count(*) FROM @post WHERE visibility=90` _TrendsManager_CountIndexTrends = `SELECT count(*) FROM @contact c JOIN @user u ON c.friend_id=u.id JOIN @user_metric m ON c.friend_id=m.user_id WHERE c.user_id=? AND c.is_del=0 AND u.is_del=0 AND m.is_del=0 AND m.tweets_count>0` - _TrendsManager_GetIndexTrends = `SELECT u.username username, u.nickname nickname, u.avatar avatar FROM @contact c JOIN @user u ON c.friend_id=u.id JOIN @user_metric m ON c.friend_id=m.user_id WHERE c.user_id=? AND c.is_del=0 AND u.is_del=0 AND m.is_del=0 AND m.tweets_count>0 LIMIT ? OFFSET ?` + _TrendsManager_GetIndexTrends = `SELECT u.username username, u.nickname nickname, u.avatar avatar FROM @contact c JOIN @user u ON c.friend_id=u.id JOIN @user_metric m ON c.friend_id=m.user_id WHERE c.user_id=? AND c.status=2 AND c.is_del=0 AND u.is_del=0 AND m.is_del=0 AND m.tweets_count>0 LIMIT ? OFFSET ?` _Tweet_CountFollowingTweets = `SELECT count(*) FROM @post WHERE user_id=? AND is_del=0` _Tweet_CountFollowingTweetsFollow = `SELECT count(*) FROM @post WHERE (user_id=? OR (visibility>=60 AND user_id IN(?))) AND is_del=0` _Tweet_CountFollowingTweetsFriend = `SELECT count(*) FROM @post WHERE (user_id=? OR (visibility>=50 AND user_id IN(?))) AND is_del=0` diff --git a/internal/dao/sakila/yesql/yesql_cc.sql b/internal/dao/sakila/yesql/yesql_cc.sql index 029cd3b2..73d6f490 100644 --- a/internal/dao/sakila/yesql/yesql_cc.sql +++ b/internal/dao/sakila/yesql/yesql_cc.sql @@ -240,6 +240,7 @@ ON c.friend_id=u.id JOIN @user_metric m ON c.friend_id=m.user_id WHERE c.user_id=? + AND c.status=2 AND c.is_del=0 AND u.is_del=0 AND m.is_del=0