sandthorn_driver_sequel 3.2.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -4
- data/lib/sandthorn_driver_sequel/access/aggregate_access.rb +0 -4
- data/lib/sandthorn_driver_sequel/access/event_access.rb +11 -15
- data/lib/sandthorn_driver_sequel/errors.rb +0 -14
- data/lib/sandthorn_driver_sequel/event_query.rb +3 -2
- data/lib/sandthorn_driver_sequel/event_store.rb +10 -98
- data/lib/sandthorn_driver_sequel/event_store_context.rb +3 -3
- data/lib/sandthorn_driver_sequel/migration.rb +20 -3
- data/lib/sandthorn_driver_sequel/storage.rb +0 -9
- data/lib/sandthorn_driver_sequel/version.rb +1 -1
- data/lib/sandthorn_driver_sequel/wrappers/event_wrapper.rb +1 -1
- data/lib/sandthorn_driver_sequel/wrappers.rb +0 -1
- data/lib/sandthorn_driver_sequel.rb +0 -26
- data/sandthorn_driver_sequel.gemspec +2 -3
- data/spec/aggregate_access_spec.rb +0 -10
- data/spec/benchmark_spec.rb +6 -35
- data/spec/configuration_spec.rb +0 -29
- data/spec/driver_interface_spec.rb +0 -7
- data/spec/event_access_spec.rb +4 -22
- data/spec/event_store_spec.rb +43 -73
- data/spec/event_store_with_context_spec.rb +5 -5
- data/spec/get_events_spec.rb +12 -47
- data/spec/migration_specifying_domain_spec.rb +6 -6
- data/spec/saving_events_spec.rb +22 -28
- data/spec/storage_spec.rb +1 -0
- metadata +8 -31
- data/lib/sandthorn_driver_sequel/access/snapshot_access.rb +0 -105
- data/lib/sandthorn_driver_sequel/wrappers/snapshot_wrapper.rb +0 -19
- data/spec/asking_for_aggregates_to_snapshot_spec.rb +0 -66
- data/spec/saving_snapshot_spec.rb +0 -69
- data/spec/snapshot_access_spec.rb +0 -110
@@ -1,19 +0,0 @@
|
|
1
|
-
module SandthornDriverSequel
|
2
|
-
class SnapshotWrapper < SimpleDelegator
|
3
|
-
def aggregate_version
|
4
|
-
self[:aggregate].aggregate_version
|
5
|
-
end
|
6
|
-
|
7
|
-
def data
|
8
|
-
self[:aggregate]
|
9
|
-
end
|
10
|
-
|
11
|
-
def snapshot_id
|
12
|
-
self[:snapshot_id]
|
13
|
-
end
|
14
|
-
|
15
|
-
def aggregate_table_id
|
16
|
-
self[:aggregate_table_id]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class Foo; end
|
4
|
-
class Bar; end
|
5
|
-
|
6
|
-
module SandthornDriverSequel
|
7
|
-
|
8
|
-
describe EventStore do
|
9
|
-
before(:each) { prepare_for_test }
|
10
|
-
let(:aggregate) { Struct::AggregateMock.new(aggregate_id, 11) }
|
11
|
-
let(:aggregate_id) { @id ||= UUIDTools::UUID.random_create.to_s }
|
12
|
-
context "when asking for aggregates to snapshot" do
|
13
|
-
let(:aggregates) {
|
14
|
-
[{id: "1", class_name: Foo}, {id: aggregate_id, class_name: Struct::AggregateMock},{id: "3", class_name: Foo}]}
|
15
|
-
|
16
|
-
before(:each) {save_test_events}
|
17
|
-
|
18
|
-
context "when asking for type 'Bar' and max event count 5" do
|
19
|
-
let(:needs_snapshot) { event_store.obsolete_snapshots aggregate_types: [Struct::AggregateMock], max_event_distance: 5 }
|
20
|
-
context "and no snapshots exist" do
|
21
|
-
it "should return that id 2 with class Struct::AggregateMock need to be snapshotted" do
|
22
|
-
expect(needs_snapshot.length).to eql 1
|
23
|
-
expect(needs_snapshot.first[:aggregate_id]).to eql aggregates[1][:id]
|
24
|
-
expect(needs_snapshot.first[:aggregate_type]).to eql "Struct::AggregateMock"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
context "and a recent snapshot exists" do
|
28
|
-
before(:each) do
|
29
|
-
snapshot_data = { event_data: "YO MAN", aggregate_version: 11 }
|
30
|
-
event_store.save_snapshot(aggregate)
|
31
|
-
end
|
32
|
-
it "should return nil" do
|
33
|
-
expect(needs_snapshot).to be_empty
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
def save_test_events
|
40
|
-
for_1 = event_generator count: 4, start_at: 1
|
41
|
-
for_2 = event_generator count: 3, start_at: 1
|
42
|
-
for_3 = event_generator count: 6, start_at: 1
|
43
|
-
for_2_2 = event_generator count: 10, start_at: 4
|
44
|
-
for_1_2 = event_generator count: 1, start_at: 5
|
45
|
-
save_events for_1, 0
|
46
|
-
save_events for_2, 1
|
47
|
-
save_events for_3, 2
|
48
|
-
save_events for_1_2, 0
|
49
|
-
save_events for_2_2, 1
|
50
|
-
end
|
51
|
-
def save_events events, aggregate_index
|
52
|
-
event_store.save_events events, aggregates[aggregate_index][:id], aggregates[aggregate_index][:class_name]
|
53
|
-
end
|
54
|
-
|
55
|
-
def event_generator count: 1, start_at: 1
|
56
|
-
events = []
|
57
|
-
i = 0
|
58
|
-
while i < count do
|
59
|
-
events << { aggregate_version: i+start_at, event_args: nil, event_name: "event_foo_#{i}" }
|
60
|
-
i += 1
|
61
|
-
end
|
62
|
-
events
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
module SandthornDriverSequel
|
5
|
-
describe EventStore do
|
6
|
-
before(:each) { prepare_for_test }
|
7
|
-
let(:aggregate_id) { @id ||= UUIDTools::UUID.random_create.to_s }
|
8
|
-
let(:test_events) { [{aggregate_version: 1, event_args: nil, event_name: "new"},{aggregate_version: 2, event_args: nil, event_name: "foo"}] }
|
9
|
-
let(:additional_events) { [{aggregate_version: 3, event_args: nil, event_name: "klopp"},{aggregate_version: 4, event_args: nil, event_name: "flipp"}] }
|
10
|
-
let(:aggregate) { Struct::AggregateMock.new aggregate_id, 2 }
|
11
|
-
let(:save_snapshot) { event_store.save_snapshot aggregate }
|
12
|
-
let(:save_events) { event_store.save_events test_events, aggregate_id, Struct::AggregateMock }
|
13
|
-
let(:save_additional_events) { event_store.save_events additional_events, aggregate_id, Struct::AggregateMock }
|
14
|
-
context "when loading an aggregate using get_aggregate" do
|
15
|
-
context "and it has a snapshot" do
|
16
|
-
before(:each) do
|
17
|
-
save_events
|
18
|
-
save_snapshot
|
19
|
-
save_additional_events
|
20
|
-
end
|
21
|
-
let(:events) { event_store.get_aggregate aggregate_id, Struct::AggregateMock }
|
22
|
-
it "should have the first event as :aggregate_set_from_snapshot" do
|
23
|
-
expect(events.first[:aggregate]).to eql aggregate
|
24
|
-
end
|
25
|
-
it "should have additional events after first snapshot-event" do
|
26
|
-
expect(events.length).to eql 1+additional_events.length
|
27
|
-
expect(events[1][:aggregate_version]).to eql additional_events[0][:aggregate_version]
|
28
|
-
expect(events.last[:aggregate_version]).to eql additional_events.last[:aggregate_version]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
context "when saving a snapshot" do
|
34
|
-
|
35
|
-
context "and events are saved beforehand" do
|
36
|
-
before(:each) { save_events }
|
37
|
-
it "should be able to save snapshot" do
|
38
|
-
expect { save_snapshot }.to_not raise_error
|
39
|
-
end
|
40
|
-
it "should be able to save and get snapshot" do
|
41
|
-
save_snapshot
|
42
|
-
snap = event_store.get_snapshot(aggregate_id)
|
43
|
-
expect(snap).to eql aggregate
|
44
|
-
end
|
45
|
-
end
|
46
|
-
context "when trying to save a snapshot on a non-existing aggregate" do
|
47
|
-
it "should raise a NonAggregateError" do
|
48
|
-
expect { save_snapshot }.to raise_error SandthornDriverSequel::Errors::NoAggregateError
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context "when saving a snapshot twice" do
|
53
|
-
before(:each) { save_events; save_snapshot }
|
54
|
-
it "should not raise error" do
|
55
|
-
expect { save_snapshot }.to_not raise_error
|
56
|
-
end
|
57
|
-
end
|
58
|
-
context "when saving a snapshot on a version less than current version" do
|
59
|
-
before(:each) { save_events; }
|
60
|
-
it "should save without protesting" do
|
61
|
-
data = Struct::AggregateMock.new aggregate_id, 1
|
62
|
-
event_store.save_snapshot(data)
|
63
|
-
snap = event_store.get_snapshot(aggregate_id)
|
64
|
-
expect(snap).to eql data
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module SandthornDriverSequel
|
4
|
-
describe SnapshotAccess do
|
5
|
-
include EventStoreContext
|
6
|
-
|
7
|
-
before do
|
8
|
-
prepare_for_test
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:context) { :test }
|
12
|
-
let(:db) { Sequel.connect(event_store_url)}
|
13
|
-
let(:aggregate_id) { generate_uuid }
|
14
|
-
let(:storage) { Storage.new(db, :test) }
|
15
|
-
let(:event_serializer) { -> (data) { YAML.dump(data) } }
|
16
|
-
let(:event_deserializer) { -> (data) { YAML.load(data) } }
|
17
|
-
let(:snapshot_serializer) { -> (data) { YAML.dump(data) } }
|
18
|
-
let(:snapshot_deserializer) { -> (data) { YAML.load(data) } }
|
19
|
-
let(:aggregate_access) { AggregateAccess.new(storage) }
|
20
|
-
let(:event_access) { EventAccess.new(storage, event_serializer, event_deserializer) }
|
21
|
-
let(:aggregate) { aggregate_access.register_aggregate(aggregate_id, "foo") }
|
22
|
-
let(:access) { SnapshotAccess.new(storage, snapshot_serializer, snapshot_deserializer) }
|
23
|
-
let(:events) do
|
24
|
-
[
|
25
|
-
{
|
26
|
-
aggregate_version: 1,
|
27
|
-
event_name: "new",
|
28
|
-
event_args: "new_data"
|
29
|
-
},{
|
30
|
-
aggregate_version: 2,
|
31
|
-
event_name: "foo",
|
32
|
-
event_args: "foo_data"
|
33
|
-
}
|
34
|
-
]
|
35
|
-
end
|
36
|
-
let(:snapshot_aggregate) {
|
37
|
-
aggregate #register the aggregate
|
38
|
-
Struct::AggregateMock.new aggregate_id, 0
|
39
|
-
}
|
40
|
-
|
41
|
-
let(:snapshot_aggregate_2) {
|
42
|
-
Struct::AggregateMock.new aggregate_id, 2
|
43
|
-
}
|
44
|
-
|
45
|
-
describe "#find_by_aggregate_id" do
|
46
|
-
it "returns the correct data" do
|
47
|
-
access.record_snapshot(snapshot_aggregate)
|
48
|
-
aggregate.update(aggregate_version: 1)
|
49
|
-
snapshot = access.find_by_aggregate_id(aggregate.aggregate_id)
|
50
|
-
expect(snapshot.data).to eql snapshot_aggregate
|
51
|
-
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe "#record" do
|
56
|
-
context "when the aggregate doesn't exist" do
|
57
|
-
it "raises an error" do
|
58
|
-
agg = Struct::AggregateMock.new "qux", 1
|
59
|
-
expect { access.record_snapshot(agg) }.to raise_error(Errors::NoAggregateError)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
context "when the aggregate exists" do
|
63
|
-
context "when no previous snapshot exists" do
|
64
|
-
it "records the snapshot" do
|
65
|
-
|
66
|
-
expect(access.find_by_aggregate_id(aggregate_id)).to be_nil
|
67
|
-
access.record_snapshot(snapshot_aggregate)
|
68
|
-
|
69
|
-
snapshot = access.find_by_aggregate_id(aggregate_id)
|
70
|
-
expect(snapshot).to_not be_nil
|
71
|
-
expect(snapshot.data).to eq(snapshot_aggregate)
|
72
|
-
expect(snapshot.data.aggregate_version).to eq(0)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
context "when the snapshot isn't fresh" do
|
76
|
-
context "when the versions match" do
|
77
|
-
it "records a new snapshot" do
|
78
|
-
expect(access.find_by_aggregate_id(aggregate_id)).to be_nil
|
79
|
-
access.record_snapshot(snapshot_aggregate)
|
80
|
-
event_access.store_events(aggregate, events)
|
81
|
-
access.record_snapshot(snapshot_aggregate_2)
|
82
|
-
|
83
|
-
snapshot = access.find_by_aggregate_id(aggregate_id)
|
84
|
-
expect(snapshot).to_not be_nil
|
85
|
-
expect(snapshot.data).to eq(snapshot_aggregate_2)
|
86
|
-
expect(snapshot.data.aggregate_version).to eq(2)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
it "can write and read snapshots" do
|
96
|
-
|
97
|
-
snapshot_id = access.record_snapshot(snapshot_aggregate)
|
98
|
-
snapshot = access.find(snapshot_id)
|
99
|
-
|
100
|
-
expect(snapshot).to_not be_nil
|
101
|
-
expect(snapshot.data).to eq(snapshot_aggregate)
|
102
|
-
expect(snapshot)
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
def generate_uuid
|
107
|
-
SecureRandom.uuid
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|