Migrate FileInfo store to Sync by default (#10837)

Этот коммит содержится в:
Jesús Espino
2019-05-15 22:07:03 +02:00
коммит произвёл GitHub
родитель 99ea780f20
Коммит beb7592c93
17 изменённых файлов: 462 добавлений и 459 удалений

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

@@ -30,7 +30,6 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/services/filesstore"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
)
@@ -276,14 +275,14 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
if newPost := result.Data.(*model.PostList).Posts[post.Id]; len(newPost.Filenames) != len(post.Filenames) {
// Another thread has already created FileInfos for this post, so just return those
result := <-a.Srv.Store.FileInfo().GetForPost(post.Id, true, false)
if result.Err != nil {
fileInfos, err := a.Srv.Store.FileInfo().GetForPost(post.Id, true, false)
if err != nil {
mlog.Error(fmt.Sprintf("Unable to get FileInfos for migrated post, err=%v", result.Err), mlog.String("post_id", post.Id))
return []*model.FileInfo{}
}
mlog.Debug("Post already migrated to use FileInfos", mlog.String("post_id", post.Id))
return result.Data.([]*model.FileInfo)
return fileInfos
}
mlog.Debug("Migrating post to use FileInfos", mlog.String("post_id", post.Id))
@@ -291,9 +290,9 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
savedInfos := make([]*model.FileInfo, 0, len(infos))
fileIds := make([]string, 0, len(filenames))
for _, info := range infos {
if result := <-a.Srv.Store.FileInfo().Save(info); result.Err != nil {
if _, err := a.Srv.Store.FileInfo().Save(info); err != nil {
mlog.Error(
fmt.Sprintf("Unable to save file info when migrating post to use FileInfos, err=%v", result.Err),
fmt.Sprintf("Unable to save file info when migrating post to use FileInfos, err=%v", err),
mlog.String("post_id", post.Id),
mlog.String("file_info_id", info.Id),
mlog.String("file_info_path", info.Path),
@@ -506,7 +505,7 @@ type uploadFileTask struct {
// Testing: overrideable dependency functions
pluginsEnvironment *plugin.Environment
writeFile func(io.Reader, string) (int64, *model.AppError)
saveToDatabase func(*model.FileInfo) store.StoreChannel
saveToDatabase func(*model.FileInfo) (*model.FileInfo, *model.AppError)
}
func (t *uploadFileTask) init(a *App) {
@@ -605,8 +604,8 @@ func (a *App) UploadFileX(channelId, name string, input io.Reader,
return nil, aerr
}
if result := <-t.saveToDatabase(t.fileinfo); result.Err != nil {
return nil, result.Err
if _, err := t.saveToDatabase(t.fileinfo); err != nil {
return nil, err
}
wg.Wait()
@@ -915,8 +914,8 @@ func (a *App) DoUploadFileExpectModification(now time.Time, rawTeamId string, ra
return nil, data, err
}
if result := <-a.Srv.Store.FileInfo().Save(info); result.Err != nil {
return nil, data, result.Err
if _, err := a.Srv.Store.FileInfo().Save(info); err != nil {
return nil, data, err
}
return info, data, nil
@@ -1059,11 +1058,7 @@ func (a *App) generatePreviewImage(img image.Image, previewPath string, width in
}
func (a *App) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
result := <-a.Srv.Store.FileInfo().Get(fileId)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.FileInfo), nil
return a.Srv.Store.FileInfo().Get(fileId)
}
func (a *App) GetFile(fileId string) ([]byte, *model.AppError) {
@@ -1086,21 +1081,19 @@ func (a *App) CopyFileInfos(userId string, fileIds []string) ([]string, *model.A
now := model.GetMillis()
for _, fileId := range fileIds {
result := <-a.Srv.Store.FileInfo().Get(fileId)
if result.Err != nil {
return nil, result.Err
fileInfo, err := a.Srv.Store.FileInfo().Get(fileId)
if err != nil {
return nil, err
}
fileInfo := result.Data.(*model.FileInfo)
fileInfo.Id = model.NewId()
fileInfo.CreatorId = userId
fileInfo.CreateAt = now
fileInfo.UpdateAt = now
fileInfo.PostId = ""
if result := <-a.Srv.Store.FileInfo().Save(fileInfo); result.Err != nil {
return newFileIds, result.Err
if _, err := a.Srv.Store.FileInfo().Save(fileInfo); err != nil {
return newFileIds, err
}
newFileIds = append(newFileIds, fileInfo.Id)

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

@@ -91,7 +91,7 @@ func BenchmarkUploadFile(b *testing.B) {
if err != nil {
b.Fatal(err)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.RemoveFile(info1.Path)
},
@@ -110,7 +110,7 @@ func BenchmarkUploadFile(b *testing.B) {
if aerr != nil {
b.Fatal(aerr)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
},
},
@@ -128,7 +128,7 @@ func BenchmarkUploadFile(b *testing.B) {
if aerr != nil {
b.Fatal(aerr)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
},
},
@@ -143,7 +143,7 @@ func BenchmarkUploadFile(b *testing.B) {
if err != nil {
b.Fatal(err)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(resp.FileInfos[0].Id)
th.App.Srv.Store.FileInfo().PermanentDelete(resp.FileInfos[0].Id)
th.App.RemoveFile(resp.FileInfos[0].Path)
},
},
@@ -160,7 +160,7 @@ func BenchmarkUploadFile(b *testing.B) {
if aerr != nil {
b.Fatal(aerr)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
},
},
@@ -177,7 +177,7 @@ func BenchmarkUploadFile(b *testing.B) {
if aerr != nil {
b.Fatal(aerr)
}
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
},
},

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

@@ -55,7 +55,7 @@ func TestDoUploadFile(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.RemoveFile(info1.Path)
}()
}
@@ -69,7 +69,7 @@ func TestDoUploadFile(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
th.App.RemoveFile(info2.Path)
}()
}
@@ -83,7 +83,7 @@ func TestDoUploadFile(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
th.App.RemoveFile(info3.Path)
}()
}
@@ -97,7 +97,7 @@ func TestDoUploadFile(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info4.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info4.Id)
th.App.RemoveFile(info4.Path)
}()
}
@@ -120,7 +120,7 @@ func TestUploadFile(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.RemoveFile(info1.Path)
}()
}
@@ -199,7 +199,7 @@ func TestCopyFileInfos(t *testing.T) {
info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
require.Nil(t, err)
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.RemoveFile(info1.Path)
}()
@@ -209,7 +209,7 @@ func TestCopyFileInfos(t *testing.T) {
info2, err := th.App.GetFileInfo(infoIds[0])
require.Nil(t, err)
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
th.App.RemoveFile(info2.Path)
}()

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

@@ -1064,8 +1064,8 @@ func (a *App) uploadAttachments(attachments *[]AttachmentImportData, post *model
func (a *App) UpdateFileInfoWithPostId(post *model.Post) {
for _, fileId := range post.FileIds {
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); result.Err != nil {
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, result.Err), mlog.String("post_id", post.Id))
if err := a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); err != nil {
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, err), mlog.String("post_id", post.Id))
}
}
}

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

@@ -11,6 +11,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils/fileutils"
@@ -250,12 +251,9 @@ func TestImportProcessImportDataFileVersionLine(t *testing.T) {
}
func GetAttachments(userId string, th *TestHelper, t *testing.T) []*model.FileInfo {
if result := <-th.App.Srv.Store.FileInfo().GetForUser(userId); result.Err != nil {
t.Fatal(result.Err.Error())
} else {
return result.Data.([]*model.FileInfo)
}
return nil
fileInfos, err := th.App.Srv.Store.FileInfo().GetForUser(userId)
require.Nil(t, err)
return fileInfos
}
func AssertFileIdsInPost(files []*model.FileInfo, th *TestHelper, t *testing.T) {

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

@@ -30,10 +30,14 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
pchan := a.Srv.Store.User().GetAllProfilesInChannel(channel.Id, true)
cmnchan := a.Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true)
var fchan store.StoreChannel
var fchan chan store.StoreResult
if len(post.FileIds) != 0 {
fchan = a.Srv.Store.FileInfo().GetForPost(post.Id, true, true)
fchan = make(chan store.StoreResult, 1)
go func() {
fileInfos, err := a.Srv.Store.FileInfo().GetForPost(post.Id, true, true)
fchan <- store.StoreResult{Data: fileInfos, Err: err}
close(fchan)
}()
}
result := <-pchan

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

@@ -290,11 +290,10 @@ func (a *App) GetMessageForNotification(post *model.Post, translateFunc i18n.Tra
}
// extract the filenames from their paths and determine what type of files are attached
result := <-a.Srv.Store.FileInfo().GetForPost(post.Id, true, true)
if result.Err != nil {
mlog.Warn(fmt.Sprintf("Encountered error when getting files for notification message, post_id=%v, err=%v", post.Id, result.Err), mlog.String("post_id", post.Id))
infos, err := a.Srv.Store.FileInfo().GetForPost(post.Id, true, true)
if err != nil {
mlog.Warn(fmt.Sprintf("Encountered error when getting files for notification message, post_id=%v, err=%v", post.Id, err), mlog.String("post_id", post.Id))
}
infos := result.Data.([]*model.FileInfo)
filenames := make([]string, len(infos))
onlyImages := true

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

@@ -291,7 +291,7 @@ func TestPluginAPIGetFile(t *testing.T) {
info, err := th.App.DoUploadFile(uploadTime, th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, filename, fileData)
require.Nil(t, err)
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
}()

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

@@ -302,9 +302,9 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
func (a *App) attachFilesToPost(post *model.Post) *model.AppError {
var attachedIds []string
for _, fileId := range post.FileIds {
result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId)
if result.Err != nil {
mlog.Warn("Failed to attach file to post", mlog.String("file_id", fileId), mlog.String("post_id", post.Id), mlog.Err(result.Err))
err := a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId)
if err != nil {
mlog.Warn("Failed to attach file to post", mlog.String("file_id", fileId), mlog.String("post_id", post.Id), mlog.Err(err))
continue
}
@@ -776,8 +776,8 @@ func (a *App) DeletePostFiles(post *model.Post) {
return
}
if result := <-a.Srv.Store.FileInfo().DeleteForPost(post.Id); result.Err != nil {
mlog.Warn(fmt.Sprintf("Encountered error when deleting files for post, post_id=%v, err=%v", post.Id, result.Err), mlog.String("post_id", post.Id))
if _, err := a.Srv.Store.FileInfo().DeleteForPost(post.Id); err != nil {
mlog.Warn(fmt.Sprintf("Encountered error when deleting files for post, post_id=%v, err=%v", post.Id, err), mlog.String("post_id", post.Id))
}
}
@@ -980,12 +980,7 @@ func (a *App) GetFileInfosForPostWithMigration(postId string) ([]*model.FileInfo
}
func (a *App) GetFileInfosForPost(postId string) ([]*model.FileInfo, *model.AppError) {
result := <-a.Srv.Store.FileInfo().GetForPost(postId, false, true)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.FileInfo), nil
return a.Srv.Store.FileInfo().GetForPost(postId, false, true)
}
func (a *App) PostWithProxyAddedToImageURLs(post *model.Post) *model.Post {

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

@@ -191,19 +191,22 @@ func TestAttachFilesToPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
info1 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
info1, err := th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
CreatorId: th.BasicUser.Id,
Path: "path.txt",
})).(*model.FileInfo)
info2 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
})
require.Nil(t, err)
info2, err := th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
CreatorId: th.BasicUser.Id,
Path: "path.txt",
})).(*model.FileInfo)
})
require.Nil(t, err)
post := th.BasicPost
post.FileIds = []string{info1.Id, info2.Id}
err := th.App.attachFilesToPost(post)
err = th.App.attachFilesToPost(post)
assert.Nil(t, err)
infos, err := th.App.GetFileInfosForPost(post.Id)
@@ -215,20 +218,23 @@ func TestAttachFilesToPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
info1 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
info1, err := th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
CreatorId: th.BasicUser.Id,
Path: "path.txt",
PostId: model.NewId(),
})).(*model.FileInfo)
info2 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
})
require.Nil(t, err)
info2, err := th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
CreatorId: th.BasicUser.Id,
Path: "path.txt",
})).(*model.FileInfo)
})
require.Nil(t, err)
post := th.BasicPost
post.FileIds = []string{info1.Id, info2.Id}
err := th.App.attachFilesToPost(post)
err = th.App.attachFilesToPost(post)
assert.Nil(t, err)
infos, err := th.App.GetFileInfosForPost(post.Id)
@@ -623,7 +629,7 @@ func TestDeletePostWithFileAttachments(t *testing.T) {
t.Fatal(err)
} else {
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
th.App.RemoveFile(info1.Path)
}()
}

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

@@ -747,8 +747,8 @@ func (a *App) OldImportPost(post *model.Post) string {
firstPostId = post.Id
}
for _, fileId := range post.FileIds {
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); result.Err != nil {
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, result.Err), mlog.String("post_id", post.Id))
if err := a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); err != nil {
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, err), mlog.String("post_id", post.Id))
}
}
post.FileIds = nil

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

@@ -1447,12 +1447,11 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
return result.Err
}
result := <-a.Srv.Store.FileInfo().GetForUser(user.Id)
if result.Err != nil {
infos, err := a.Srv.Store.FileInfo().GetForUser(user.Id)
if err != nil {
mlog.Warn("Error getting file list for user from FileInfoStore")
}
infos := result.Data.([]*model.FileInfo)
for _, info := range infos {
res, err := a.FileExists(info.Path)
if err != nil {
@@ -1480,8 +1479,8 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
}
}
if result := <-a.Srv.Store.FileInfo().PermanentDeleteByUser(user.Id); result.Err != nil {
return result.Err
if _, err := a.Srv.Store.FileInfo().PermanentDeleteByUser(user.Id); err != nil {
return err
}
if result := <-a.Srv.Store.User().PermanentDelete(user.Id); result.Err != nil {