diff options
author | Jesús Espino <jespinog@gmail.com> | 2018-08-24 09:53:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-24 09:53:44 +0200 |
commit | bba3bbd9f3e250cca0ce705e664382fe3ad6e78a (patch) | |
tree | 63b8e40450634949d6bc3f80346cd26b7639c49d | |
parent | 8bbee74ed811e7aa8167ad7cea6d05042a90f446 (diff) | |
download | chat-bba3bbd9f3e250cca0ce705e664382fe3ad6e78a.tar.gz chat-bba3bbd9f3e250cca0ce705e664382fe3ad6e78a.tar.bz2 chat-bba3bbd9f3e250cca0ce705e664382fe3ad6e78a.zip |
MM-11572: Force correct order on messages generated in the bulk (#9244)
-rw-r--r-- | app/import.go | 15 | ||||
-rw-r--r-- | cmd/mattermost/commands/sampledata.go | 29 | ||||
-rw-r--r-- | i18n/en.json | 4 | ||||
-rw-r--r-- | store/sqlstore/channel_store.go | 8 | ||||
-rw-r--r-- | store/store.go | 1 | ||||
-rw-r--r-- | store/storetest/mocks/ChannelStore.go | 16 |
6 files changed, 66 insertions, 7 deletions
diff --git a/app/import.go b/app/import.go index 078198dd4..496c6b7fc 100644 --- a/app/import.go +++ b/app/import.go @@ -100,6 +100,10 @@ func (a *App) BulkImport(fileReader io.Reader, dryRun bool, workers int) (*model return model.NewAppError("BulkImport", "app.import.bulk_import.file_scan.error", nil, err.Error(), http.StatusInternalServerError), 0 } + if err := a.finalizeImport(dryRun); err != nil { + return err, 0 + } + return nil, 0 } @@ -165,3 +169,14 @@ func (a *App) ImportLine(line LineImportData, dryRun bool) *model.AppError { return model.NewAppError("BulkImport", "app.import.import_line.unknown_line_type.error", map[string]interface{}{"Type": line.Type}, "", http.StatusBadRequest) } } + +func (a *App) finalizeImport(dryRun bool) *model.AppError { + if dryRun { + return nil + } + result := <-a.Srv.Store.Channel().ResetLastPostAt() + if result.Err != nil { + return result.Err + } + return nil +} diff --git a/cmd/mattermost/commands/sampledata.go b/cmd/mattermost/commands/sampledata.go index 0983ab0df..ed550bf6b 100644 --- a/cmd/mattermost/commands/sampledata.go +++ b/cmd/mattermost/commands/sampledata.go @@ -59,6 +59,15 @@ func randomPastTime(seconds int) int64 { return (today.Unix() * 1000) - int64(rand.Intn(seconds*1000)) } +func sortedRandomDates(size int) []int64 { + dates := make([]int64, size) + for i := 0; i < size; i++ { + dates[i] = randomPastTime(50000) + } + sort.Slice(dates, func(a, b int) bool { return dates[a] < dates[b] }) + return dates +} + func randomEmoji() string { emojis := []string{"+1", "-1", "heart", "blush"} return emojis[rand.Intn(len(emojis))] @@ -274,8 +283,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { for team, channels := range teamsAndChannels { for _, channel := range channels { + dates := sortedRandomDates(postsPerChannel) + for i := 0; i < postsPerChannel; i++ { - postLine := createPost(team, channel, allUsers) + postLine := createPost(team, channel, allUsers, dates[i]) encoder.Encode(postLine) } } @@ -286,8 +297,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { user2 := allUsers[rand.Intn(len(allUsers))] channelLine := createDirectChannel([]string{user1, user2}) encoder.Encode(channelLine) + + dates := sortedRandomDates(postsPerDirectChannel) for j := 0; j < postsPerDirectChannel; j++ { - postLine := createDirectPost([]string{user1, user2}) + postLine := createDirectPost([]string{user1, user2}, dates[j]) encoder.Encode(postLine) } } @@ -303,8 +316,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { } channelLine := createDirectChannel(users) encoder.Encode(channelLine) + + dates := sortedRandomDates(postsPerGroupChannel) for j := 0; j < postsPerGroupChannel; j++ { - postLine := createDirectPost(users) + postLine := createDirectPost(users, dates[j]) encoder.Encode(postLine) } } @@ -529,9 +544,9 @@ func createChannel(idx int, teamName string) app.LineImportData { } } -func createPost(team string, channel string, allUsers []string) app.LineImportData { +func createPost(team string, channel string, allUsers []string, createAt int64) app.LineImportData { message := randomMessage(allUsers) - create_at := randomPastTime(50000) + create_at := createAt user := allUsers[rand.Intn(len(allUsers))] // Some messages are flagged by an user @@ -589,9 +604,9 @@ func createDirectChannel(members []string) app.LineImportData { } } -func createDirectPost(members []string) app.LineImportData { +func createDirectPost(members []string, createAt int64) app.LineImportData { message := randomMessage(members) - create_at := randomPastTime(50000) + create_at := createAt user := members[rand.Intn(len(members))] // Some messages are flagged by an user diff --git a/i18n/en.json b/i18n/en.json index 7d192b88a..b379e08d1 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -4911,6 +4911,10 @@ "translation": "We could not reset the channel schemes" }, { + "id": "store.sql_channel.reset_last_post_at.app_error", + "translation": "We could not reset the channel last post at date" + }, + { "id": "store.sql_channel.save.archived_channel.app_error", "translation": "You can not modify an archived channel" }, diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index e158ba5ea..97f60dda0 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -1921,3 +1921,11 @@ func (s SqlChannelStore) ClearAllCustomRoleAssignments() store.StoreChannel { } }) } + +func (s SqlChannelStore) ResetLastPostAt() store.StoreChannel { + return store.Do(func(result *store.StoreResult) { + if _, err := s.GetMaster().Exec("UPDATE Channels SET LastPostAt = (SELECT UpdateAt FROM Posts WHERE ChannelId = Channels.Id ORDER BY UpdateAt DESC LIMIT 1);"); err != nil { + result.Err = model.NewAppError("SqlChannelStore.ResetLastPostAt", "store.sql_channel.reset_last_post_at.app_error", nil, err.Error(), http.StatusInternalServerError) + } + }) +} diff --git a/store/store.go b/store/store.go index 34ba08091..0c89a0a91 100644 --- a/store/store.go +++ b/store/store.go @@ -172,6 +172,7 @@ type ChannelStore interface { MigrateChannelMembers(fromChannelId string, fromUserId string) StoreChannel ResetAllChannelSchemes() StoreChannel ClearAllCustomRoleAssignments() StoreChannel + ResetLastPostAt() StoreChannel } type ChannelMemberHistoryStore interface { diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 8adc98e10..747a844ec 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -711,6 +711,22 @@ func (_m *ChannelStore) ResetAllChannelSchemes() store.StoreChannel { return r0 } +// ResetLastPostAt provides a mock function with given fields: +func (_m *ChannelStore) ResetLastPostAt() store.StoreChannel { + ret := _m.Called() + + var r0 store.StoreChannel + if rf, ok := ret.Get(0).(func() store.StoreChannel); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(store.StoreChannel) + } + } + + return r0 +} + // Restore provides a mock function with given fields: channelId, time func (_m *ChannelStore) Restore(channelId string, time int64) store.StoreChannel { ret := _m.Called(channelId, time) |