From be2f5871aeba9d5a75049054f72946d7f6316b09 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Fri, 21 Jul 2023 23:03:59 +0300 Subject: [PATCH] Adds a database migration to restore the fileinfos that are deleted (#4815) (#4818) (cherry picked from commit 257cc5f1fd167cc791dd482a30b872da9743bff2) Co-authored-by: Miguel de la Cruz --- .../000040_fix_fileinfo_soft_deletes.down.sql | 1 + .../000040_fix_fileinfo_soft_deletes.up.sql | 8 ++++ .../fixtures/test40FixFileinfoSoftDeletes.sql | 9 ++++ .../migrationstests/migration40_test.go | 48 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.down.sql create mode 100644 server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.up.sql create mode 100644 server/services/store/sqlstore/migrationstests/fixtures/test40FixFileinfoSoftDeletes.sql create mode 100644 server/services/store/sqlstore/migrationstests/migration40_test.go diff --git a/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.down.sql b/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.down.sql new file mode 100644 index 000000000..e0ac49d1e --- /dev/null +++ b/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.down.sql @@ -0,0 +1 @@ +SELECT 1; diff --git a/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.up.sql b/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.up.sql new file mode 100644 index 000000000..156ca4edc --- /dev/null +++ b/server/services/store/sqlstore/migrations/000040_fix_fileinfo_soft_deletes.up.sql @@ -0,0 +1,8 @@ +{{if .plugin}} + UPDATE FileInfo + SET DeleteAt = 0 + WHERE CreatorId = 'boards' + AND DeleteAt != 0; +{{else}} +SELECT 1; +{{end}} diff --git a/server/services/store/sqlstore/migrationstests/fixtures/test40FixFileinfoSoftDeletes.sql b/server/services/store/sqlstore/migrationstests/fixtures/test40FixFileinfoSoftDeletes.sql new file mode 100644 index 000000000..6abc8f145 --- /dev/null +++ b/server/services/store/sqlstore/migrationstests/fixtures/test40FixFileinfoSoftDeletes.sql @@ -0,0 +1,9 @@ +INSERT INTO FileInfo +(Id, CreatorId, CreateAt, UpdateAt, DeleteAt) +VALUES +('fileinfo-1', 'user-id', 1, 1, 1000), +('fileinfo-2', 'user-id', 1, 1, 1000), +('fileinfo-3', 'user-id', 1, 1, 0), +('fileinfo-4', 'boards', 1, 1, 2000), +('fileinfo-5', 'boards', 1, 1, 2000), +('fileinfo-6', 'boards', 1, 1, 0); diff --git a/server/services/store/sqlstore/migrationstests/migration40_test.go b/server/services/store/sqlstore/migrationstests/migration40_test.go new file mode 100644 index 000000000..66ce1faea --- /dev/null +++ b/server/services/store/sqlstore/migrationstests/migration40_test.go @@ -0,0 +1,48 @@ +package migrationstests + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test40FixFileinfoSoftDeletes(t *testing.T) { + th, tearDown := SetupPluginTestHelper(t) + defer tearDown() + + th.f.MigrateToStep(39). + ExecFile("./fixtures/test40FixFileinfoSoftDeletes.sql"). + MigrateToStep(40) + + type FileInfo struct { + Id string + DeleteAt int + } + + getFileInfo := func(t *testing.T, id string) FileInfo { + t.Helper() + fileInfo := FileInfo{} + + query := "SELECT id, deleteat FROM FileInfo WHERE id = $1" + if th.IsMySQL() { + query = "SELECT Id as id, DeleteAt as deleteat FROM FileInfo WHERE Id = ?" + } + + err := th.f.DB().Get(&fileInfo, query, id) + require.NoError(t, err) + + return fileInfo + } + + t.Run("the file infos that don't belong to boards will not be restored", func(t *testing.T) { + require.Equal(t, 1000, getFileInfo(t, "fileinfo-1").DeleteAt) + require.Equal(t, 1000, getFileInfo(t, "fileinfo-2").DeleteAt) + require.Empty(t, getFileInfo(t, "fileinfo-3").DeleteAt) + }) + + t.Run("the file infos that belong to boards should correctly be restored", func(t *testing.T) { + require.Empty(t, getFileInfo(t, "fileinfo-3").DeleteAt) + require.Empty(t, getFileInfo(t, "fileinfo-4").DeleteAt) + require.Empty(t, getFileInfo(t, "fileinfo-5").DeleteAt) + }) +}