rails_event_store_active_record 0.21.0 → 0.22.0

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: 1d9b1b348afcdfb181fa21de7e02634bbefd6fb2
4
- data.tar.gz: 1f1c9e835e50c9968b9decec17a23e55546c8813
3
+ metadata.gz: 303421f1c02382bcb00a76cc09102dfca1a926dd
4
+ data.tar.gz: 329ae1361280c35255b69751c77b6fe3657affb2
5
5
  SHA512:
6
- metadata.gz: 4e1ab1cdb157e94a52d66132526904bebfa9db5eddd5f0e12afc551794698f90dcd26e70b7902107b8b1c69094556401dc06ee813522c67bdd4ee2e9f8f83833
7
- data.tar.gz: 8e39656125d3abf7e3c6e66c0bdb0ee19b3ce36fc00f0858c670b5fbc079ec181ea3b17ac47ddf24f4aa795dc9ef8325031f2a9aa803c17624f575cee77179bc
6
+ metadata.gz: e1b0b2289b35a8cdead875a2cc484bab23ab524ef94c6789553ce7815912f79918d5e666c2fecd9efebec84a9130561ddf586d02ce5f6059df75ffd62d7c54ac
7
+ data.tar.gz: 4f11f781d3ed17d87d7ff32f759a396116c40cabbf70528a39364567f883df3bd0434078b4a9ce80cb00236f4e691fdd27a8e6dd4492a2cbea9d645018f36462
data/Makefile CHANGED
@@ -15,7 +15,7 @@ mutate: test ## Run mutation tests
15
15
  @bundle exec mutant --include lib --require ruby_event_store --use rspec "RailsEventStoreActiveRecord*" \
16
16
  --ignore-subject "RailsEventStoreActiveRecord::EventRepository#normalize_to_array" \
17
17
  --ignore-subject "RailsEventStoreActiveRecord::LegacyEventRepository#normalize_to_array" \
18
- --ignore-subject "RailsEventStoreActiveRecord::EventRepository#detect_pkey_index_violated"
18
+ --ignore-subject "RailsEventStoreActiveRecord::EventRepository#detect_index_violated"
19
19
 
20
20
  build:
21
21
  @gem build -V rails_event_store_active_record.gemspec
@@ -12,48 +12,19 @@ module RailsEventStoreActiveRecord
12
12
  end
13
13
 
14
14
  def append_to_stream(events, stream_name, expected_version)
15
- raise RubyEventStore::InvalidExpectedVersion if stream_name.eql?(RubyEventStore::GLOBAL_STREAM) && !expected_version.equal?(:any)
16
-
17
- events = normalize_to_array(events)
18
- expected_version =
19
- case expected_version
20
- when Integer, :any
21
- expected_version
22
- when :none
23
- -1
24
- when :auto
25
- eis = EventInStream.where(stream: stream_name).order("position DESC").first
26
- (eis && eis.position) || -1
27
- else
28
- raise RubyEventStore::InvalidExpectedVersion
29
- end
15
+ add_to_stream(events, stream_name, expected_version, true) do |event|
16
+ build_event_record(event).save!
17
+ event.event_id
18
+ end
19
+ end
30
20
 
31
- ActiveRecord::Base.transaction(requires_new: true) do
32
- in_stream = events.flat_map.with_index do |event, index|
33
- position = unless expected_version.equal?(:any)
34
- expected_version + index + POSITION_SHIFT
35
- end
36
- build_event_record(event).save!
37
- events = [{
38
- stream: RubyEventStore::GLOBAL_STREAM,
39
- position: nil,
40
- event_id: event.event_id
41
- }]
42
- events.unshift({
43
- stream: stream_name,
44
- position: position,
45
- event_id: event.event_id
46
- }) unless stream_name.eql?(RubyEventStore::GLOBAL_STREAM)
47
- events
48
- end
49
- EventInStream.import(in_stream)
21
+ def link_to_stream(event_ids, stream_name, expected_version)
22
+ (normalize_to_array(event_ids) - Event.where(id: event_ids).pluck(:id)).each do |id|
23
+ raise RubyEventStore::EventNotFound.new(id)
50
24
  end
51
- self
52
- rescue ActiveRecord::RecordNotUnique => e
53
- if detect_pkey_index_violated(e)
54
- raise RubyEventStore::EventDuplicatedInStream
25
+ add_to_stream(event_ids, stream_name, expected_version, nil) do |event_id|
26
+ event_id
55
27
  end
56
- raise RubyEventStore::WrongExpectedEventVersion
57
28
  end
58
29
 
59
30
  def delete_stream(stream_name)
@@ -133,7 +104,7 @@ module RailsEventStoreActiveRecord
133
104
  )
134
105
  mapper.serialized_record_to_event(serialized_record)
135
106
  rescue ActiveRecord::RecordNotFound
136
- raise RubyEventStore::EventNotFound
107
+ raise RubyEventStore::EventNotFound.new(event_id)
137
108
  end
138
109
 
139
110
  def get_all_streams
@@ -146,10 +117,80 @@ module RailsEventStoreActiveRecord
146
117
 
147
118
  attr_reader :mapper
148
119
 
