rails_event_store_active_record 0.27.1 → 0.28.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
- SHA1:
3
- metadata.gz: 8570ebd9e95de577529ef0411550572cb797773f
4
- data.tar.gz: d2cf335f4efcb6ce41c4971bc624b177a7245730
2
+ SHA256:
3
+ metadata.gz: 5a0d4acb7008aec0595bba6cbe82ee841e120bec2206aef067832a839d502cea
4
+ data.tar.gz: d3d3ee18ff599b7c3f85ccc826f68101f4bf3c82d021c3c5b994da48efeabda3
5
5
  SHA512:
6
- metadata.gz: 030c49e0c6c7821ca871b3edfef88d5772833773f0f59b58dd30449edb8ff6cbf1e7e54e5dd2c897e2818cc465db682f1e23dfdcf3852d0a112f321105c9dae0
7
- data.tar.gz: 2437f7f2f6fe7d372b45dbbbe8c4c8a396e323c097b131cc213f0aa5ca69ca8d79c179b43b71a5a092ba06eb02cae3c0e824897668fabb22ee15edb99c49aebf
6
+ metadata.gz: 3a70559fa37a6185c93d46ce8454bf3b9907e8ff204713ca7e7fcdfe8327c1913bed69f54df43750fc79f2a147ce3ba5a9d6b972753644999bba0ad4a9979941
7
+ data.tar.gz: 7a0575b7006dfc80b027c702b7843268bdd28d38c686c8f0d57ff6148d0f2c3d3f745cdaf72b93d046e3ae73268f98ae02e7a3b5ff97931b78f8668480719a30
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gem 'ruby_event_store', path: '../ruby_event_store'
6
6
  if v = ENV['RAILS_VERSION']
7
7
  gem 'rails', v
8
8
  end
9
+ gem 'pry'
data/Makefile CHANGED
@@ -12,20 +12,29 @@ install: ## Install gem dependencies
12
12
  @BUNDLE_GEMFILE=spec/v1_v2_schema_migration/Gemfile bundle install
13
13
  @BUNDLE_GEMFILE=spec/without_rails/Gemfile bundle install
14
14
 
15
+ remove-lock:
16
+ @echo "Removing resolved dependency versions"
17
+ -rm Gemfile.lock
18
+ -rm spec/v1_v2_schema_migration/Gemfile.lock
19
+ -rm spec/without_rails/Gemfile.lock
20
+
21
+ reinstall: remove-lock install ## Removing resolved dependency versions
22
+
23
+
15
24
  test: ## Run unit tests
16
25
  @echo "Running unit tests"
17
- @bundle exec rspec
26
+ @BUNDLE_GEMFILE=Gemfile bundle exec rspec
18
27
 
19
28
  mutate: test ## Run mutation tests
20
29
  @echo "Running mutation tests"
21
- @DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
30
+ @BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
22
31
  $(addprefix --require ,$(REQUIRE)) \
23
32
  $(addprefix --ignore-subject ,$(IGNORE)) \
24
33
  --use rspec "$(SUBJECT)"
25
34
 
26
35
  mutate-fast: ## Run mutation tests with --fail-fast
27
36
  @echo "Running mutation tests with --fail-fast"
28
- @DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
37
+ @BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
29
38
  $(addprefix --require ,$(REQUIRE)) \
30
39
  $(addprefix --ignore-subject ,$(IGNORE)) \
31
40
  --fail-fast \
@@ -39,7 +48,7 @@ build:
39
48
 
40
49
  push:
41
50
  @echo "Pushing package to RubyGems"
42
- @gem push pkg/$(GEM_NAME)-$(GEM_VERSION).gem
51
+ @gem push -k dev_arkency pkg/$(GEM_NAME)-$(GEM_VERSION).gem
43
52
 
44
53
  clean:
45
54
  @echo "Removing previously built package"
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # RailsEventStoreActiveRecord
2
2
 
