sandthorn_driver_sequel 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6df7059570ed2a75376a49b5568cde3dcda5abef
4
- data.tar.gz: d0df6fa0c71d2388f31897fcb4c1918ecbe6bdd2
3
+ metadata.gz: 3924bb5783466dc29b26e328fd109b286e72bed4
4
+ data.tar.gz: 464c30fdd02be1bbf86b60407aaa842a13719bae
5
5
  SHA512:
6
- metadata.gz: cff7f24db48fe2ec230c54526f3231fff0c2f7cd94ed12b585807cfa06084cba5a7c4d7d63b670816844ba8b5f6a3114a6d755bb0b16b6cb2e3336ea9c68236c
7
- data.tar.gz: 2592b3457a5c030517d968302aa4c716af3cd58fa3080c1378960a066a40fbf28000e963ab22d75c0fbf600acd3b4f75799ceea63d3a01f1c7d90d59927b5ea4
6
+ metadata.gz: 29ee3fe2c3e741503c089cae89b5fa8b6efd3680c0d58dcfb57e9a5301d80a7f191c5aee54194194321c4c41ac4230162b5976acde1de5f49998796c349b5557
7
+ data.tar.gz: 54c2d0a99c0a9258ffd08f8bc13d43f319a696e1b7a9f362d2b1b9a1fe9c6e069e94e44badf30d7d501ed1952f912eaac7424192585ba8a0b5ad243cf342c1bf
@@ -25,6 +25,7 @@ module SandthornDriverSequel
25
25
  :event_name,
26
26
  :event_data,
27
27
  :timestamp)
28
+ .order(:sequence_number)
28
29
  .all)
29
30
  end
30
31
 
@@ -7,19 +7,13 @@ module SandthornDriverSequel
7
7
  def build(
8
8
  aggregate_types: [],
9
9
  take: 0,
10
- after_sequence_number: 0,
11
- include_events: [],
12
- exclude_events: [])
10
+ after_sequence_number: 0)
13
11
 
14
12
  aggregate_types.map!(&:to_s)
15
- include_events.map!(&:to_s)
16
- exclude_events.map!(&:to_s)
17
13
 
18
14
  query = storage.events
19
15
  query = add_aggregate_types(query, aggregate_types)
20
16
  query = add_sequence_number(query, after_sequence_number)
21
- query = add_included_events(query, include_events)
22
- query = add_excluded_events(query, exclude_events)
23
17
  query = add_select(query)
24
18
  query = add_limit(query, take)
25
19
  @query = query.order(:sequence_number)
@@ -58,22 +52,6 @@ module SandthornDriverSequel
58
52
  ]
59
53
  end
60
54
 
61
- def add_excluded_events(query, exclude_events)
62
- if exclude_events.any?
63
- query.exclude(event_name: exclude_events)
64
- else
65
- query
66
- end
67
- end
68
-
69
- def add_included_events(query, include_events)
70
- if include_events.any?
71
- query.where(event_name: include_events)
72
- else
73
- query
74
- end
75
- end
76
-
77
55
  def add_sequence_number(query, after_sequence_number)
78
56
  query.where { sequence_number > after_sequence_number }
79
57
  end
@@ -1,3 +1,3 @@
1
1
  module SandthornDriverSequel
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -6,13 +6,15 @@ require 'sandthorn_driver_sequel/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "sandthorn_driver_sequel"
8
8
  spec.version = SandthornDriverSequel::VERSION
9
- spec.authors = ["Lars Krantz", "Morgan Hallgren"]
10
- spec.email = ["lars.krantz@alaz.se", "morgan.hallgren@gmail.com"]
11
- spec.description = %q{sequel driver for sandthorn}
12
- spec.summary = %q{sequel driver for sandthorn}
9
+ spec.authors = ["Lars Krantz", "Morgan Hallgren", "Jesper Josefsson"]
10
+ spec.email = ["lars.krantz@alaz.se", "morgan.hallgren@gmail.com", "jesper.josefsson@gmail.com"]
11
+ spec.description = %q{Sequel driver for Sandthorn}
12
+ spec.summary = %q{Sequel driver for Sandthorn}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
16
+ spec.required_ruby_version = ">= 2.0"
17
+
16
18
  spec.files = `git ls-files`.split($/)
