First part of PostStore migration (#15123)

Automatic Merge
Этот коммит содержится в:
Rodrigo Villablanca
2020-08-12 13:35:57 -04:00
коммит произвёл GitHub
родитель d9ee26a47b
Коммит 5566395032
18 изменённых файлов: 367 добавлений и 276 удалений

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

@@ -2330,7 +2330,7 @@ func (a *App) ViewChannel(view *model.ChannelView, userId string, currentSession
func (a *App) PermanentDeleteChannel(channel *model.Channel) *model.AppError {
if err := a.Srv().Store.Post().PermanentDeleteByChannel(channel.Id); err != nil {
return err
return model.NewAppError("PermanentDeleteChannel", "app.post.permanent_delete_by_channel.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if err := a.Srv().Store.Channel().PermanentDeleteMembersByChannel(channel.Id); err != nil {

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

@@ -291,12 +291,13 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
fileMigrationLock.Lock()
defer fileMigrationLock.Unlock()
result, err := a.Srv().Store.Post().Get(post.Id, false)
if err != nil {
mlog.Error("Unable to get post when migrating post to use FileInfos", mlog.Err(err), mlog.String("post_id", post.Id))
result, nErr := a.Srv().Store.Post().Get(post.Id, false)
if nErr != nil {
mlog.Error("Unable to get post when migrating post to use FileInfos", mlog.Err(nErr), mlog.String("post_id", post.Id))
return []*model.FileInfo{}
}
var err *model.AppError
if newPost := result.Posts[post.Id]; len(newPost.Filenames) != len(post.Filenames) {
// Another thread has already created FileInfos for this post, so just return those
var fileInfos []*model.FileInfo
@@ -337,13 +338,13 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
newPost.FileIds = fileIds
// Update Posts to clear Filenames and set FileIds
if _, err = a.Srv().Store.Post().Update(newPost, post); err != nil {
if _, nErr = a.Srv().Store.Post().Update(newPost, post); nErr != nil {
mlog.Error(
"Unable to save migrated post when migrating to use FileInfos",
mlog.String("new_file_ids", strings.Join(newPost.FileIds, ",")),
mlog.String("old_filenames", strings.Join(post.Filenames, ",")),
mlog.String("post_id", post.Id),
mlog.Err(err),
mlog.Err(nErr),
)
return []*model.FileInfo{}
}

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

@@ -1038,7 +1038,16 @@ func (a *App) importReplies(data []ReplyImportData, post *model.Post, teamId str
if len(postsForCreateList) > 0 {
if _, _, err := a.Srv().Store.Post().SaveMultiple(postsForCreateList); err != nil {
return err
var appErr *model.AppError
var invErr *store.ErrInvalidInput
switch {
case errors.As(err, &appErr):
return appErr
case errors.As(err, &invErr):
return model.NewAppError("importReplies", "app.post.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
default:
return model.NewAppError("importReplies", "app.post.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
}
@@ -1229,9 +1238,9 @@ func (a *App) importMultiplePostLines(lines []LineImportWorkerData, dryRun bool)
user := users[*line.Post.User]
// Check if this post already exists.
posts, err := a.Srv().Store.Post().GetPostsCreatedAt(channel.Id, *line.Post.CreateAt)
if err != nil {
return line.LineNumber, err
posts, appErr := a.Srv().Store.Post().GetPostsCreatedAt(channel.Id, *line.Post.CreateAt)
if appErr != nil {
return line.LineNumber, appErr
}
var post *model.Post
@@ -1256,9 +1265,9 @@ func (a *App) importMultiplePostLines(lines []LineImportWorkerData, dryRun bool)
post.Props = *line.Post.Props
}
fileIds, err := a.uploadAttachments(line.Post.Attachments, post, team.Id, dryRun)
if err != nil {
return line.LineNumber, err
fileIds, appErr := a.uploadAttachments(line.Post.Attachments, post, team.Id, dryRun)
if appErr != nil {
return line.LineNumber, appErr
}
for _, fileID := range post.FileIds {
if _, ok := fileIds[fileID]; !ok {
@@ -1281,14 +1290,26 @@ func (a *App) importMultiplePostLines(lines []LineImportWorkerData, dryRun bool)
}
if len(postsForCreateList) > 0 {
if _, idx, err := a.Srv().Store.Post().SaveMultiple(postsForCreateList); err != nil {
if _, idx, nErr := a.Srv().Store.Post().SaveMultiple(postsForCreateList); nErr != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput
var retErr *model.AppError
switch {
case errors.As(nErr, &appErr):
retErr = appErr
case errors.As(nErr, &invErr):
retErr = model.NewAppError("importMultiplePostLines", "app.post.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
default:
retErr = model.NewAppError("importMultiplePostLines", "app.post.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
if idx != -1 && idx < len(postsForCreateList) {
post := postsForCreateList[idx]
if lineNumber, ok := postsForCreateMap[getPostStrID(post)]; ok {
return lineNumber, err
return lineNumber, retErr
}
}
return 0, err
return 0, retErr
}
}
@@ -1566,13 +1587,25 @@ func (a *App) importMultipleDirectPostLines(lines []LineImportWorkerData, dryRun
if len(postsForCreateList) > 0 {
if _, idx, err := a.Srv().Store.Post().SaveMultiple(postsForCreateList); err != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput
var retErr *model.AppError
switch {
case errors.As(err, &appErr):
retErr = appErr
case errors.As(err, &invErr):
retErr = model.NewAppError("importMultiplePostLines", "app.post.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
default:
retErr = model.NewAppError("importMultiplePostLines", "app.post.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if idx != -1 && idx < len(postsForCreateList) {
post := postsForCreateList[idx]
if lineNumber, ok := postsForCreateMap[getPostStrID(post)]; ok {
return lineNumber, err
return lineNumber, retErr
}
}
return 0, err
return 0, retErr
}
}
if _, idx, err := a.Srv().Store.Post().OverwriteMultiple(postsForOverwriteList); err != nil {

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

@@ -71,7 +71,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
pchan := make(chan store.StoreResult, 1)
go func() {
post, err := a.Srv().Store.Post().GetSingle(postId)
pchan <- store.StoreResult{Data: post, Err: err}
pchan <- store.StoreResult{Data: post, NErr: err}
close(pchan)
}()
@@ -90,16 +90,22 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
}()
result := <-pchan
if result.Err != nil {
if result.NErr != nil {
if cookie == nil {
return "", result.Err
var nfErr *store.ErrNotFound
switch {
case errors.As(result.NErr, &nfErr):
return "", model.NewAppError("DoPostActionWithCookie", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return "", model.NewAppError("DoPostActionWithCookie", "app.post.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
}
}
if cookie.Integration == nil {
return "", model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "no Integration in action cookie", http.StatusBadRequest)
return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_integration.app_error", nil, "no Integration in action cookie", http.StatusBadRequest)
}
if postId != cookie.PostId {
return "", model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "postId doesn't match", http.StatusBadRequest)
return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_integration.app_error", nil, "postId doesn't match", http.StatusBadRequest)
}
channel, err := a.Srv().Store.Channel().Get(cookie.ChannelId, true)

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

@@ -419,8 +419,8 @@ func TestPostActionProps(t *testing.T) {
require.Nil(t, err)
assert.True(t, len(clientTriggerId) == 26)
newPost, err := th.App.Srv().Store.Post().GetSingle(post.Id)
require.Nil(t, err)
newPost, nErr := th.App.Srv().Store.Post().GetSingle(post.Id)
require.Nil(t, nErr)
assert.True(t, newPost.IsPinned)
assert.False(t, newPost.HasReactions)

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

@@ -180,7 +180,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
pchan = make(chan store.StoreResult, 1)
go func() {
r, pErr := a.Srv().Store.Post().Get(post.RootId, false)
pchan <- store.StoreResult{Data: r, Err: pErr}
pchan <- store.StoreResult{Data: r, NErr: pErr}
close(pchan)
}()
}
@@ -221,7 +221,7 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
var parentPostList *model.PostList
if pchan != nil {
result := <-pchan
if result.Err != nil {
if result.NErr != nil {
return nil, model.NewAppError("createPost", "api.post.create_post.root_id.app_error", nil, "", http.StatusBadRequest)
}
parentPostList = result.Data.(*model.PostList)
@@ -290,9 +290,18 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
}
}
rpost, err := a.Srv().Store.Post().Save(post)
if err != nil {
return nil, err
rpost, nErr := a.Srv().Store.Post().Save(post)
if nErr != nil {
var appErr *model.AppError
var invErr *store.ErrInvalidInput
switch {
case errors.As(nErr, &appErr):
return nil, appErr
case errors.As(nErr, &invErr):
return nil, model.NewAppError("CreatePost", "app.post.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
default:
return nil, model.NewAppError("CreatePost", "app.post.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
// Update the mapping from pending post id to the actual post id, for any clients that
@@ -512,12 +521,22 @@ func (a *App) DeleteEphemeralPost(userId, postId string) {
func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model.AppError) {
post.SanitizeProps()
postLists, err := a.Srv().Store.Post().Get(post.Id, false)
if err != nil {
return nil, err
postLists, nErr := a.Srv().Store.Post().Get(post.Id, false)
if nErr != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
switch {
case errors.As(nErr, &invErr):
return nil, model.NewAppError("UpdatePost", "app.post.get.app_error", nil, invErr.Error(), http.StatusBadRequest)
case errors.As(nErr, &nfErr):
return nil, model.NewAppError("UpdatePost", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("UpdatePost", "app.post.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
oldPost := postLists.Posts[post.Id]
var err *model.AppError
if oldPost == nil {
err = model.NewAppError("UpdatePost", "api.post.update_post.find.app_error", nil, "id="+post.Id, http.StatusBadRequest)
return nil, err
@@ -586,9 +605,15 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
}
}
rpost, err := a.Srv().Store.Post().Update(newPost, oldPost)
if err != nil {
return nil, err
rpost, nErr := a.Srv().Store.Post().Update(newPost, oldPost)
if nErr != nil {
var appErr *model.AppError
switch {
case errors.As(nErr, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("UpdatePost", "app.post.update.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
@@ -659,11 +684,36 @@ func (a *App) GetPostsSince(options model.GetPostsSinceOptions) (*model.PostList
}
func (a *App) GetSinglePost(postId string) (*model.Post, *model.AppError) {
return a.Srv().Store.Post().GetSingle(postId)
post, err := a.Srv().Store.Post().GetSingle(postId)
if err != nil {
var nfErr *store.ErrNotFound
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("GetSinglePost", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("GetSinglePost", "app.post.get.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return post, nil
}
func (a *App) GetPostThread(postId string, skipFetchThreads bool) (*model.PostList, *model.AppError) {
return a.Srv().Store.Post().Get(postId, skipFetchThreads)
posts, err := a.Srv().Store.Post().Get(postId, skipFetchThreads)
if err != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
switch {
case errors.As(err, &invErr):
return nil, model.NewAppError("GetPostThread", "app.post.get.app_error", nil, invErr.Error(), http.StatusBadRequest)
case errors.As(err, &nfErr):
return nil, model.NewAppError("GetPostThread", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("GetPostThread", "app.post.get.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return posts, nil
}
func (a *App) GetFlaggedPosts(userId string, offset int, limit int) (*model.PostList, *model.AppError) {
@@ -679,9 +729,18 @@ func (a *App) GetFlaggedPostsForChannel(userId, channelId string, offset int, li
}
func (a *App) GetPermalinkPost(postId string, userId string) (*model.PostList, *model.AppError) {
list, err := a.Srv().Store.Post().Get(postId, false)
if err != nil {
return nil, err
list, nErr := a.Srv().Store.Post().Get(postId, false)
if nErr != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
switch {
case errors.As(nErr, &invErr):
return nil, model.NewAppError("GetPermalinkPost", "app.post.get.app_error", nil, invErr.Error(), http.StatusBadRequest)
case errors.As(nErr, &nfErr):
return nil, model.NewAppError("GetPermalinkPost", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("GetPermalinkPost", "app.post.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
if len(list.Order) != 1 {
@@ -842,10 +901,9 @@ func (a *App) GetPostsForChannelAroundLastUnread(channelId, userId string, limit
}
func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppError) {
post, err := a.Srv().Store.Post().GetSingle(postId)
if err != nil {
err.StatusCode = http.StatusBadRequest
return nil, err
post, nErr := a.Srv().Store.Post().GetSingle(postId)
if nErr != nil {
return nil, model.NewAppError("DeletePost", "app.post.get.app_error", nil, nErr.Error(), http.StatusBadRequest)
}
channel, err := a.GetChannel(post.ChannelId)
@@ -859,7 +917,13 @@ func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppErro
}
if err := a.Srv().Store.Post().Delete(postId, model.GetMillis(), deleteByID); err != nil {
return nil, err
var nfErr *store.ErrNotFound
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("DeletePost", "app.post.delete.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("DeletePost", "app.post.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil)
@@ -1047,7 +1111,7 @@ func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo
pchan := make(chan store.StoreResult, 1)
go func() {
post, err := a.Srv().Store.Post().GetSingle(postId)
pchan <- store.StoreResult{Data: post, Err: err}
pchan <- store.StoreResult{Data: post, NErr: err}
close(pchan)
}()
@@ -1059,8 +1123,14 @@ func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo
if len(infos) == 0 {
// No FileInfos were returned so check if they need to be created for this post
result := <-pchan
if result.Err != nil {
return nil, result.Err
if result.NErr != nil {
var nfErr *store.ErrNotFound
switch {
case errors.As(result.NErr, &nfErr):
return nil, model.NewAppError("GetFileInfosForPostWithMigration", "app.post.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("GetFileInfosForPostWithMigration", "app.post.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
}
}
post := result.Data.(*model.Post)

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

@@ -1489,7 +1489,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
}
if err := a.Srv().Store.Post().PermanentDeleteByUser(user.Id); err != nil {
return err
return model.NewAppError("PermanentDeleteUser", "app.post.permanent_delete_by_user.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if err := a.Srv().Store.Bot().PermanentDelete(user.Id); err != nil {