149
- def detect_pkey_index_violated(e)
150
- e.message.include?("for key 'PRIMARY'") || # MySQL
151
- e.message.include?("event_store_events_pkey") || # Postgresql
152
- e.message.include?("event_store_events.id") # Sqlite3
120
+ def add_to_stream(collection, stream_name, expected_version, include_global, &to_event_id)
121
+ raise RubyEventStore::InvalidExpectedVersion if stream_name.eql?(RubyEventStore::GLOBAL_STREAM) && !expected_version.equal?(:any)
122
+
123
+ collection = normalize_to_array(collection)
124
+ expected_version = normalize_expected_version(expected_version, stream_name)
125
+
126
+ ActiveRecord::Base.transaction(requires_new: true) do
127
+ in_stream = collection.flat_map.with_index do |element, index|
128
+ position = compute_position(expected_version, index)
129
+ event_id = to_event_id.call(element)
130
+ collection = []
131
+ collection.unshift({
132
+ stream: RubyEventStore::GLOBAL_STREAM,
133
+ position: nil,
134
+ event_id: event_id
135
+ }) if include_global
136
+ collection.unshift({
137
+ stream: stream_name,
138
+ position: position,
139
+ event_id: event_id
140
+ }) unless stream_name.eql?(RubyEventStore::GLOBAL_STREAM)
141
+ collection
142
+ end
143
+ EventInStream.import(in_stream)
144
+ end
145
+ self
146
+ rescue ActiveRecord::RecordNotUnique => e
147
+ raise_error(e)
148
+ end
149
+
150
+ def raise_error(e)
151
+ if detect_index_violated(e)
152
+ raise RubyEventStore::EventDuplicatedInStream
153
+ end
154
+ raise RubyEventStore::WrongExpectedEventVersion
155
+ end
156
+
157
+ def compute_position(expected_version, index)
158
+ unless expected_version.equal?(:any)
159
+ expected_version + index + POSITION_SHIFT
160
+ end
161
+ end
162
+
163
+ def normalize_expected_version(expected_version, stream_name)
164
+ case expected_version
165
+ when Integer, :any
166
+ expected_version
167
+ when :none
168
+ -1
169
+ when :auto
170
+ eis = EventInStream.where(stream: stream_name).order("position DESC").first
171
+ (eis && eis.position) || -1
172
+ else
173
+ raise RubyEventStore::InvalidExpectedVersion
174
+ end
175
+ end
176
+
177
+ MYSQL_PKEY_ERROR = "for key 'PRIMARY'"
178
+ POSTGRES_PKEY_ERROR = "event_store_events_pkey"
179
+ SQLITE3_PKEY_ERROR = "event_store_events.id"
180
+
181
+ MYSQL_INDEX_ERROR = "for key 'index_event_store_events_in_streams_on_stream_and_event_id'"
182
+ POSTGRES_INDEX_ERROR = "Key (stream, event_id)"
183
+ SQLITE3_INDEX_ERROR = "event_store_events_in_streams.stream, event_store_events_in_streams.event_id"
184
+
185
+ def detect_index_violated(e)
186
+ m = e.message
187
+ m.include?(MYSQL_PKEY_ERROR) ||
188
+ m.include?(POSTGRES_PKEY_ERROR) ||
189
+ m.include?(SQLITE3_PKEY_ERROR) ||
190
+
191
+ m.include?(MYSQL_INDEX_ERROR) ||
192
+ m.include?(POSTGRES_INDEX_ERROR) ||
193
+ m.include?(SQLITE3_INDEX_ERROR)
153
194
  end
154
195
 
155
196
  def build_event_record(event)
@@ -46,6 +46,10 @@ instead:
46
46
  raise RubyEventStore::EventDuplicatedInStream
47
47
  end
48
48
 
49
+ def link_to_stream(_event_ids, _stream_name, _expected_version)
50
+ raise RubyEventStore::NotSupported
51
+ end
52
+
49
53
  def delete_stream(stream_name)
50
54
  LegacyEvent.where({stream: stream_name}).update_all(stream: RubyEventStore::GLOBAL_STREAM)
51
55
  end
@@ -113,7 +117,7 @@ instead:
113
117
  end
114
118
 
115
119
  def read_event(event_id)
116
- build_event_entity(LegacyEvent.find_by(event_id: event_id)) or raise RubyEventStore::EventNotFound
120
+ build_event_entity(LegacyEvent.find_by(event_id: event_id)) or raise RubyEventStore::EventNotFound.new(event_id)
117
121
  end
118
122
 
119
123
  def get_all_streams
@@ -1,3 +1,3 @@
1
1
  module RailsEventStoreActiveRecord
2
- VERSION = "0.21.0"
2
+ VERSION = "0.22.0"
3
3
  end
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'fakefs', '~> 0.11.2'
31
31
  spec.add_development_dependency 'childprocess'
32
32
 
33
- spec.add_dependency 'ruby_event_store', '= 0.21.0'
33
+ spec.add_dependency 'ruby_event_store', '= 0.22.0'
34
34
  spec.add_dependency 'activesupport', '>= 3.0'
35
35
  spec.add_dependency 'activemodel', '>= 3.0'
36
36
  spec.add_dependency 'activerecord-import', '~> 0.21'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_event_store_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-22 00:00:00.000000000 Z
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - '='
158
158
  - !ruby/object:Gem::Version
159
- version: 0.21.0
159
+ version: 0.22.0
160
160
  type: :runtime
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - '='
165
165
  - !ruby/object:Gem::Version
166
- version: 0.21.0
166
+ version: 0.22.0
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: activesupport
169
169
  requirement: !ruby/object:Gem::Requirement
@@ -250,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
250
  version: '0'
251
251
  requirements: []
252
252
  rubyforge_project:
253
- rubygems_version: 2.6.11
253
+ rubygems_version: 2.6.13
254
254
  signing_key:
255
255
  specification_version: 4
256
256
  summary: Active Record events repository for Rails Event Store