PLT-7619: Cleanup flags in data retention. (#7501)

This commit is contained in:
George Goldberg
2017-09-22 18:23:16 +01:00
committed by Harrison Healey
parent 8bef94d250
commit e0d5703f72
4 changed files with 98 additions and 0 deletions

View File

@@ -5887,6 +5887,10 @@
"id": "store.sql_post.permanent_delete_batch.app_error",
"translation": "We encountered an error permanently deleting the batch of posts"
},
{
"id": "store.sql_preference.cleanup_flags_batch.app_error",
"translation": "We encountered an error cleaning up the batch of flags"
},
{
"id": "store.sql_preference.delete.app_error",
"translation": "We encountered an error while deleting preferences"

View File

@@ -372,3 +372,55 @@ func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string)
return storeChannel
}
func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
query :=
`DELETE FROM
Preferences
WHERE
Category = :Category
AND Name IN (
SELECT
*
FROM (
SELECT
Preferences.Name
FROM
Preferences
LEFT JOIN
Posts
ON
Preferences.Name = Posts.Id
WHERE
Preferences.Category = :Category
AND Posts.Id IS null
LIMIT
:Limit
)
AS t
)`
sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Limit": limit})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError)
} else {
rowsAffected, err1 := sqlResult.RowsAffected()
if err1 != nil {
result.Err = model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError)
result.Data = int64(0)
} else {
result.Data = rowsAffected
}
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}

View File

@@ -6,6 +6,8 @@ package store
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
)
@@ -473,3 +475,42 @@ func TestPreferenceDeleteCategoryAndName(t *testing.T) {
t.Fatal("should've returned no preferences")
}
}
func TestPreferenceCleanupFlagsBatch(t *testing.T) {
Setup()
category := model.PREFERENCE_CATEGORY_FLAGGED_POST
userId := model.NewId()
o1 := &model.Post{}
o1.ChannelId = model.NewId()
o1.UserId = userId
o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
o1.CreateAt = 1000
o1 = (<-store.Post().Save(o1)).Data.(*model.Post)
preference1 := model.Preference{
UserId: userId,
Category: category,
Name: o1.Id,
Value: "true",
}
preference2 := model.Preference{
UserId: userId,
Category: category,
Name: model.NewId(),
Value: "true",
}
Must(store.Preference().Save(&model.Preferences{preference1, preference2}))
result := <-store.Preference().CleanupFlagsBatch(10000)
assert.Nil(t, result.Err)
result = <-store.Preference().Get(userId, category, preference1.Name)
assert.Nil(t, result.Err)
result = <-store.Preference().Get(userId, category, preference2.Name)
assert.NotNil(t, result.Err)
}

View File

@@ -346,6 +346,7 @@ type PreferenceStore interface {
DeleteCategoryAndName(category string, name string) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel
IsFeatureEnabled(feature, userId string) StoreChannel
CleanupFlagsBatch(limit int64) StoreChannel
}
type LicenseStore interface {