[MM-39157] Thread participant removal with just one query using DB's JSON functions (#18725)

* feat: removes thread participant with just one query using DB's native JSON functions

* fix: corrects syntax post test failure
Этот коммит содержится в:
Archit Mathur
2021-10-19 23:37:17 +05:30
коммит произвёл GitHub
родитель acd08f0a48
Коммит 8ac9a3a5b6

Просмотреть файл

@@ -6,7 +6,6 @@ package sqlstore
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"regexp" "regexp"
@@ -2411,27 +2410,15 @@ func (s *SqlPostStore) cleanupThreads(postId, rootId string, permanent bool, use
updateQuery := s.getQueryBuilder().Update("Threads") updateQuery := s.getQueryBuilder().Update("Threads")
if count == 0 { if count == 0 {
var participants model.StringArray if s.DriverName() == model.DatabaseDriverPostgres {
err = s.getQueryBuilder(). updateQuery = updateQuery.Set("Participants", sq.Expr("Participants - ?", userId))
Select("Participants"). } else {
From("Threads"). // The .Where is because JSON_REMOVE returns null if the element to remove wasn't present
Where(sq.Eq{"PostId": rootId}). updateQuery = updateQuery.
RunWith(s.GetReplica()). Set("Participants", sq.Expr(
QueryRow(). `JSON_REMOVE(Participants, JSON_UNQUOTE(JSON_SEARCH(Participants, 'one', ?)))`, userId,
Scan(&participants) )).
Where(sq.Expr(`JSON_CONTAINS(Participants, ?)`, strconv.Quote(userId)))
if err != nil {
return errors.Wrap(err, "failed getting thread participants")
}
if participants.Contains(userId) {
participants = participants.Remove(userId)
var participantsJSON []byte
participantsJSON, err = json.Marshal(participants)
if err != nil {
return errors.Wrap(err, "failed marshalling thread participants")
}
updateQuery = updateQuery.Set("Participants", string(participantsJSON))
} }
} }