17
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -66,14 +66,20 @@ module SandthornDriverSequel
66
66
 
67
67
  describe "#find_events_by_aggregate_id" do
68
68
  context "when there are events" do
69
- it "returns correct events" do
69
+ before do
70
70
  access.store_events(aggregate, events)
71
+ end
72
+ let(:stored_events) { access.find_events_by_aggregate_id(aggregate.aggregate_id) }
71
73
 
72
- stored_events = access.find_events_by_aggregate_id(aggregate.aggregate_id)
74
+ it "returns correct events" do
73
75
  expect(stored_events.map(&:aggregate_table_id)).to all(eq(aggregate.id))
74
76
  expect(stored_events.size).to eq(events.size)
75
77
  expect(stored_events).to all(respond_to(:merge))
76
78
  end
79
+ it "returns events in correct order" do
80
+ expect(stored_events.first[:aggregate_version] < stored_events.last[:aggregate_version]).to be_truthy
81
+ expect(stored_events.first[:sequence_number] < stored_events.last[:sequence_number]).to be_truthy
82
+ end
77
83
  end
78
84
  end
79
85
 
@@ -41,75 +41,46 @@ module SandthornDriverSequel
41
41
 
42
42
  context "when using get_events" do
43
43
  context "and using take" do
44
- let(:events) {event_store.get_events after_sequence_number: 0, include_events: [:new], take: 2}
44
+ let(:events) {event_store.get_events after_sequence_number: 0, take: 2}
45
45
  it "should find 2 events" do
46
- events.length.should eql 2
47
- end
48
- end
49
- context "and getting events of type :new" do
50
- let(:events) {event_store.get_events after_sequence_number: 0, include_events: [:new]}
51
- it "should find 3 events" do
52
- events.length.should eql 3
53
- end
54
- it "should only be new-events" do
55
- events.all? { |e| e[:event_name] == "new" }
46
+ expect(events.length).to eql 2
56
47
  end
57
48
  end
58
49
  context "and combining args" do
59
50
  let(:events) do
60
51
  all = event_store.get_events after_sequence_number: 0
61
52
  first_seq_number = all[0][:sequence_number]
62
- event_store.get_events after_sequence_number: first_seq_number , exclude_events: [:foo], include_events: [:new, "bar", :flubber], take: 100
63
- end
64
- it "should find 4 events" do
65
- events.length.should eql 4
66
- end
67
- it "should not be any foo-events" do
68
- events.all? { |e| e[:event_name] != "foo" }
69
- end
70
- end
71
- context "and getting all events but excluding new" do
72
- let(:events) {event_store.get_events after_sequence_number: 0, exclude_events: [:new] }
73
- it "should find 5 events" do
74
- events.length.should eql 5
75
- end
76
- it "should only be new and foo-events" do
77
- events.all? { |e| e[:event_name] != "new" }
78
- end
79
- end
80
- context "and getting events of type :new and foo" do
81
- let(:events) {event_store.get_events after_sequence_number: 0, aggregate_types: ["String", SandthornDriverSequel::EventStore], include_events: [:new, "foo"]}
82
- it "should find 3 events" do
83
- events.length.should eql 3
53
+ event_store.get_events after_sequence_number: first_seq_number, take: 100
84
54
  end
85
- it "should only be new and foo-events" do
86
- events.all? { |e| e[:event_name] == "new" || e[:event_name] == "foo" }
55
+ it "should find 7 events" do
56
+ expect(events.length).to eql 7
87
57
  end
58
+
88
59
  end
89
60
  context "and getting events for SandthornDriverSequel::EventStore, and String after 0" do
90
61
  let(:events) {event_store.get_events after_sequence_number: 0, aggregate_types: [SandthornDriverSequel::EventStore, String]}
91
62
  it "should find 5 events" do
