- soft delete reaction by setting new field Reactions.DeleteAt to non-zero.
- include new field Reactions.UpdateAt
Этот коммит содержится в:
Doug Lauder
2021-01-20 10:09:23 -05:00
коммит произвёл GitHub
родитель 11c6d07d6a
Коммит dbbf985e61
9 изменённых файлов: 227 добавлений и 55 удалений

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

@@ -4,12 +4,12 @@
package sqlstore
import (
"github.com/mattermost/gorp"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/gorp"
"github.com/pkg/errors"
)
type SqlReactionStore struct {
@@ -40,7 +40,7 @@ func (s *SqlReactionStore) Save(reaction *model.Reaction) (*model.Reaction, erro
return nil, errors.Wrap(err, "begin_transaction")
}
defer finalizeTransaction(transaction)
err = saveReactionAndUpdatePost(transaction, reaction)
err = s.saveReactionAndUpdatePost(transaction, reaction)
if err != nil {
// We don't consider duplicated save calls as an error
if !IsUniqueConstraintError(err, []string{"reactions_pkey", "PRIMARY"}) {
@@ -56,6 +56,8 @@ func (s *SqlReactionStore) Save(reaction *model.Reaction) (*model.Reaction, erro
}
func (s *SqlReactionStore) Delete(reaction *model.Reaction) (*model.Reaction, error) {
reaction.PreUpdate()
transaction, err := s.GetMaster().Begin()
if err != nil {
return nil, errors.Wrap(err, "begin_transaction")
@@ -78,11 +80,16 @@ func (s *SqlReactionStore) GetForPost(postId string, allowFromCache bool) ([]*mo
if _, err := s.GetReplica().Select(&reactions,
`SELECT
*
UserId,
PostId,
EmojiName,
CreateAt,
COALESCE(UpdateAt, CreateAt) As UpdateAt,
COALESCE(DeleteAt, 0) As DeleteAt
FROM
Reactions
WHERE
PostId = :PostId
PostId = :PostId AND COALESCE(DeleteAt, 0) = 0
ORDER BY
CreateAt`, map[string]interface{}{"PostId": postId}); err != nil {
return nil, errors.Wrapf(err, "failed to get Reactions with postId=%s", postId)
@@ -95,12 +102,18 @@ func (s *SqlReactionStore) BulkGetForPosts(postIds []string) ([]*model.Reaction,
keys, params := MapStringsToQueryParams(postIds, "postId")
var reactions []*model.Reaction
if _, err := s.GetReplica().Select(&reactions, `SELECT
*
if _, err := s.GetReplica().Select(&reactions,
`SELECT
UserId,
PostId,
EmojiName,
CreateAt,
COALESCE(UpdateAt, CreateAt) As UpdateAt,
COALESCE(DeleteAt, 0) As DeleteAt
FROM
Reactions
WHERE
PostId IN `+keys+`
PostId IN `+keys+` AND COALESCE(DeleteAt, 0) = 0
ORDER BY
CreateAt`, params); err != nil {
return nil, errors.Wrap(err, "failed to get Reactions")
@@ -110,22 +123,36 @@ func (s *SqlReactionStore) BulkGetForPosts(postIds []string) ([]*model.Reaction,
func (s *SqlReactionStore) DeleteAllWithEmojiName(emojiName string) error {
var reactions []*model.Reaction
now := model.GetMillis()
params := map[string]interface{}{
"EmojiName": emojiName,
"UpdateAt": now,
"DeleteAt": now,
}
if _, err := s.GetReplica().Select(&reactions,
`SELECT
*
FROM
Reactions
WHERE
EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName}); err != nil {
UserId,
PostId,
EmojiName,
CreateAt,
COALESCE(UpdateAt, CreateAt) As UpdateAt,
COALESCE(DeleteAt, 0) As DeleteAt
FROM
Reactions
WHERE
EmojiName = :EmojiName AND COALESCE(DeleteAt, 0) = 0`, params); err != nil {
return errors.Wrapf(err, "failed to get Reactions with emojiName=%s", emojiName)
}
_, err := s.GetMaster().Exec(
`DELETE FROM
`UPDATE
Reactions
SET
UpdateAt = :UpdateAt, DeleteAt = :DeleteAt
WHERE
EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName})
EmojiName = :EmojiName AND COALESCE(DeleteAt, 0) = 0`, params)
if err != nil {
return errors.Wrapf(err, "failed to delete Reactions with emojiName=%s", emojiName)
}
@@ -167,23 +194,60 @@ func (s *SqlReactionStore) PermanentDeleteBatch(endTime int64, limit int64) (int
return rowsAffected, nil
}
func saveReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
if err := transaction.Insert(reaction); err != nil {
return err
func (s *SqlReactionStore) saveReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
params := map[string]interface{}{
"UserId": reaction.UserId,
"PostId": reaction.PostId,
"EmojiName": reaction.EmojiName,
"CreateAt": reaction.CreateAt,
"UpdateAt": reaction.UpdateAt,
}
if s.DriverName() == model.DATABASE_DRIVER_MYSQL {
if _, err := transaction.Exec(
`INSERT INTO
Reactions
(UserId, PostId, EmojiName, CreateAt, UpdateAt, DeleteAt)
VALUES
(:UserId, :PostId, :EmojiName, :CreateAt, :UpdateAt, 0)
ON DUPLICATE KEY UPDATE
UpdateAt = :UpdateAt, DeleteAt = 0`, params); err != nil {
return err
}
} else if s.DriverName() == model.DATABASE_DRIVER_POSTGRES {
if _, err := transaction.Exec(
`INSERT INTO
Reactions
(UserId, PostId, EmojiName, CreateAt, UpdateAt, DeleteAt)
VALUES
(:UserId, :PostId, :EmojiName, :CreateAt, :UpdateAt, 0)
ON CONFLICT (UserId, PostId, EmojiName)
DO UPDATE SET UpdateAt = :UpdateAt, DeleteAt = 0`, params); err != nil {
return err
}
}
return updatePostForReactionsOnInsert(transaction, reaction.PostId)
}
func deleteReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
params := map[string]interface{}{
"UserId": reaction.UserId,
"PostId": reaction.PostId,
"EmojiName": reaction.EmojiName,
"CreateAt": reaction.CreateAt,
"UpdateAt": reaction.UpdateAt,
"DeleteAt": reaction.UpdateAt, // DeleteAt = UpdateAt
}
if _, err := transaction.Exec(
`DELETE FROM
`UPDATE
Reactions
SET
UpdateAt = :UpdateAt, DeleteAt = :DeleteAt
WHERE
PostId = :PostId AND
UserId = :UserId AND
EmojiName = :EmojiName`,
map[string]interface{}{"PostId": reaction.PostId, "UserId": reaction.UserId, "EmojiName": reaction.EmojiName}); err != nil {
EmojiName = :EmojiName`, params); err != nil {
return err
}
@@ -195,7 +259,7 @@ const (
Posts
SET
UpdateAt = :UpdateAt,
HasReactions = (SELECT count(0) > 0 FROM Reactions WHERE PostId = :PostId)
HasReactions = (SELECT count(0) > 0 FROM Reactions WHERE PostId = :PostId AND COALESCE(DeleteAt, 0) = 0)
WHERE
Id = :PostId`
)

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

@@ -10,5 +10,5 @@ import (
)
func TestReactionStore(t *testing.T) {
StoreTest(t, storetest.TestReactionStore)
StoreTestWithSqlStore(t, storetest.TestReactionStore)
}

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

@@ -966,6 +966,9 @@ func upgradeDatabaseToVersion532(sqlStore *SqlStore) {
sqlStore.AlterColumnTypeIfExists("Posts", "FileIds", "text", "varchar(300)")
sqlStore.CreateColumnIfNotExists("ThreadMemberships", "UnreadMentions", "bigint", "bigint", "0")
sqlStore.CreateColumnIfNotExistsNoDefault("Channels", "Shared", "tinyint(1)", "boolean")
sqlStore.CreateColumnIfNotExistsNoDefault("Reactions", "UpdateAt", "bigint", "bigint")
sqlStore.CreateColumnIfNotExistsNoDefault("Reactions", "DeleteAt", "bigint", "bigint")
// saveSchemaVersion(sqlStore, Version5320)
// }
}