Skip to content

Commit

Permalink
Merge pull request #15 from Sandthorn/snapshot_support
Browse files Browse the repository at this point in the history
Snapshot support by making it possible to get events after a specific aggregate_version
  • Loading branch information
hallgren authored Jun 12, 2018
2 parents 7c7bb7d + 10dbc2d commit 066b3c7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/sandthorn_driver_sequel/access/event_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ def store_events(aggregate, events)
aggregate.save
end

def find_events_by_aggregate_id(aggregate_id)
def find_events_by_aggregate_id(aggregate_id, after_aggregate_version = 0)
aggregate_version = Sequel.qualify(storage.events_table_name, :aggregate_version)
wrap(storage.events
.join(storage.aggregates_table_name, id: :aggregate_table_id)
.where(aggregate_id: aggregate_id)
.where(aggregate_version > after_aggregate_version)
.select(
:sequence_number,
:aggregate_id,
Expand Down
8 changes: 4 additions & 4 deletions lib/sandthorn_driver_sequel/event_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def all aggregate_type
end
end

def find aggregate_id, aggregate_type
aggregate_events(aggregate_id)
def find aggregate_id, aggregate_type, after_aggregate_version = 0
aggregate_events(aggregate_id, after_aggregate_version)
end

def get_events(*args)
Expand All @@ -49,10 +49,10 @@ def get_events(*args)

private

def aggregate_events(aggregate_id)
def aggregate_events(aggregate_id, after_aggregate_version = 0)
driver.execute do |db|
event_access = get_event_access(db)
event_access.find_events_by_aggregate_id(aggregate_id)
event_access.find_events_by_aggregate_id(aggregate_id, after_aggregate_version)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sandthorn_driver_sequel/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SandthornDriverSequel
VERSION = "4.0.0"
VERSION = "4.1.0"
end
32 changes: 32 additions & 0 deletions spec/event_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module SandthornDriverSequel
event_name: "foo",
event_data: "foo_data",
event_metadata: nil
},{
aggregate_version: 3,
event_name: "foo2",
event_data: "foo_data2",
event_metadata: nil
}
]
end
Expand Down Expand Up @@ -80,10 +85,37 @@ module SandthornDriverSequel
expect(stored_events.size).to eq(events.size)
expect(stored_events).to all(respond_to(:merge))
end

it "returns events in correct order" do
expect(stored_events.first[:aggregate_version] < stored_events.last[:aggregate_version]).to be_truthy
expect(stored_events.first[:sequence_number] < stored_events.last[:sequence_number]).to be_truthy
end

end

context "when using after_aggregate_version" do
before do
access.store_events(aggregate, events)
end

# exclude the first event
let(:stored_events) { access.find_events_by_aggregate_id(aggregate.aggregate_id, 1) }

it "returns correct events" do
expect(stored_events.map(&:aggregate_table_id)).to all(eq(aggregate.id))
expect(stored_events.size).to eq(events.size-1)
expect(stored_events).to all(respond_to(:merge))
end

it "returns events in correct order" do
expect(stored_events.first[:aggregate_version] < stored_events.last[:aggregate_version]).to be_truthy
expect(stored_events.first[:sequence_number] < stored_events.last[:sequence_number]).to be_truthy
end

it "should not return event with aggregate_version = 1" do
expect(stored_events.first[:aggregate_version]).not_to eq(1)
end

end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/saving_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module SandthornDriverSequel
expect(event[:event_name]).to eql("new")
expect(event[:aggregate_id]).to eql aggregate_id
expect(event[:aggregate_version]).to eql 1
expect(event[:sequence_number]).to be_a(Fixnum)
expect(event[:sequence_number]).to be_a(Integer)
expect(event[:timestamp]).to be_a(Time)
end
end
Expand Down Expand Up @@ -87,7 +87,7 @@ module SandthornDriverSequel
expect(event[:event_name]).to eql("new")
expect(event[:aggregate_id]).to eql aggregate_id
expect(event[:aggregate_version]).to eql 1
expect(event[:sequence_number]).to be_a(Fixnum)
expect(event[:sequence_number]).to be_a(Integer)
expect(event[:timestamp]).to be_a(Time)
end
end
Expand Down

0 comments on commit 066b3c7

Please sign in to comment.