diff --git a/db/schema.sql b/db/schema.sql index 5e2e727..1a7aa7d 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -32,21 +32,21 @@ DECLARE criteria jsonb[] := ARRAY ( SELECT jsonb_array_elements(COALESCE(append_condition -> 'fail_if_events_match', '[]'::jsonb))); - AFTER bigint := (append_condition ->> 'after')::bigint; BEGIN IF cardinality(criteria) > 0 AND EXISTS ( SELECT 1 FROM events AS e - WHERE (AFTER IS NULL OR e.position > AFTER) AND EXISTS ( - SELECT - 1 - FROM - unnest(criteria) AS c - WHERE (c -> 'types' IS NULL OR e.type IN ( - SELECT - jsonb_array_elements_text(c -> 'types'))) AND NOT EXISTS ( + WHERE + EXISTS ( + SELECT + 1 + FROM + unnest(criteria) AS c + WHERE ((c ->> 'after')::bigint IS NULL OR e.position > (c ->> 'after')::bigint) AND (c -> 'types' IS NULL OR e.type IN ( +SELECT + jsonb_array_elements_text(c -> 'types'))) AND NOT EXISTS ( SELECT 1 FROM diff --git a/lib/en57/event_store.rb b/lib/en57/event_store.rb index a5e07cf..b29238e 100644 --- a/lib/en57/event_store.rb +++ b/lib/en57/event_store.rb @@ -6,8 +6,8 @@ module En57 @repository = repository end - def append(events, fail_if: EmptyScope.new, after: nil) - @repository.append(events, fail_if: fail_if.to_query, after:) + def append(events, fail_if: EmptyScope.new) + @repository.append(events, fail_if: fail_if.to_query) self end diff --git a/lib/en57/pg_repository.rb b/lib/en57/pg_repository.rb index e55c8b2..9f1c65c 100644 --- a/lib/en57/pg_repository.rb +++ b/lib/en57/pg_repository.rb @@ -12,7 +12,7 @@ module En57 @array_decoder = PG::TextDecoder::Array.new end - def append(events, fail_if:, after:) + def append(events, fail_if:) event_records = events.map do |event| serialized, description = @serializer.dump(event.data) @@ -31,7 +31,6 @@ module En57 append_condition[ :fail_if_events_match ] = fail_if_events_match unless fail_if_events_match.empty? - append_condition[:after] = after unless after.nil? with_serializable_transaction do |connection| connection.exec_params( diff --git a/test/pg_regress/expected/001_schema.out b/test/pg_regress/expected/001_schema.out index f7025e6..50f4172 100644 --- a/test/pg_regress/expected/001_schema.out +++ b/test/pg_regress/expected/001_schema.out @@ -27,19 +27,18 @@ DECLARE criteria jsonb[] := ARRAY ( SELECT jsonb_array_elements(COALESCE(append_condition -> 'fail_if_events_match', '[]'::jsonb))); - AFTER bigint := (append_condition ->> 'after')::bigint; BEGIN IF cardinality(criteria) > 0 AND EXISTS ( SELECT 1 FROM events AS e - WHERE (AFTER IS NULL OR e.position > AFTER) AND EXISTS ( + WHERE EXISTS ( SELECT 1 FROM unnest(criteria) AS c - WHERE (c -> 'types' IS NULL OR e.type IN ( + WHERE ((c ->> 'after')::bigint IS NULL OR e.position > (c ->> 'after')::bigint) AND (c -> 'types' IS NULL OR e.type IN ( SELECT jsonb_array_elements_text(c -> 'types'))) AND NOT EXISTS ( SELECT diff --git a/test/test_event_store.rb b/test/test_event_store.rb index 1b833b8..7cef1b4 100644 --- a/test/test_event_store.rb +++ b/test/test_event_store.rb @@ -10,13 +10,7 @@ module En57 event = Event.new(type: "CreditsToppedUp") with_repository do |repository| - repository.expect( - :append, - nil, - [[event]], - fail_if: Query.all, - after: nil, - ) + repository.expect(:append, nil, [[event]], fail_if: Query.all) EventStore.new(repository).append([event]) end @@ -39,13 +33,7 @@ module En57 event = Event.new(type: "CreditsToppedUp") with_repository do |repository| - repository.expect( - :append, - nil, - [[event]], - fail_if: Query.all, - after: nil, - ) + repository.expect(:append, nil, [[event]], fail_if: Query.all) event_store = EventStore.new(repository) @@ -53,35 +41,13 @@ module En57 end end - def test_append_forwards_options - event = Event.new(type: "CreditsToppedUp") - - with_repository do |repository| - repository.expect( - :append, - nil, - [[event]], - fail_if: Query.all, - after: 42, - ) - - EventStore.new(repository).append([event], after: 42) - end - end - def test_append_accepts_scope_for_fail_if event = Event.new(type: "CreditsToppedUp") with_repository do |repository| event_store = EventStore.new(repository) fail_if = event_store.read.with_tag("order_id:123") - repository.expect( - :append, - nil, - [[event]], - fail_if: fail_if.to_query, - after: nil, - ) + repository.expect(:append, nil, [[event]], fail_if: fail_if.to_query) event_store.append([event], fail_if:) end @@ -95,7 +61,7 @@ module En57 Query.new( criteria: [Query::Criteria.new(types: ["OrderPlaced"], tags: [])], ) - repository.expect(:append, nil, [[event]], fail_if:, after: nil) + repository.expect(:append, nil, [[event]], fail_if:) EventStore.new(repository).append([event], fail_if:) end diff --git a/test/test_integration.rb b/test/test_integration.rb index db04b21..211b2bb 100644 --- a/test/test_integration.rb +++ b/test/test_integration.rb @@ -49,8 +49,7 @@ module En57 event_store.append([existing_event]) event_store.append( [Event.new(id: ids[1], type: "ShipmentScheduled")], - fail_if: event_store.read.of_type("OrderPlaced"), - after: 1, + fail_if: event_store.read.of_type("OrderPlaced").after(1), ) assert_equal( @@ -68,8 +67,7 @@ module En57 assert_raises(AppendConditionViolated) do event_store.append( [Event.new(id: ids[1], type: "ShipmentScheduled")], - fail_if: event_store.read.of_type("OrderPlaced"), - after: 0, + fail_if: event_store.read.of_type("OrderPlaced").after(0), ) end diff --git a/test/test_pg_repository.rb b/test/test_pg_repository.rb index 1e7a603..4646134 100644 --- a/test/test_pg_repository.rb +++ b/test/test_pg_repository.rb @@ -62,7 +62,6 @@ module En57 ), ], fail_if: Query.all, - after: nil, ) end end @@ -77,7 +76,7 @@ module En57 "SELECT append_events($1::event_with_tags[], $2::jsonb)", [ array_encoder.encode([]), - '{"fail_if_events_match":[{"types":["OrderPlaced"]}],"after":42}', + '{"fail_if_events_match":[{"types":["OrderPlaced"],"after":42}]}', ], ], ) @@ -87,9 +86,14 @@ module En57 [], fail_if: Query.new( - criteria: [Query::Criteria.new(types: ["OrderPlaced"], tags: [])], + criteria: [ + Query::Criteria.new( + types: ["OrderPlaced"], + tags: [], + after: 42, + ), + ], ), - after: 42, ) end end @@ -111,7 +115,6 @@ module En57 PgRepository.new(connection_uri, JsonSerializer.new).append( [], fail_if: Query.all, - after: nil, ) end end @@ -129,7 +132,6 @@ module En57 PgRepository.new(connection_uri, JsonSerializer.new).append( [], fail_if: Query.all, - after: nil, ) end end @@ -147,7 +149,6 @@ module En57 PgRepository.new(connection_uri, JsonSerializer.new).append( [], fail_if: Query.all, - after: nil, ) end end @@ -165,7 +166,6 @@ module En57 PgRepository.new(connection_uri, JsonSerializer.new).append( [], fail_if: Query.all, - after: nil, ) end end @@ -181,7 +181,6 @@ module En57 PgRepository.new(connection_uri, JsonSerializer.new).append( [], fail_if: Query.all, - after: nil, ) end end