From fa38202d980c945f543a9674b7ce2253da14467d Mon Sep 17 00:00:00 2001 From: Yair Facio Date: Fri, 10 Dec 2021 03:18:46 -0600 Subject: [PATCH] [MM-39636] Migrate from gorp to sqlx in store/sqlstore/product_notices_store.go (#19012) * Migrate from gorp to sqlx in ProductNoticesStore * Address PR comments Co-authored-by: Mattermod --- store/sqlstore/product_notices_store.go | 44 ++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/store/sqlstore/product_notices_store.go b/store/sqlstore/product_notices_store.go index 0e04ab51f1..d3aaf6cda5 100644 --- a/store/sqlstore/product_notices_store.go +++ b/store/sqlstore/product_notices_store.go @@ -35,9 +35,13 @@ func (s SqlProductNoticesStore) createIndexesIfNotExists() { } func (s SqlProductNoticesStore) Clear(notices []string) error { - sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.Eq{"NoticeId": notices}).ToSql() - if _, err := s.GetMaster().Exec(sql, args...); err != nil { - return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState") + sql, args, err := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.Eq{"NoticeId": notices}).ToSql() + if err != nil { + return errors.Wrap(err, "product_notice_view_state_tosql") + } + + if _, err := s.GetMasterX().Exec(sql, args...); err != nil { + return errors.Wrap(err, "failed to delete records from ProductNoticeViewState") } return nil } @@ -47,27 +51,31 @@ func (s SqlProductNoticesStore) ClearOldNotices(currentNotices model.ProductNoti for _, currentNotice := range currentNotices { notices = append(notices, currentNotice.ID) } - sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.NotEq{"NoticeId": notices}).ToSql() - if _, err := s.GetMaster().Exec(sql, args...); err != nil { + sql, args, err := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.NotEq{"NoticeId": notices}).ToSql() + if err != nil { + return errors.Wrap(err, "product_notice_view_state_tosql") + } + + if _, err := s.GetMasterX().Exec(sql, args...); err != nil { return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState") } return nil } func (s SqlProductNoticesStore) View(userId string, notices []string) error { - transaction, err := s.GetMaster().Begin() + transaction, err := s.GetMasterX().Beginx() if err != nil { return errors.Wrap(err, "begin_transaction") } - defer finalizeTransaction(transaction) + defer finalizeTransactionX(transaction) - var noticeStates []model.ProductNoticeViewState + noticeStates := []model.ProductNoticeViewState{} sql, args, _ := s.getQueryBuilder(). Select("*"). From("ProductNoticeViewState"). Where(sq.And{sq.Eq{"UserId": userId}, sq.Eq{"NoticeId": notices}}). ToSql() - if _, err := transaction.Select(¬iceStates, sql, args...); err != nil { + if err := transaction.Select(¬iceStates, sql, args...); err != nil { return errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId) } @@ -77,7 +85,8 @@ func (s SqlProductNoticesStore) View(userId string, notices []string) error { for i := range noticeStates { noticeStates[i].Viewed += 1 noticeStates[i].Timestamp = now - if _, err := transaction.Update(¬iceStates[i]); err != nil { + if _, err := transaction.NamedExec(`UPDATE ProductNoticeViewState + SET Viewed=:Viewed, Timestamp=:Timestamp WHERE UserId=:UserId AND NoticeId=:NoticeId`, ¬iceStates[i]); err != nil { return errors.Wrapf(err, "failed to update ProductNoticeViewState") } } @@ -94,12 +103,14 @@ func (s SqlProductNoticesStore) View(userId string, notices []string) error { for _, noticeId := range notices { if !haveNoticeState(noticeId) { - if err := transaction.Insert(&model.ProductNoticeViewState{ + productNoticeViewState := &model.ProductNoticeViewState{ UserId: userId, NoticeId: noticeId, Viewed: 1, Timestamp: now, - }); err != nil { + } + if _, err := transaction.NamedExec(`INSERT INTO ProductNoticeViewState (UserId, NoticeId, Viewed, Timestamp) + VALUES (:UserId, :NoticeId, :Viewed, :Timestamp)`, productNoticeViewState); err != nil { return errors.Wrapf(err, "failed to insert ProductNoticeViewState") } } @@ -113,9 +124,12 @@ func (s SqlProductNoticesStore) View(userId string, notices []string) error { } func (s SqlProductNoticesStore) GetViews(userId string) ([]model.ProductNoticeViewState, error) { - var noticeStates []model.ProductNoticeViewState - sql, args, _ := s.getQueryBuilder().Select("*").From("ProductNoticeViewState").Where(sq.Eq{"UserId": userId}).ToSql() - if _, err := s.GetReplica().Select(¬iceStates, sql, args...); err != nil { + noticeStates := []model.ProductNoticeViewState{} + sql, args, err := s.getQueryBuilder().Select("*").From("ProductNoticeViewState").Where(sq.Eq{"UserId": userId}).ToSql() + if err != nil { + return nil, errors.Wrap(err, "product_notice_view_state_tosql") + } + if err := s.GetReplicaX().Select(¬iceStates, sql, args...); err != nil { return nil, errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId) } return noticeStates, nil