92
- events.length.should eql 5
63
+ expect(events.length).to eql 5
93
64
  end
94
65
  it "should be in sequence_number order" do
95
66
  check = 0
96
- events.each { |e| e[:sequence_number].should be > check; check = e[:sequence_number] }
67
+ events.each { |e| expect(e[:sequence_number]).to be > check; check = e[:sequence_number] }
97
68
  end
98
69
  it "should contain only events for aggregate_id_a and aggregate_id_c" do
99
- events.each { |e| [aggregate_id_a, aggregate_id_c].include?(e[:aggregate_id]).should be_truthy }
70
+ events.each { |e| expect([aggregate_id_a, aggregate_id_c].include?(e[:aggregate_id])).to be_truthy }
100
71
  end
101
72
  end
102
73
  context "and getting events for SandthornDriverSequel::EventStore after 0" do
103
74
  let(:events) {event_store.get_events after_sequence_number: 0, aggregate_types: [SandthornDriverSequel::EventStore]}
104
75
  it "should find 3 events" do
105
- events.length.should eql 3
76
+ expect(events.length).to eql 3
106
77
  end
107
78
  it "should be in sequence_number order" do
108
79
  check = 0
109
- events.each { |e| e[:sequence_number].should be > check; check = e[:sequence_number] }
80
+ events.each { |e| expect(e[:sequence_number]).to be > check; check = e[:sequence_number] }
110
81
  end
111
82
  it "should contain only events for aggregate_id_a" do
112
- events.each { |e| e[:aggregate_id].should eql aggregate_id_a }
83
+ events.each { |e| expect(e[:aggregate_id]).to eql aggregate_id_a }
113
84
  end
114
85
  end
115
86
  end
@@ -117,38 +88,38 @@ module SandthornDriverSequel
117
88
  context "and getting events for SandthornDriverSequel::EventStore after 0" do
118
89
  let(:events) {event_store.get_new_events_after_event_id_matching_classname 0, SandthornDriverSequel::EventStore}
119
90
  it "should find 3 events" do
120
- events.length.should eql 3
91
+ expect(events.length).to eql 3
121
92
  end
122
93
  it "should be in sequence_number order" do
123
94
  check = 0
124
- events.each { |e| e[:sequence_number].should be > check; check = e[:sequence_number] }
95
+ events.each { |e| expect(e[:sequence_number]).to be > check; check = e[:sequence_number] }
125
96
  end
126
97
  it "should contain only events for aggregate_id_a" do
127
- events.each { |e| e[:aggregate_id].should eql aggregate_id_a }
98
+ events.each { |e| expect(e[:aggregate_id]).to eql aggregate_id_a }
128
99
  end
129
100
  it "should be able to get events after a sequence number" do
130
101
  new_from = events[1][:sequence_number]
131
102
  ev = event_store.get_new_events_after_event_id_matching_classname new_from, SandthornDriverSequel::EventStore
132
- ev.last[:aggregate_version].should eql 3
133
- ev.length.should eql 1
103
+ expect(ev.last[:aggregate_version]).to eql 3
104
+ expect(ev.length).to eql 1
134
105
  end
135
106
  it "should be able to limit the number of results" do
136
107
  ev = event_store.get_new_events_after_event_id_matching_classname 0, SandthornDriverSequel::EventStore, take: 2
137
- ev.length.should eql 2
138
- ev.last[:aggregate_version].should eql 2
108
+ expect(ev.length).to eql 2
109
+ expect(ev.last[:aggregate_version]).to eql 2
139
110
  end
140
111
  end
141
112
  context "and getting events for String after 0" do
142
113
  let(:events) {event_store.get_new_events_after_event_id_matching_classname 0, "String"}
143
114
  it "should find 3 events" do
144
- events.length.should eql 2
115
+ expect(events.length).to eql 2
145
116
  end
146
117
  it "should be in sequence_number order" do
147
118
  check = 0
