Skip to content

Commit

Permalink
Let the snapshot subquery reference the outer table (#94)
Browse files Browse the repository at this point in the history
* Let the snapshot subquery reference the outer table

* Add spec for loading multiple aggregates of which one us snapshotted

* Upgrade ruby
  • Loading branch information
derekkraan authored Sep 19, 2017
1 parent 11bcf9a commit 7c3a5e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.2
2.3.4
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ env:
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
- COVERAGE=true
rvm:
- 2.1.5
- 2.2.2
- 2.3.4
matrix:
exclude:
- rvm: 2.1.5
Expand Down
12 changes: 6 additions & 6 deletions lib/sequent/core/event_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ def load_events_for_aggregates(aggregate_ids)

events = event_record_class.connection.select_all(%Q{
SELECT event_type, event_json
FROM #{quote_table_name event_record_class.table_name}
FROM #{quote_table_name event_record_class.table_name} AS o
WHERE aggregate_id in (#{aggregate_ids.map{ |aggregate_id| quote(aggregate_id)}.join(",")})
AND sequence_number >= COALESCE((SELECT MAX(sequence_number)
FROM #{quote_table_name event_record_class.table_name}
WHERE event_type = #{quote snapshot_event_class.name}
AND aggregate_id in (#{aggregate_ids.map{ |aggregate_id| quote(aggregate_id)}.join(",")})), 0)
ORDER BY sequence_number ASC, (CASE event_type WHEN #{quote snapshot_event_class.name} THEN 0 ELSE 1 END) ASC
AND sequence_number >= COALESCE((SELECT MAX(sequence_number)
FROM #{quote_table_name event_record_class.table_name} AS i
WHERE event_type = #{quote snapshot_event_class.name}
AND i.aggregate_id = o.aggregate_id), 0)
ORDER BY sequence_number ASC, (CASE event_type WHEN #{quote snapshot_event_class.name} THEN 0 ELSE 1 END) ASC
}).map! do |event_hash|
deserialize_event(event_hash)
end
Expand Down
29 changes: 28 additions & 1 deletion spec/lib/sequent/core/aggregate_snapshotter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class MyAggregate < Sequent::Core::AggregateRoot; end
Sequent::configuration.command_handlers = commands_handlers
end
end
let(:snapshot_threshold) { 1 }
let(:events) { [MyEvent.new(aggregate_id: aggregate_id, sequence_number: 1)] }

before :each do
Sequent::configuration.command_handlers << described_class.new
Expand All @@ -26,7 +28,7 @@ class MyAggregate < Sequent::Core::AggregateRoot; end
[
[
Sequent::Core::EventStream.new(aggregate_type: 'MyAggregate', aggregate_id: aggregate_id, snapshot_threshold: 1),
[MyEvent.new(aggregate_id: aggregate_id, sequence_number: 1)]
events
]
]
)
Expand All @@ -37,4 +39,29 @@ class MyAggregate < Sequent::Core::AggregateRoot; end

expect(Sequent::Core::EventRecord.last.event_type).to eq Sequent::Core::SnapshotEvent.name
end

context 'loads aggregates with snapshots' do
let(:snapshot_threshold) { 2 }
let(:events) { [MyEvent.new(aggregate_id: aggregate_id, sequence_number: 1), MyEvent.new(aggregate_id: aggregate_id, sequence_number: 2), MyEvent.new(aggregate_id: aggregate_id, sequence_number: 3)] }

let(:aggregate_id_2) { Sequent.new_uuid }

before :each do
event_store.commit_events(
Sequent::Core::CommandRecord.new,
[
[
Sequent::Core::EventStream.new(aggregate_type: 'MyAggregate', aggregate_id: aggregate_id_2, snapshot_threshold: 10),
[MyEvent.new(aggregate_id: aggregate_id_2, sequence_number: 1)]
]
]
)

Sequent.command_service.execute_commands(*take_snapshot)
end

it 'loads both events' do
expect(event_store.load_events_for_aggregates([aggregate_id, aggregate_id_2])).to have(2).items
end
end
end

0 comments on commit 7c3a5e5

Please sign in to comment.