Skip to content

Commit b264577

Browse files
authored
Merge pull request #74 from anyproto/GO-6504-payload-collection
GO-6504 fix payload deletion
2 parents 0ffd955 + 627993a commit b264577

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

db/db.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func (s *service) DeleteLog(ctx context.Context, logId string) (err error) {
154154
if res.DeletedCount == 0 {
155155
return consensuserr.ErrLogNotFound
156156
}
157-
res, err = s.payloadColl.DeleteMany(txCtx, bson.D{{"logId", logId}})
157+
regExpStr := "^" + regexp.QuoteMeta(logId) + "/+"
158+
res, err = s.payloadColl.DeleteMany(txCtx, bson.D{{"_id", bson.D{{"$regex", regExpStr}}}})
158159
if err != nil {
159160
return err
160161
}

db/db_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/anyproto/any-sync/consensus/consensusproto/consensuserr"
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
12+
"go.mongodb.org/mongo-driver/bson"
1213

1314
consensus "github.com/anyproto/any-sync-consensusnode"
1415
)
@@ -55,11 +56,22 @@ func TestService_DeleteLog(t *testing.T) {
5556
defer fx.Finish(t)
5657
log := consensus.Log{
5758
Id: "logOne",
59+
Records: []consensus.Record{
60+
{
61+
Id: "recordOne",
62+
PrevId: "",
63+
Payload: []byte("payload"),
64+
Created: time.Now().Truncate(time.Second).UTC(),
65+
},
66+
},
5867
}
5968
require.NoError(t, fx.AddLog(ctx, log))
6069
require.NoError(t, fx.DeleteLog(ctx, log.Id))
6170
_, err := fx.FetchLog(ctx, log.Id, "")
6271
require.EqualError(t, err, consensuserr.ErrLogNotFound.Error())
72+
count, err := fx.Service.(*service).payloadColl.CountDocuments(ctx, bson.D{{}})
73+
require.NoError(t, err)
74+
assert.Equal(t, int64(0), count)
6375
})
6476
t.Run("not found err", func(t *testing.T) {
6577
fx := newFixture(t, nil)

0 commit comments

Comments
 (0)