3
- An Active Record based implementation of events repository for [Rails Event Store](https://github.com/RailsEventStore/rails_event_store).
3
+ An Active Record based implementation of events repository for [Rails Event Store](https://github.com/RailsEventStore/rails_event_store).
4
4
 
5
5
  This is the default repository used in `rails_event_store` gem.
@@ -5,5 +5,4 @@ require 'rails_event_store_active_record/event_repository'
5
5
  require 'rails_event_store_active_record/event_repository_reader'
6
6
  require 'rails_event_store_active_record/correct_schema_verifier'
7
7
  require 'rails_event_store_active_record/index_violation_detector'
8
- require 'rails_event_store_active_record/legacy_event_repository'
9
8
  require 'rails_event_store_active_record/version'
@@ -50,10 +50,14 @@ This migration must be run offline -- take that into consideration:
50
50
 
51
51
 
52
52
  If you cannot migrate right now -- you can for some time continue using
53
- old repository. In order to do so, change configuration accordingly:
53
+ old repository. In order to do so:
54
+ 1. install 'rails_event_store_active_record-legacy' gem
55
+ 2. change configuration accordingly:
56
+
57
+ require 'rails_event_store_active_record/legacy'
54
58
 
55
59
  config.event_store = RailsEventStore::Client.new(
56
- repository: RailsEventStoreActiveRecord::LegacyEventRepository.new
60
+ repository: RailsEventStoreActiveRecord::Legacy::EventRepository.new
57
61
  )
58
62
 
59
63
 
@@ -2,103 +2,74 @@ require 'activerecord-import'
2
2
 
3
3
  module RailsEventStoreActiveRecord
4
4
  class EventRepository
5
-
6
5
  POSITION_SHIFT = 1
6
+ SERIALIZED_GLOBAL_STREAM_NAME = "all".freeze
7
7
 
8
- def initialize(mapper: RubyEventStore::Mappers::Default.new)
8
+ def initialize
9
9
  verify_correct_schema_present
10
- @mapper = mapper
11
- @repo_reader = EventRepositoryReader.new(mapper)
10
+ @repo_reader = EventRepositoryReader.new
12
11
  end
13
12
 
14
- def append_to_stream(events, stream_name, expected_version)
15
- add_to_stream(normalize_to_array(events), stream_name, expected_version, true) do |event|
13
+ def append_to_stream(events, stream, expected_version)
14
+ add_to_stream(normalize_to_array(events), stream, expected_version, true) do |event|
16
15
  build_event_record(event).save!
17
16
  event.event_id
18
17
  end
19
18
  end
20
19
 
21
- def link_to_stream(event_ids, stream_name, expected_version)
20
+ def link_to_stream(event_ids, stream, expected_version)
22
21
  (normalize_to_array(event_ids) - Event.where(id: event_ids).pluck(:id)).each do |id|
23
22
  raise RubyEventStore::EventNotFound.new(id)
24
23
  end
25
- add_to_stream(normalize_to_array(event_ids), stream_name, expected_version, nil) do |event_id|
24
+ add_to_stream(normalize_to_array(event_ids), stream, expected_version, nil) do |event_id|
26
25
  event_id
27
26
  end
28
27
  end
29
28
 
30
- def delete_stream(stream_name)
31
- EventInStream.where(stream: stream_name).delete_all
29
+ def delete_stream(stream)
30
+ EventInStream.where(stream: stream.name).delete_all
32
31
  end
33
32
 
34
33
  def has_event?(event_id)
35
34
  @repo_reader.has_event?(event_id)
36
35
  end
37
36
 
38
- def last_stream_event(stream_name)
39
- @repo_reader.last_stream_event(stream_name)
40
- end
41
-
42
- def read_events_forward(stream_name, after_event_id, count)
43
- @repo_reader.read_events_forward(stream_name, after_event_id, count)
44
- end
45
-
46
- def read_events_backward(stream_name, before_event_id, count)
47
- @repo_reader.read_events_backward(stream_name, before_event_id, count)
48
- end
49
-
50
- def read_stream_events_forward(stream_name)
51
- @repo_reader.read_stream_events_forward(stream_name)
52
- end
53
-
54
- def read_stream_events_backward(stream_name)
55
- @repo_reader.read_stream_events_backward(stream_name)
56
- end
57
-
58
- def read_all_streams_forward(after_event_id, count)
59
- @repo_reader.read_all_streams_forward(after_event_id, count)
60
- end
61
-
62
- def read_all_streams_backward(before_event_id, count)
63
- @repo_reader.read_all_streams_backward(before_event_id, count)
37
+ def last_stream_event(stream)
38
+ @repo_reader.last_stream_event(stream)
64
39
  end
65
40
 
66
41
  def read_event(event_id)
67
42
  @repo_reader.read_event(event_id)
68
43
  end
69
44
 
70
- def get_all_streams
71
- @repo_reader.get_all_streams
72
- end
73
-
74
- def add_metadata(event, key, value)
75
- @mapper.add_metadata(event, key, value)
45
+ def read(specification)
46
+ @repo_reader.read(specification)
76
47
  end
77
48
 
78
49
  private
79
50
 
80
- def add_to_stream(collection, stream_name, expected_version, include_global, &to_event_id)
81
- raise RubyEventStore::InvalidExpectedVersion if stream_name.eql?(RubyEventStore::GLOBAL_STREAM) && !expected_version.equal?(:any)
82
-
83
- expected_version = normalize_expected_version(expected_version, stream_name)
51
+ def add_to_stream(collection, stream, expected_version, include_global, &to_event_id)
52
+ last_stream_version = ->(stream_) { EventInStream.where(stream: stream_.name).order("position DESC").first.try(:position) }
53
+ resolved_version = expected_version.resolve_for(stream, last_stream_version)
84
54
 
85
55
  ActiveRecord::Base.transaction(requires_new: true) do
86
56
  in_stream = collection.flat_map.with_index do |element, index|
87
- position = compute_position(expected_version, index)
57
+ position = compute_position(resolved_version, index)
88
58
  event_id = to_event_id.call(element)
89
59
  collection = []
90
60
  collection.unshift({
91
- stream: RubyEventStore::GLOBAL_STREAM,
61
+ stream: SERIALIZED_GLOBAL_STREAM_NAME,
92
62
  position: nil,
93
- event_id: event_id
63
+ event_id: event_id,
94
64
  }) if include_global
95
65
  collection.unshift({
96
- stream: stream_name,
66
+ stream: stream.name,
97
67
  position: position,
98
68
  event_id: event_id
99
- }) unless stream_name.eql?(RubyEventStore::GLOBAL_STREAM)
69
+ }) unless stream.global?
100
70
  collection
