[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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
acd08f0a48
Коммит
8ac9a3a5b6
@@ -6,7 +6,6 @@ package sqlstore
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -2411,27 +2410,15 @@ func (s *SqlPostStore) cleanupThreads(postId, rootId string, permanent bool, use
|
||||
updateQuery := s.getQueryBuilder().Update("Threads")
|
||||
|
||||
if count == 0 {
|
||||
var participants model.StringArray
|
||||
err = s.getQueryBuilder().
|
||||
Select("Participants").
|
||||
From("Threads").
|
||||
Where(sq.Eq{"PostId": rootId}).
|
||||
RunWith(s.GetReplica()).
|
||||
QueryRow().
|
||||
Scan(&participants)
|
||||
|
||||
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))
|
||||
if s.DriverName() == model.DatabaseDriverPostgres {
|
||||
updateQuery = updateQuery.Set("Participants", sq.Expr("Participants - ?", userId))
|
||||
} else {
|
||||
// The .Where is because JSON_REMOVE returns null if the element to remove wasn't present
|
||||
updateQuery = updateQuery.
|
||||
Set("Participants", sq.Expr(
|
||||
`JSON_REMOVE(Participants, JSON_UNQUOTE(JSON_SEARCH(Participants, 'one', ?)))`, userId,
|
||||
)).
|
||||
Where(sq.Expr(`JSON_CONTAINS(Participants, ?)`, strconv.Quote(userId)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user