148
- events.each { |e| e[:sequence_number].should be > check; check = e[:sequence_number] }
119
+ events.each { |e| expect(e[:sequence_number]).to be > check; check = e[:sequence_number] }
149
120
  end
150
121
  it "should contain only events for aggregate_id_c" do
151
- events.each { |e| e[:aggregate_id].should eql aggregate_id_c }
122
+ events.each { |e| expect(e[:aggregate_id]).to eql aggregate_id_c }
152
123
  end
153
124
  end
154
125
  end
@@ -17,19 +17,19 @@ module SandthornDriverSequel
17
17
  it "should be able to save and retrieve events on the aggregate" do
18
18
  event_store.save_events test_events, aggregate_id, String
19
19
  events = event_store.get_aggregate_events aggregate_id
20
- events.length.should eql test_events.length
20
+ expect(events.length).to eql test_events.length
21
21
  end
22
22
 
23
23
  it "should have correct keys when asking for events" do
24
24
  event_store.save_events test_events, aggregate_id, String
25
25
  events = event_store.get_aggregate aggregate_id, String
26
26
  event = events.first
27
- event[:event_data].should eql(test_events.first[:event_data])
28
- event[:event_name].should eql("new")
29
- event[:aggregate_id].should eql aggregate_id
30
- event[:aggregate_version].should eql 1
31
- event[:sequence_number].should be_a(Fixnum)
32
- event[:timestamp].should be_a(Time)
27
+ expect(event[:event_data]).to eql(test_events.first[:event_data])
28
+ expect(event[:event_name]).to eql("new")
29
+ expect(event[:aggregate_id]).to eql aggregate_id
30
+ expect(event[:aggregate_version]).to eql 1
31
+ expect(event[:sequence_number]).to be_a(Fixnum)
32
+ expect(event[:timestamp]).to be_a(Time)
33
33
  end
34
34
  end
35
35
 
@@ -56,16 +56,16 @@ module SandthornDriverSequel
56
56
 
57
57
  it "both types should exist in get_all_typenames in alphabetical order" do
58
58
  names = event_store.get_all_types
59
- names.length.should eql 2
60
- names.first.should eql "Hash"
61
- names.last.should eql "String"
59
+ expect(names.length).to eql 2
60
+ expect(names.first).to eql "Hash"
61
+ expect(names.last).to eql "String"
62
62
  end
63
63
 
64
64
  it "should list the aggregate ids when asking for get_aggregate_list_by_typename" do
65
65
  ids = event_store.get_aggregate_ids(aggregate_type: String)
66
- ids.length.should eql 2
67
- ids.any? { |e| e == aggregate_id_1 }.should be_truthy
68
- ids.any? { |e| e == aggregate_id_3 }.should be_truthy
66
+ expect(ids.length).to eql 2
67
+ expect(ids.any? { |e| e == aggregate_id_1 }).to be_truthy
68
+ expect(ids.any? { |e| e == aggregate_id_3 }).to be_truthy
69
69
  end
70
70
  end
71
71
 
@@ -40,7 +40,7 @@ module SandthornDriverSequel
40
40
  it "should be able to save and get snapshot" do
41
41
  save_snapshot
42
42
  snap = event_store.get_snapshot(aggregate_id)
43
- snap.should eql snapshot_data
43
+ expect(snap).to eql snapshot_data
44
44
  end
45
45
  end
46
46
  context "when trying to save a snapshot on a non-existing aggregate" do
@@ -69,7 +69,7 @@ module SandthornDriverSequel
69
69
  data[:aggregate_version] = 1
70
70
  event_store.save_snapshot(data, aggregate_id)
71
71
  snap = event_store.get_snapshot(aggregate_id)
72
- snap.should eql data
72
+ expect(snap).to eql data
73
73
  end
74
74
  end
75
75
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sandthorn_driver_sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Krantz
8
8
  - Morgan Hallgren
9
+ - Jesper Josefsson
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2015-03-09 00:00:00.000000000 Z
13
+ date: 2015-04-01 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bundler
@@ -235,10 +236,11 @@ dependencies:
235
236
  - - ">="