101
71
  end
72
+ fill_ids(in_stream)
102
73
  EventInStream.import(in_stream)
103
74
  end
104
75
  self
@@ -113,23 +84,9 @@ module RailsEventStoreActiveRecord
113
84
  raise RubyEventStore::WrongExpectedEventVersion
114
85
  end
115
86
 
116
- def compute_position(expected_version, index)
117
- unless expected_version.equal?(:any)
118
- expected_version + index + POSITION_SHIFT
119
- end
120
- end
121
-
122
- def normalize_expected_version(expected_version, stream_name)
123
- case expected_version
124
- when Integer, :any
125
- expected_version
126
- when :none
127
- -1
128
- when :auto
129
- eis = EventInStream.where(stream: stream_name).order("position DESC").first
130
- (eis && eis.position) || -1
131
- else
132
- raise RubyEventStore::InvalidExpectedVersion
87
+ def compute_position(resolved_version, index)
88
+ unless resolved_version.nil?
89
+ resolved_version + index + POSITION_SHIFT
133
90
  end
134
91
  end
135
92
 
@@ -137,8 +94,7 @@ module RailsEventStoreActiveRecord
137
94
  IndexViolationDetector.new.detect(message)
138
95
  end
139
96
 
140
- def build_event_record(event)
141
- serialized_record = @mapper.event_to_serialized_record(event)
97
+ def build_event_record(serialized_record)
142
98
  Event.new(
143
99
  id: serialized_record.event_id,
144
100
  data: serialized_record.data,
@@ -155,6 +111,10 @@ module RailsEventStoreActiveRecord
155
111
  def verify_correct_schema_present
156
112
  CorrectSchemaVerifier.new.verify
157
113
  end
114
+
115
+ # Overwritten in a sub-class
116
+ def fill_ids(_in_stream)
117
+ end
158
118
  end
159
119
 
160
120
  end
@@ -1,102 +1,67 @@
1
1
  module RailsEventStoreActiveRecord
2
2
  class EventRepositoryReader
3
3
 
4
- def initialize(mapper)
5
- @mapper = mapper
6
- end
7
-
8
4
  def has_event?(event_id)
9
5
  Event.exists?(id: event_id)
10
6
  end
11
7
 
12
- def last_stream_event(stream_name)
13
- record = EventInStream.where(stream: stream_name).order('position DESC, id DESC').first
8
+ def last_stream_event(stream)
9
+ record = EventInStream.where(stream: stream.name).order('position DESC, id DESC').first
14
10
  record && build_event_instance(record)
15
11
  end
16
12
 
17
- def read_events_forward(stream_name, after_event_id, count)
18
- stream = EventInStream.where(stream: stream_name)
19
- unless after_event_id.equal?(:head)
20
- after_event = stream.find_by!(event_id: after_event_id)
21
- stream = stream.where('id > ?', after_event)
22
- end
23
-
24
- stream.preload(:event).order('position ASC, id ASC').limit(count)
25
- .map(&method(:build_event_instance))
13
+ def read_event(event_id)
14
+ event = Event.find(event_id)
15
+ RubyEventStore::SerializedRecord.new(
16
+ event_id: event.id,
17
+ metadata: event.metadata,
18
+ data: event.data,
19
+ event_type: event.event_type
20
+ )
21
+ rescue ActiveRecord::RecordNotFound
22
+ raise RubyEventStore::EventNotFound.new(event_id)
26
23
  end
27
24
 
28
- def read_events_backward(stream_name, before_event_id, count)
29
- stream = EventInStream.where(stream: stream_name)
30
- unless before_event_id.equal?(:head)
31
- before_event = stream.find_by!(event_id: before_event_id)
32
- stream = stream.where('id < ?', before_event)
33
- end
34
-
35
- stream.preload(:event).order('position DESC, id DESC').limit(count)
36
- .map(&method(:build_event_instance))
37
- end
25
+ def read(spec)
26
+ raise RubyEventStore::ReservedInternalName if spec.stream_name.eql?(EventRepository::SERIALIZED_GLOBAL_STREAM_NAME)
38
27
 
39
- def read_stream_events_forward(stream_name)
40
- EventInStream.preload(:event).where(stream: stream_name).order('position ASC, id ASC')
41
- .map(&method(:build_event_instance))
42
- end
28
+ stream = EventInStream.preload(:event).where(stream: normalize_stream_name(spec))
29
+ stream = stream.order(position: order(spec.direction)) unless spec.global_stream?
30
+ stream = stream.limit(spec.count) if spec.limit?
31
+ stream = stream.where(start_condition(spec)) unless spec.head?
32
+ stream = stream.order(id: order(spec.direction))
43
33
 
44
- def read_stream_events_backward(stream_name)
45
- EventInStream.preload(:event).where(stream: stream_name).order('position DESC, id DESC')
46
- .map(&method(:build_event_instance))
34
+ stream.map(&method(:build_event_instance)).each
47
35
  end
48
36
 
49
- def read_all_streams_forward(after_event_id, count)
50
- stream = EventInStream.where(stream: RubyEventStore::GLOBAL_STREAM)
51
- unless after_event_id.equal?(:head)
52
- after_event = stream.find_by!(event_id: after_event_id)
53
- stream = stream.where('id > ?', after_event)
54
- end
37
+ private
55
38
 
56
- stream.preload(:event).order('id ASC').limit(count)
57
- .map(&method(:build_event_instance))
39
+ def normalize_stream_name(specification)
40
+ specification.global_stream? ? EventRepository::SERIALIZED_GLOBAL_STREAM_NAME : specification.stream_name
58
41
  end
59
42
 
60
- def read_all_streams_backward(before_event_id, count)
61
- stream = EventInStream.where(stream: RubyEventStore::GLOBAL_STREAM)
62
- unless before_event_id.equal?(:head)
63
- before_event = stream.find_by!(event_id: before_event_id)
64
- stream = stream.where('id < ?', before_event)
43
+ def start_condition(specification)
44
+ event_record =
45
+ EventInStream.find_by!(event_id: specification.start, stream: normalize_stream_name(specification))
46
+ case specification.direction
47
+ when :forward
48
+ ['id > ?', event_record]
49
+ else
50
+ ['id < ?', event_record]
65
51
  end
66
-
67
- stream.preload(:event).order('id DESC').limit(count)
68
- .map(&method(:build_event_instance))
69
- end
70
-
71
- def read_event(event_id)
72
- event = Event.find(event_id)
73
- serialized_record = RubyEventStore::SerializedRecord.new(
74
- event_id: event.id,
75
- metadata: event.metadata,
76
- data: event.data,
77
- event_type: event.event_type
78
- )
79
- @mapper.serialized_record_to_event(serialized_record)
80
- rescue ActiveRecord::RecordNotFound
81
- raise RubyEventStore::EventNotFound.new(event_id)
82
52
  end
83
53
 
84
- def get_all_streams
85
- (["all"] + EventInStream.order(:id).pluck(:stream))
86
- .uniq
87
- .map { |name| RubyEventStore::Stream.new(name) }
54
+ def order(direction)
55
+ {forward: 'ASC', backward: 'DESC'}.fetch(direction)
88
56
  end
89
57
 
90
- private
91
-
92
58
  def build_event_instance(record)
93
- serialized_record = RubyEventStore::SerializedRecord.new(
94
- event_id: record.event.id,
95
- metadata: record.event.metadata,
96
- data: record.event.data,
59
+ RubyEventStore::SerializedRecord.new(
60
+ event_id: record.event.id,
61
+ metadata: record.event.metadata,
62
+ data: record.event.data,
97
63
  event_type: record.event.event_type
98
64
  )
99
- @mapper.serialized_record_to_event(serialized_record)
100
65
  end
101
66
  end
102
67
 
@@ -1,3 +1,3 @@
1
1
  module RailsEventStoreActiveRecord
2
- VERSION = "0.27.1"
2
+ VERSION = "0.28.0"
3
3
  end
@@ -11,8 +11,14 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['dev@arkency.com']
12
12
 
13
13
  spec.summary = %q{Active Record events repository for Rails Event Store}
14
- spec.description = %q{Implementation of events repository based on Rails Active Record for Rails Event Store'}
15
- spec.homepage = 'https://github.com/RailsEventStore/rails_event_store_active_record'
14
+ spec.description = %q{Implementation of events repository based on Rails Active Record for Rails Event Store}
15
+ spec.homepage = 'https://railseventstore.org'
16
+ spec.metadata = {
17
+ "homepage_uri" => "https://railseventstore.org/",
18
+ "changelog_uri" => "https://github.com/RailsEventStore/rails_event_store/releases",
19
+ "source_code_uri" => "https://github.com/RailsEventStore/rails_event_store",
20
+ "bug_tracker_uri" => "https://github.com/RailsEventStore/rails_event_store/issues",
21
+ }
16
22
 
17
23
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
24
  spec.bindir = 'exe'
@@ -22,7 +28,7 @@ Gem::Specification.new do |spec|
22
28
  spec.add_development_dependency 'bundler', '~> 1.15'
23
29
  spec.add_development_dependency 'rake', '~> 10.0'
24
30
  spec.add_development_dependency 'rspec', '~> 3.6'
25
- spec.add_development_dependency 'rails', '~> 5.1'
31
+ spec.add_development_dependency 'rails', '~> 5.2'
26
32
  spec.add_development_dependency 'sqlite3', '1.3.13'
27
33
  spec.add_development_dependency 'pg', '0.21'
28
34
  spec.add_development_dependency 'mysql2', '0.4.10'
@@ -31,7 +37,7 @@ Gem::Specification.new do |spec|
31
37
  spec.add_development_dependency 'childprocess'
32
38
  spec.add_development_dependency 'google-protobuf', '~> 3.5.1.2'
33
39
 
34
- spec.add_dependency 'ruby_event_store', '= 0.27.1'
40
+ spec.add_dependency 'ruby_event_store', '= 0.28.0'
35
41
  spec.add_dependency 'activesupport', '>= 3.0'
36
42
  spec.add_dependency 'activemodel', '>= 3.0'
37
43
  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.27.1
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-26 00:00:00.000000000 Z
11
+ date: 2018-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '5.1'
61
+ version: '5.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '5.1'
68
+ version: '5.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -170,14 +170,14 @@ dependencies:
170
170
  requirements:
171
171
  - - '='
172
172
  - !ruby/object:Gem::Version
173
- version: 0.27.1
173
+ version: 0.28.0
174
174
  type: :runtime
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - '='
179
179
  - !ruby/object:Gem::Version
180
- version: 0.27.1
180
+ version: 0.28.0
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: activesupport
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -221,7 +221,7 @@ dependencies:
221
221
  - !ruby/object:Gem::Version
222
222
  version: '0.21'
223
223
  description: Implementation of events repository based on Rails Active Record for
224
- Rails Event Store'
224
+ Rails Event Store
225
225
  email:
226
226
  - dev@arkency.com
227
227
  executables: []
@@ -244,13 +244,16 @@ files:
244
244
  - lib/rails_event_store_active_record/generators/templates/v1_v2_migration_template.rb
245
245
  - lib/rails_event_store_active_record/generators/v1_v2_migration_generator.rb
246
246
  - lib/rails_event_store_active_record/index_violation_detector.rb
247
- - lib/rails_event_store_active_record/legacy_event_repository.rb
248
247
  - lib/rails_event_store_active_record/version.rb
249
248
  - rails_event_store_active_record.gemspec
250
- homepage: https://github.com/RailsEventStore/rails_event_store_active_record
249
+ homepage: https://railseventstore.org
251
250
  licenses:
252
251
  - MIT
253
- metadata: {}
252
+ metadata:
253
+ homepage_uri: https://railseventstore.org/
254
+ changelog_uri: https://github.com/RailsEventStore/rails_event_store/releases
255
+ source_code_uri: https://github.com/RailsEventStore/rails_event_store
256
+ bug_tracker_uri: https://github.com/RailsEventStore/rails_event_store/issues
254
257
  post_install_message:
255
258
  rdoc_options: []
256
259
  require_paths:
@@ -267,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
267
270
  version: '0'
268
271
  requirements: []
269
272
  rubyforge_project:
270
- rubygems_version: 2.6.13
273
+ rubygems_version: 2.7.6
271
274
  signing_key:
272
275
  specification_version: 4
273
276
  summary: Active Record events repository for Rails Event Store
@@ -1,176 +0,0 @@
1
- require 'active_record'
2
-
3
- module RailsEventStoreActiveRecord
4
- class LegacyEventRepository
5
- class LegacyEvent < ::ActiveRecord::Base
6
- self.primary_key = :id
7
- self.table_name = 'event_store_events'
8
- serialize :metadata
9
- serialize :data
10
- end
11
-
12
- private_constant :LegacyEvent
13
-
14
- def initialize
15
- warn <<-MSG
16
- `RailsEventStoreActiveRecord::LegacyEventRepository` has been deprecated.
17
-
18
- Please migrate to new database schema and use `RailsEventStoreActiveRecord::EventRepository`
19
- instead:
20
-
21
- rails generate rails_event_store_active_record:v1_v2_migration
22
-
23
- MSG
24
-
25
- @mapper = RubyEventStore::Mappers::Default.new
26
- end
27
-
28
- def append_to_stream(events, stream_name, expected_version)
29
- validate_expected_version_is_not_auto(expected_version)
30
- validate_expected_version_is_any_for_global_stream(expected_version, stream_name)
31
-
32
- case expected_version
33
- when :none
34
- validate_stream_is_empty(stream_name)
35
- when :any
36
- when Integer
37
- validate_expected_version_number(expected_version, stream_name)
38
- else
39
- raise RubyEventStore::InvalidExpectedVersion
40
- end
41
-
42
- normalize_to_array(events).each do |event|
43
- data = event.to_h.merge!(stream: stream_name, event_type: event.class)
44
- LegacyEvent.create!(data)
45
- end
46
- self
47
- rescue ActiveRecord::RecordNotUnique
48
- raise RubyEventStore::EventDuplicatedInStream
49
- end
50
-
51
- def link_to_stream(_event_ids, _stream_name, _expected_version)
52
- raise RubyEventStore::NotSupported
53
- end
54
-
55
- def delete_stream(stream_name)
56
- LegacyEvent.where({stream: stream_name}).update_all(stream: RubyEventStore::GLOBAL_STREAM)
57
- end
58
-
59
- def has_event?(event_id)
60
- LegacyEvent.exists?(event_id: event_id)
61
- end
62
-
63
- def last_stream_event(stream_name)
64
- build_event_entity(LegacyEvent.where(stream: stream_name).last)
65
- end
66
-
67
- def read_events_forward(stream_name, start_event_id, count)
68
- stream = LegacyEvent.where(stream: stream_name)
69
- unless start_event_id.equal?(:head)
70
- starting_event = LegacyEvent.find_by(event_id: start_event_id)
71
- stream = stream.where('id > ?', starting_event)
72
- end
73
-
74
- stream.order('id ASC').limit(count)
75
- .map(&method(:build_event_entity))
76
- end
77
-
78
- def read_events_backward(stream_name, start_event_id, count)
79
- stream = LegacyEvent.where(stream: stream_name)
80
- unless start_event_id.equal?(:head)
81
- starting_event = LegacyEvent.find_by(event_id: start_event_id)
82
- stream = stream.where('id < ?', starting_event)
83
- end
84
-
85
- stream.order('id DESC').limit(count)
86
- .map(&method(:build_event_entity))
87
- end
88
-
89
- def read_stream_events_forward(stream_name)
90
- LegacyEvent.where(stream: stream_name).order('id ASC')
91
- .map(&method(:build_event_entity))
92
- end
93
-
94
- def read_stream_events_backward(stream_name)
95
- LegacyEvent.where(stream: stream_name).order('id DESC')
96
- .map(&method(:build_event_entity))
97
- end
98
-
99
- def read_all_streams_forward(start_event_id, count)
100
- stream = LegacyEvent
101
- unless start_event_id.equal?(:head)
102
- starting_event = LegacyEvent.find_by(event_id: start_event_id)
103
- stream = stream.where('id > ?', starting_event)
104
- end
105
-
106
- stream.order('id ASC').limit(count)
107
- .map(&method(:build_event_entity))
108
- end
109
-
110
- def read_all_streams_backward(start_event_id, count)
111
- stream = LegacyEvent
112
- unless start_event_id.equal?(:head)
113
- starting_event = LegacyEvent.find_by(event_id: start_event_id)
114
- stream = stream.where('id < ?', starting_event)
115
- end
116
-
117
- stream.order('id DESC').limit(count)
118
- .map(&method(:build_event_entity))
119
- end
120
-
121
- def read_event(event_id)
122
- build_event_entity(LegacyEvent.find_by(event_id: event_id)) or raise RubyEventStore::EventNotFound.new(event_id)
123
- end
124
-
125
- def get_all_streams
126
- (["all"] + LegacyEvent.pluck(:stream))
127
- .uniq
128
- .map { |name| RubyEventStore::Stream.new(name) }
129
- end
130
-
131
- def add_metadata(event, key, value)
132
- @mapper.add_metadata(event, key, value)
133
- end
134
-
135
- private
136
-
137
- def normalize_to_array(events)
138
- return events if events.is_a?(Enumerable)
139
- [events]
140
- end
141
-
142
- def build_event_entity(record)
143
- return nil unless record
144
- record.event_type.constantize.new(
145
- event_id: record.event_id,
146
- metadata: record.metadata,
147
- data: record.data
148
- )
149
- end
150
-
151
- def last_stream_version(stream_name)
152
- LegacyEvent.where(stream: stream_name).count - 1
153
- end
154
-
155
- def stream_non_empty?(stream_name)
156
- LegacyEvent.where(stream: stream_name).exists?
157
- end
158
-
159
- def validate_expected_version_is_any_for_global_stream(expected_version, stream_name)
160
- raise RubyEventStore::InvalidExpectedVersion if stream_name.eql?(RubyEventStore::GLOBAL_STREAM) && !expected_version.equal?(:any)
161
- end
162
-
163
- def validate_stream_is_empty(stream_name)
164
- raise RubyEventStore::WrongExpectedEventVersion if stream_non_empty?(stream_name)
165
- end
166
-
167
- def validate_expected_version_number(expected_version, stream_name)
168
- raise RubyEventStore::WrongExpectedEventVersion unless last_stream_version(stream_name).equal?(expected_version)
169
- end
170
-
171
- def validate_expected_version_is_not_auto(expected_version)
172
- raise RubyEventStore::InvalidExpectedVersion, ":auto mode is not supported by LegacyEventRepository" if expected_version.equal?(:auto)
173
- end
174
-
175
- end
176
- end