Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
DROP INDEX pins_batch;
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
CREATE INDEX pins_batch ON pins(batch_id);
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX pins_batch;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX pins_batch ON pins(batch_id);
25 changes: 13 additions & 12 deletions internal/database/sqlcommon/pin_sql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Kaleido, Inc.
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -141,24 +141,25 @@ func (s *SQLCommon) GetPins(ctx context.Context, filter database.Filter) (messag

}

func (s *SQLCommon) SetPinDispatched(ctx context.Context, sequence int64) (err error) {
func (s *SQLCommon) UpdatePins(ctx context.Context, filter database.Filter, update database.Update) (err error) {

ctx, tx, autoCommit, err := s.beginOrUseTx(ctx)
if err != nil {
return err
}
defer s.rollbackTx(ctx, tx, autoCommit)

_, err = s.updateTx(ctx, tx, sq.
Update("pins").
Set("dispatched", true).
Where(sq.Eq{
sequenceColumn: sequence,
}),
func() {
s.callbacks.OrderedCollectionEvent(database.CollectionPins, fftypes.ChangeEventTypeUpdated, sequence)
},
)
query, err := s.buildUpdate(sq.Update("pins"), update, pinFilterFieldMap)
if err != nil {
return err
}

query, err = s.filterUpdate(ctx, "", query, filter, pinFilterFieldMap)
if err != nil {
return err
}

_, err = s.updateTx(ctx, tx, query, nil /* no change events filter based update */)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this type does reliably emit ChangeEventTypeCreated. It theoretically emits ChangeEventTypeDeleted, although I don't know of anywhere we call DeletePin. It now never emits ChangeEventTypeUpdated though.

I wonder if having change events at all is useful, or if it's confusing to emit them inconsistently.

Copy link
Contributor Author

@peterbroadhurst peterbroadhurst Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is common across the codebase for these update-multiple style actions.

We don't have a need for the events where we use them currently, and it's hard in SQL without detailed DB-specific coding to find out what rows have been updated. There's also a performance overhead

if err != nil {
return err
}
Expand Down
31 changes: 25 additions & 6 deletions internal/database/sqlcommon/pin_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestPinsE2EWithDB(t *testing.T) {
}

s.callbacks.On("OrderedCollectionEvent", database.CollectionPins, fftypes.ChangeEventTypeCreated, mock.Anything).Return()
s.callbacks.On("OrderedCollectionEvent", database.CollectionPins, fftypes.ChangeEventTypeUpdated, mock.Anything).Return()
s.callbacks.On("OrderedCollectionEvent", database.CollectionPins, fftypes.ChangeEventTypeDeleted, mock.Anything).Return()

err := s.UpsertPin(ctx, pin)
Expand All @@ -67,7 +66,7 @@ func TestPinsE2EWithDB(t *testing.T) {
assert.Equal(t, int64(1), *res.TotalCount)

// Set it dispatched
err = s.SetPinDispatched(ctx, pin.Sequence)
err = s.UpdatePins(ctx, database.PinQueryFactory.NewFilter(ctx).Eq("sequence", pin.Sequence), database.PinQueryFactory.NewUpdate(ctx).Set("dispatched", true))
assert.NoError(t, err)

// Double insert, checking no error and we keep the dispatched flag
Expand Down Expand Up @@ -166,22 +165,42 @@ func TestGetPinReadMessageFail(t *testing.T) {
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestSetPinsDispatchedBeginFail(t *testing.T) {
func TestUpdatePinsBeginFail(t *testing.T) {
s, mock := newMockProvider().init()
mock.ExpectBegin().WillReturnError(fmt.Errorf("pop"))
err := s.SetPinDispatched(context.Background(), 12345)
ctx := context.Background()
err := s.UpdatePins(ctx, database.PinQueryFactory.NewFilter(ctx).Eq("sequence", 1), database.PinQueryFactory.NewUpdate(ctx).Set("dispatched", true))
assert.Regexp(t, "FF10114", err)
}

func TestSetPinsDispatchedUpdateFail(t *testing.T) {
func TestUpdatePinsUpdateFail(t *testing.T) {
s, mock := newMockProvider().init()
mock.ExpectBegin()
mock.ExpectExec("UPDATE .*").WillReturnError(fmt.Errorf("pop"))
mock.ExpectRollback()
err := s.SetPinDispatched(context.Background(), 12345)
ctx := context.Background()
err := s.UpdatePins(ctx, database.PinQueryFactory.NewFilter(ctx).Eq("sequence", 1), database.PinQueryFactory.NewUpdate(ctx).Set("dispatched", true))
assert.Regexp(t, "FF10117", err)
}

func TestUpdatePinsBadFilter(t *testing.T) {
s, mock := newMockProvider().init()
mock.ExpectBegin()
mock.ExpectRollback()
ctx := context.Background()
err := s.UpdatePins(ctx, database.PinQueryFactory.NewFilter(ctx).Eq("sequence", 1), database.PinQueryFactory.NewUpdate(ctx).Set("bad", true))
assert.Regexp(t, "FF10148", err)
}

func TestUpdatePinsBadUpdate(t *testing.T) {
s, mock := newMockProvider().init()
mock.ExpectBegin()
mock.ExpectRollback()
ctx := context.Background()
err := s.UpdatePins(ctx, database.PinQueryFactory.NewFilter(ctx).Eq("bad", 1), database.PinQueryFactory.NewUpdate(ctx).Set("dispatched", true))
assert.Regexp(t, "FF10148", err)
}

func TestPinDeleteBeginFail(t *testing.T) {
s, mock := newMockProvider().init()
mock.ExpectBegin().WillReturnError(fmt.Errorf("pop"))
Expand Down
Loading