236
237
  - !ruby/object:Gem::Version
237
238
  version: '0'
238
- description: sequel driver for sandthorn
239
+ description: Sequel driver for Sandthorn
239
240
  email:
240
241
  - lars.krantz@alaz.se
241
242
  - morgan.hallgren@gmail.com
243
+ - jesper.josefsson@gmail.com
242
244
  executables: []
243
245
  extensions: []
244
246
  extra_rdoc_files: []
@@ -264,7 +266,6 @@ files:
264
266
  - lib/sandthorn_driver_sequel/event_store.rb
265
267
  - lib/sandthorn_driver_sequel/event_store_context.rb
266
268
  - lib/sandthorn_driver_sequel/migration.rb
267
- - lib/sandthorn_driver_sequel/old_event_store.rb
268
269
  - lib/sandthorn_driver_sequel/sequel_driver.rb
269
270
  - lib/sandthorn_driver_sequel/storage.rb
270
271
  - lib/sandthorn_driver_sequel/utilities.rb
@@ -300,7 +301,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
300
301
  requirements:
301
302
  - - ">="
302
303
  - !ruby/object:Gem::Version
303
- version: '0'
304
+ version: '2.0'
304
305
  required_rubygems_version: !ruby/object:Gem::Requirement
305
306
  requirements:
306
307
  - - ">="
@@ -311,7 +312,7 @@ rubyforge_project:
311
312
  rubygems_version: 2.4.3
312
313
  signing_key:
313
314
  specification_version: 4
314
- summary: sequel driver for sandthorn
315
+ summary: Sequel driver for Sandthorn
315
316
  test_files:
316
317
  - spec/aggregate_access_spec.rb
317
318
  - spec/asking_for_aggregates_to_snapshot_spec.rb
