[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 (
|
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user