From 7cc2ca56a5fa09d8109c05bc994781883322c2f0 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 26 May 2022 15:44:28 +0200 Subject: [PATCH] Don't fetch blocks that belong to deleted boards (#3139) --- server/services/store/sqlstore/blocks.go | 23 ++++++- server/services/store/storetests/blocks.go | 78 ++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/server/services/store/sqlstore/blocks.go b/server/services/store/sqlstore/blocks.go index 5fadeab02f9..c0cc2f61c5d 100644 --- a/server/services/store/sqlstore/blocks.go +++ b/server/services/store/sqlstore/blocks.go @@ -254,9 +254,26 @@ func (s *SQLStore) getSubTree3(db sq.BaseRunner, c store.Container, blockID stri func (s *SQLStore) getAllBlocks(db sq.BaseRunner, c store.Container) ([]model.Block, error) { query := s.getQueryBuilder(db). - Select(s.blockFields()...). - From(s.tablePrefix + "blocks"). - Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) + Select( + "b.id", + "b.parent_id", + "b.root_id", + "b.created_by", + "b.modified_by", + "b."+s.escapeField("schema"), + "b.type", + "b.title", + "b.fields", + s.timestampToCharField("b.insert_at", "insertAt"), + "b.create_at", + "b.update_at", + "b.delete_at", + "COALESCE(b.workspace_id, '0')", + ). + From(s.tablePrefix + "blocks as b"). + Join(s.tablePrefix + "blocks as pb on b.root_id = pb.id"). + Where(sq.Eq{"coalesce(b.workspace_id, '0')": c.WorkspaceID}). + Where(sq.Eq{"pb.delete_at": 0}) rows, err := query.Query() if err != nil { diff --git a/server/services/store/storetests/blocks.go b/server/services/store/storetests/blocks.go index 096c92e36c7..8b581d548f2 100644 --- a/server/services/store/storetests/blocks.go +++ b/server/services/store/storetests/blocks.go @@ -72,6 +72,11 @@ func StoreTestBlocksStore(t *testing.T, setup func(t *testing.T) (store.Store, f defer tearDown() testGetBlocks(t, store, container) }) + t.Run("GetAllBlocks", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testGetAllBlocks(t, store, container) + }) t.Run("GetBlock", func(t *testing.T) { store, tearDown := setup(t) defer tearDown() @@ -864,6 +869,79 @@ func testGetBlocks(t *testing.T, storeInstance store.Store, container store.Cont }) } +func testGetAllBlocks(t *testing.T, store store.Store, container store.Container) { + t.Run("getting all blocks should not include those from deleted boards", func(t *testing.T) { + newBlocks := []model.Block{ + { + ID: "board1", + Type: model.TypeBoard, + ParentID: "board1", + RootID: "board1", + ModifiedBy: "user-id", + }, + { + ID: "card1", + Type: model.TypeCard, + ParentID: "board1", + RootID: "board1", + ModifiedBy: "user-id", + }, + { + ID: "text1", + Type: model.TypeCard, + ParentID: "card1", + RootID: "board1", + ModifiedBy: "user-id", + }, + { + ID: "board2", + Type: model.TypeBoard, + ParentID: "board2", + RootID: "board2", + ModifiedBy: "user-id", + }, + { + ID: "card2", + Type: model.TypeCard, + ParentID: "board2", + RootID: "board2", + ModifiedBy: "user-id", + }, + { + ID: "text2", + Type: model.TypeCard, + ParentID: "card2", + RootID: "board2", + ModifiedBy: "user-id", + }, + } + + InsertBlocks(t, store, container, newBlocks, "user-id") + + t.Run("should return all blocks", func(t *testing.T) { + blocks, err := store.GetAllBlocks(container) + require.NoError(t, err) + require.Len(t, blocks, 6) + }) + + t.Run("after deleting a board, should only return the other one", func(t *testing.T) { + require.NoError(t, store.DeleteBlock(container, "board1", "user-id")) + + blocks, err := store.GetAllBlocks(container) + require.NoError(t, err) + require.Len(t, blocks, 3) + + expectedIDs := []string{"board2", "card2", "text2"} + + blockIDs := []string{} + for _, block := range blocks { + blockIDs = append(blockIDs, block.ID) + } + require.ElementsMatch(t, expectedIDs, blockIDs) + }) + }) +} + func testGetBlock(t *testing.T, store store.Store, container store.Container) { t.Run("get a block", func(t *testing.T) { block := model.Block{