@@ -1,228 +0,0 @@
1
- require 'sandthorn_driver_sequel/sequel_driver'
2
-
3
- module SandthornDriverSequel
4
- class OldEventStore
5
- include EventStoreContext
6
- attr_reader :driver, :context, :url
7
- def initialize url: nil, context: nil
8
- @driver = SequelDriver.new url: url
9
- @context = context
10
- @url = url
11
- end
12
-
13
- def save_events aggregate_events, originating_aggregate_version, aggregate_id, class_name
14
- current_aggregate_version = originating_aggregate_version
15
- aggregate_type = class_name.to_s
16
- driver.execute_in_transaction do |db|
17
- if current_aggregate_version == 0
18
- pk_id = register_new_aggregate(aggregate_id, aggregate_type, db)
19
- else
20
- current_aggregate = get_current_aggregate_from_aggregates_table(aggregate_id, aggregate_type, db)
21
- check_initial_aggregate_version!(current_aggregate, current_aggregate_version)
22
- pk_id = current_aggregate[:id]
23
- end
24
- timestamp = Time.now.utc
25
- aggregate_events.each do |event|
26
- current_aggregate_version += 1
27
- check_event_aggregate_version!(event, class_name, current_aggregate_version)
28
- insert_event(db, event, pk_id, timestamp)
29
- end
30
- db[aggregates_table_name].where(id: pk_id).update(aggregate_version: current_aggregate_version)
31
- end
32
- end
33
-
34
- def insert_event(db, event, pk_id, timestamp)
35
- to_insert = {
36
- aggregate_table_id: pk_id,
37
- aggregate_version: event[:aggregate_version],
38
- event_name: event[:event_name],
39
- event_data: event[:event_data],
40
- timestamp: timestamp
41
- }
42
- db[events_table_name].insert(to_insert)
43
- end
44
-
45
- def check_event_aggregate_version!(event, aggregate_type, current_aggregate_version)
46
- if event[:aggregate_version] != current_aggregate_version
47
- raise SandthornDriverSequel::Errors::ConcurrencyError, event, aggregate_type, current_aggregate_version
48
- end
49
- end
50
-
51
- def check_initial_aggregate_version!(aggregate, current_aggregate_version)
52
- if aggregate[:aggregate_version] != current_aggregate_version
53
- raise SandthornDriverSequel::Errors::WrongAggregateVersionError, aggregate, current_aggregate_version
54
- end
55
- end
56
-
57
- def register_new_aggregate(aggregate_id, aggregate_type, db)
58
- to_insert = {
59
- aggregate_id: aggregate_id,
60
- aggregate_type: aggregate_type,
61
- aggregate_version: 0
62
- }
63
- pk_id = db[aggregates_table_name].insert(to_insert)
64
- end
65
-
66
- def save_snapshot aggregate_snapshot, aggregate_id, class_name
67
- driver.execute_in_transaction do |db|
68
- current_aggregate = get_current_aggregate_from_aggregates_table aggregate_id, class_name, db
69
- pk_id = current_aggregate[:id]
70
- current_snapshot = get_current_snapshot pk_id, db
71
- aggregate_version = aggregate_snapshot[:aggregate_version]
72
- return if snapshot_fresh?(current_snapshot, aggregate_version)
73
- check_snapshot_version!(current_aggregate, aggregate_version)
74
- if current_snapshot.nil?
75
- to_insert = {aggregate_version: aggregate_version, snapshot_data: aggregate_snapshot[:event_data], aggregate_table_id: pk_id }
76
- db[snapshots_table_name].insert(to_insert)
77
- else
78
- to_update = {aggregate_version: aggregate_version, snapshot_data: aggregate_snapshot[:event_data] }
79
- db[snapshots_table_name].where(aggregate_table_id: pk_id).update(to_update)
80
- end
81
- end
82
- end
83
-
84
- def snapshot_fresh?(current_snapshot, aggregate_version)
85
- !current_snapshot.nil? && current_snapshot[:aggregate_version] == aggregate_version
86
- end
87
-
88
- def check_snapshot_version!(aggregate, aggregate_version)
89
- if aggregate[:aggregate_version] < aggregate_version
90
- raise SandthornDriverSequel::Errors::WrongSnapshotVersionError, aggregate, version
91
- end
92
- end
93
-
94
- def get_aggregate_events aggregate_id, *class_name
95
- #aggregate_type = class_name.to_s unless class_name.nil?
96
- return aggregate_events aggregate_id: aggregate_id
97
- end
98
-
99
- def get_aggregate aggregate_id, *class_name
100
- snapshot = get_snapshot aggregate_id, class_name
101
- after_aggregate_version = 0
102
- after_aggregate_version = snapshot[:aggregate_version] unless snapshot.nil?
103
- events = aggregate_events after_aggregate_version: after_aggregate_version, aggregate_id: aggregate_id
104
- unless snapshot.nil?
105
- snap_event = snapshot
106
- snap_event[:event_name] = "aggregate_set_from_snapshot"
107
- events = events.unshift(snap_event)
108
- end
109
- events
110
- end
111
- def get_aggregate_list_by_typename class_name
112
- aggregate_type = class_name.to_s
113
- driver.execute do |db|
114
- db[aggregates_table_name].where(aggregate_type: aggregate_type).select(:aggregate_id).map { |e| e[:aggregate_id] }
115
- end
116
- end
117
-
118
- def get_all_typenames
119
- driver.execute do |db|
120
- db[aggregates_table_name].select(:aggregate_type).distinct.order(:aggregate_type).map{|e| e[:aggregate_type]}
121
- end
122
- end
123
-
124
- def get_snapshot aggregate_id, *class_name
125
- aggregate_type = class_name.first.to_s
126
- driver.execute do |db|
127
- current_aggregate = get_current_aggregate_from_aggregates_table aggregate_id, aggregate_type, db
128
- snap = get_current_snapshot current_aggregate[:id], db
129
- return nil if snap.nil?
130
- return {aggregate_version: snap[:aggregate_version], event_data: snap[:snapshot_data]}
131
- end
132
- end
133
-
134
- def get_new_events_after_event_id_matching_classname event_id, class_name, args = {}
135
- take = args.fetch(:take, 0)
136
- aggregate_type = class_name.to_s
137
- driver.execute do |db|
138
- query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_type: aggregate_type)
139
- query = query.where{sequence_number > event_id}
140
- rel = "#{events_table_name}__aggregate_version".to_sym
141
- query = query.select(:aggregate_type, rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp)
142
- query = query.limit(take) if take > 0
143
- return query.order(:sequence_number).all
144
- end
145
- end
146
- def get_events aggregate_types: [], take: 0, after_sequence_number: 0, include_events: [], exclude_events: []
147
- include_events = include_events.map { |e| e.to_s }
148
- exclude_events = exclude_events.map { |e| e.to_s }
149
- aggregate_types = aggregate_types.map { |e| e.to_s }
150
- driver.execute do |db|
151
- if aggregate_types.empty?
152
- query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id)
153
- else
154
- query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_type: aggregate_types)
155
- end
156
- query = query.where{sequence_number > after_sequence_number}
157
- unless include_events.empty?
158
- query = query.where(event_name: include_events)
159
- end
160
- unless exclude_events.empty?
161
- query = query.exclude(event_name: exclude_events)
162
- end
163
- rel = "#{events_table_name}__aggregate_version".to_sym
164
- query = query.select(:aggregate_type, rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp)
165
- query = query.limit(take) if take > 0
166
- return query.order(:sequence_number).all
167
- end
168
- end
169
- def obsolete_snapshots aggregate_types: [], max_event_distance: 100
170
- driver.execute do |db|
171
- rel = "#{snapshots_table_name}__aggregate_version".to_sym
172
- aggr_rel = "#{aggregates_table_name}__aggregate_version".to_sym
173
- query_select = eval("lambda{(#{aggr_rel} - coalesce(#{rel},0)).as(distance)}")
174
- query = db[aggregates_table_name].left_outer_join(snapshots_table_name, aggregate_table_id: :id)
175
- query = query.select &query_select
176
- query = query.select_append(:aggregate_id, :aggregate_type)
177
- query_where = eval("lambda{(#{aggr_rel} - coalesce(#{rel},0)) > max_event_distance}")
178
- query = query.where &query_where
179
- unless class_names.empty?
180
- class_names.map! {|c|c.to_s}
181
- query = query.where(aggregate_type: class_names)
182
- end
183
- query.all
184
- end
185
- end
186
- private
187
-
188
- def aggregate_events after_aggregate_version: 0, aggregate_id: nil
189
-
190
- rel = "#{events_table_name}__aggregate_version".to_sym
191
- where_proc = eval("lambda{ #{rel} > after_aggregate_version }")
192
- driver.execute do |db|
193
- query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_id: aggregate_id)
194
- query = query.where &where_proc
195
- result = query.select(rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp).order(:sequence_number).all
196
- end
197
-
198
- # result = nil
199
- # Benchmark.bm do |x|
200
- # x.report("find") {
201
- # rel = "#{events_table_name}__aggregate_version".to_sym
202
- # where_proc = eval("lambda{ #{rel} > after_aggregate_version }")
203
- # driver.execute do |db|
204
- # query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_id: aggregate_id)
205
- # query = query.where &where_proc
206
- # result = query.select(rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp).order(:sequence_number).all
207
- # end
208
- # }
209
-
210
- # end
211
- # result
212
- end
213
- def get_current_aggregate_from_aggregates_table aggregate_id, aggregate_type, db
214
- aggregate_type = aggregate_type.to_s
215
- current_aggregate = db[aggregates_table_name].where(aggregate_id: aggregate_id)
216
- if current_aggregate.empty?
217
- error_message = "#{aggregate_type} with id #{aggregate_id} was not found in the eventstore."
218
- raise SandthornDriverSequel::Errors::NoAggregateError.new(error_message)
219
- end
220
- current_aggregate.first
221
- end
222
- def get_current_snapshot aggregate_table_id, db
223
- snap = db[snapshots_table_name].where(aggregate_table_id: aggregate_table_id)
224
- return nil if snap.empty?
225
- snap.first
226
- end
227
- end
228
- end