Skip to content

Commit

Permalink
Don't fetch blocks that belong to deleted boards (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdelacroix authored May 26, 2022
1 parent acaccdf commit 7cc2ca5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
23 changes: 20 additions & 3 deletions server/services/store/sqlstore/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
78 changes: 78 additions & 0 deletions server/services/store/storetests/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 7cc2ca5

Please sign in to comment.