From 5353bceaeab55bfbc7337f687434fc97193bf7d5 Mon Sep 17 00:00:00 2001 From: Joshua Bezaleel Abednego Date: Sun, 4 Oct 2020 09:57:28 +0700 Subject: [PATCH] MM-24948: Command webhook store squirrel refactor (#14659) Automatic Merge --- store/sqlstore/command_webhook_store.go | 42 +++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/store/sqlstore/command_webhook_store.go b/store/sqlstore/command_webhook_store.go index 7eee1038e8..dc2051e112 100644 --- a/store/sqlstore/command_webhook_store.go +++ b/store/sqlstore/command_webhook_store.go @@ -6,6 +6,8 @@ package sqlstore import ( "database/sql" + sq "github.com/Masterminds/squirrel" + "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/store" @@ -58,7 +60,19 @@ func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, error) { var webhook model.CommandWebhook exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME - if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil { + + query := s.getQueryBuilder(). + Select("*"). + From("CommandWebhooks"). + Where(sq.Eq{"Id": id}). + Where(sq.Gt{"CreateAt": exptime}) + + queryString, args, err := query.ToSql() + if err != nil { + return nil, errors.Wrap(err, "get_tosql") + } + + if err := s.GetReplica().SelectOne(&webhook, queryString, args...); err != nil { if err == sql.ErrNoRows { return nil, store.NewErrNotFound("CommandWebhook", id) } @@ -69,7 +83,18 @@ func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, error) { } func (s SqlCommandWebhookStore) TryUse(id string, limit int) error { - if sqlResult, err := s.GetMaster().Exec("UPDATE CommandWebhooks SET UseCount = UseCount + 1 WHERE Id = :Id AND UseCount < :UseLimit", map[string]interface{}{"Id": id, "UseLimit": limit}); err != nil { + query := s.getQueryBuilder(). + Update("CommandWebhooks"). + Set("UseCount", sq.Expr("UseCount + 1")). + Where(sq.Eq{"Id": id}). + Where(sq.Lt{"UseCount": limit}) + + queryString, args, err := query.ToSql() + if err != nil { + return errors.Wrap(err, "tryuse_tosql") + } + + if sqlResult, err := s.GetMaster().Exec(queryString, args...); err != nil { return errors.Wrapf(err, "tryuse: id=%s limit=%d", id, limit) } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { return store.NewErrInvalidInput("CommandWebhook", "id", id) @@ -81,7 +106,18 @@ func (s SqlCommandWebhookStore) TryUse(id string, limit int) error { func (s SqlCommandWebhookStore) Cleanup() { mlog.Debug("Cleaning up command webhook store.") exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME - if _, err := s.GetMaster().Exec("DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime", map[string]interface{}{"ExpTime": exptime}); err != nil { + + query := s.getQueryBuilder(). + Delete("CommandWebhooks"). + Where(sq.Lt{"CreateAt": exptime}) + + queryString, args, err := query.ToSql() + if err != nil { + mlog.Error("Failed to build query when trying to perform a cleanup in command webhook store.") + return + } + + if _, err := s.GetMaster().Exec(queryString, args...); err != nil { mlog.Error("Unable to cleanup command webhook store.") } }