rails_event_store_active_record 0.19.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/README.md +1 -1
- data/lib/rails_event_store_active_record/event.rb +0 -2
- data/lib/rails_event_store_active_record/event_repository.rb +31 -22
- data/lib/rails_event_store_active_record/generators/templates/migration_template.rb +5 -0
- data/lib/rails_event_store_active_record/generators/templates/v1_v2_migration_template.rb +4 -2
- data/lib/rails_event_store_active_record/legacy_event_repository.rb +12 -0
- data/lib/rails_event_store_active_record/version.rb +1 -1
- data/rails_event_store_active_record.gemspec +3 -3
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 870a6b6052a42cee07f40861b1a9ace1a173984f
|
4
|
+
data.tar.gz: 8a4c447a0c33358373a5f8143b45f41f0f44949d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 308760f7dfc995d9b535541150f32c4797f581deb00e349fde26f19320110af803305835cf53814d614aed981fe6cbcc819573a296883a7cfb0c8d34df406762
|
7
|
+
data.tar.gz: b9894d086f193ede1b9e46cd74344f8a8c6a921a4495c7c31e37e77d350dfc087d1c4088394a353b87a4b5de92655021f9e39389bce3e27af03dad1cb5ab2ec6
|
data/Gemfile
CHANGED
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](
|
3
|
+
An Active Record based implementation of events repository for [Rails Event Store](https://github.com/RailsEventStore/rails_event_store/rails_event_store).
|
4
4
|
|
5
5
|
This is the default repository used in `rails_event_store` gem.
|
@@ -6,8 +6,9 @@ module RailsEventStoreActiveRecord
|
|
6
6
|
|
7
7
|
POSITION_SHIFT = 1
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(mapper: RubyEventStore::Mappers::Default.new)
|
10
10
|
verify_correct_schema_present
|
11
|
+
@mapper = mapper
|
11
12
|
end
|
12
13
|
|
13
14
|
def append_to_stream(events, stream_name, expected_version)
|
@@ -32,14 +33,10 @@ module RailsEventStoreActiveRecord
|
|
32
33
|
position = unless expected_version.equal?(:any)
|
33
34
|
expected_version + index + POSITION_SHIFT
|
34
35
|
end
|
35
|
-
|
36
|
-
id: event.event_id,
|
37
|
-
data: event.data,
|
38
|
-
metadata: event.metadata,
|
39
|
-
event_type: event.class,
|
40
|
-
)
|
36
|
+
build_event_record(event).save!
|
41
37
|
events = [{
|
42
38
|
stream: RubyEventStore::GLOBAL_STREAM,
|
39
|
+
position: nil,
|
43
40
|
event_id: event.event_id
|
44
41
|
}]
|
45
42
|
events.unshift({
|
@@ -68,9 +65,8 @@ module RailsEventStoreActiveRecord
|
|
68
65
|
end
|
69
66
|
|
70
67
|
def last_stream_event(stream_name)
|
71
|
-
|
72
|
-
|
73
|
-
)
|
68
|
+
record = EventInStream.where(stream: stream_name).order('position DESC, id DESC').first
|
69
|
+
record && build_event_instance(record)
|
74
70
|
end
|
75
71
|
|
76
72
|
def read_events_forward(stream_name, after_event_id, count)
|
@@ -81,7 +77,7 @@ module RailsEventStoreActiveRecord
|
|
81
77
|
end
|
82
78
|
|
83
79
|
stream.preload(:event).order('position ASC, id ASC').limit(count)
|
84
|
-
.map(&method(:
|
80
|
+
.map(&method(:build_event_instance))
|
85
81
|
end
|
86
82
|
|
87
83
|
def read_events_backward(stream_name, before_event_id, count)
|
@@ -92,17 +88,17 @@ module RailsEventStoreActiveRecord
|
|
92
88
|
end
|
93
89
|
|
94
90
|
stream.preload(:event).order('position DESC, id DESC').limit(count)
|
95
|
-
.map(&method(:
|
91
|
+
.map(&method(:build_event_instance))
|
96
92
|
end
|
97
93
|
|
98
94
|
def read_stream_events_forward(stream_name)
|
99
95
|
EventInStream.preload(:event).where(stream: stream_name).order('position ASC, id ASC')
|
100
|
-
.map(&method(:
|
96
|
+
.map(&method(:build_event_instance))
|
101
97
|
end
|
102
98
|
|
103
99
|
def read_stream_events_backward(stream_name)
|
104
100
|
EventInStream.preload(:event).where(stream: stream_name).order('position DESC, id DESC')
|
105
|
-
.map(&method(:
|
101
|
+
.map(&method(:build_event_instance))
|
106
102
|
end
|
107
103
|
|
108
104
|
def read_all_streams_forward(after_event_id, count)
|
@@ -113,7 +109,7 @@ module RailsEventStoreActiveRecord
|
|
113
109
|
end
|
114
110
|
|
115
111
|
stream.preload(:event).order('id ASC').limit(count)
|
116
|
-
.map(&method(:
|
112
|
+
.map(&method(:build_event_instance))
|
117
113
|
end
|
118
114
|
|
119
115
|
def read_all_streams_backward(before_event_id, count)
|
@@ -124,24 +120,37 @@ module RailsEventStoreActiveRecord
|
|
124
120
|
end
|
125
121
|
|
126
122
|
stream.preload(:event).order('id DESC').limit(count)
|
127
|
-
.map(&method(:
|
123
|
+
.map(&method(:build_event_instance))
|
128
124
|
end
|
129
125
|
|
130
126
|
private
|
131
127
|
|
128
|
+
attr_reader :mapper
|
129
|
+
|
132
130
|
def detect_pkey_index_violated(e)
|
133
131
|
e.message.include?("for key 'PRIMARY'") || # MySQL
|
134
132
|
e.message.include?("event_store_events_pkey") || # Postgresql
|
135
133
|
e.message.include?("event_store_events.id") # Sqlite3
|
136
134
|
end
|
137
135
|
|
138
|
-
def
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
136
|
+
def build_event_record(event)
|
137
|
+
serialized_record = mapper.event_to_serialized_record(event)
|
138
|
+
Event.new(
|
139
|
+
id: serialized_record.event_id,
|
140
|
+
data: serialized_record.data,
|
141
|
+
metadata: serialized_record.metadata,
|
142
|
+
event_type: serialized_record.event_type
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
146
|
+
def build_event_instance(record)
|
147
|
+
serialized_record = RubyEventStore::SerializedRecord.new(
|
148
|
+
event_id: record.event.id,
|
149
|
+
metadata: record.event.metadata,
|
150
|
+
data: record.event.data,
|
151
|
+
event_type: record.event.event_type
|
144
152
|
)
|
153
|
+
mapper.serialized_record_to_event(serialized_record)
|
145
154
|
end
|
146
155
|
|
147
156
|
def normalize_to_array(events)
|
@@ -1,6 +1,8 @@
|
|
1
1
|
class CreateEventStoreEvents < ActiveRecord::Migration<%= migration_version %>
|
2
2
|
def change
|
3
3
|
postgres = ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
4
|
+
sqlite = ActiveRecord::Base.connection.adapter_name == "SQLite"
|
5
|
+
rails_42 = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5.0.0")
|
4
6
|
enable_extension "pgcrypto" if postgres
|
5
7
|
create_table(:event_store_events_in_streams, force: false) do |t|
|
6
8
|
t.string :stream, null: false
|
@@ -31,6 +33,9 @@ class CreateEventStoreEvents < ActiveRecord::Migration<%= migration_version %>
|
|
31
33
|
t.text :data, null: false
|
32
34
|
t.datetime :created_at, null: false
|
33
35
|
end
|
36
|
+
if sqlite && rails_42
|
37
|
+
add_index :event_store_events, :id, unique: true
|
38
|
+
end
|
34
39
|
end
|
35
40
|
add_index :event_store_events, :created_at
|
36
41
|
end
|
@@ -13,6 +13,7 @@ class MigrateResSchemaV1ToV2 < ActiveRecord::Migration<%= migration_version %>
|
|
13
13
|
postgres = ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
14
14
|
mysql = ActiveRecord::Base.connection.adapter_name == "Mysql2"
|
15
15
|
sqlite = ActiveRecord::Base.connection.adapter_name == "SQLite"
|
16
|
+
rails_42 = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5.0.0")
|
16
17
|
enable_extension "pgcrypto" if postgres
|
17
18
|
create_table(:event_store_events_in_streams, force: false) do |t|
|
18
19
|
t.string :stream, null: false
|
@@ -68,6 +69,7 @@ class MigrateResSchemaV1ToV2 < ActiveRecord::Migration<%= migration_version %>
|
|
68
69
|
t.text :data, null: false
|
69
70
|
t.datetime :created_at, null: false
|
70
71
|
end
|
72
|
+
add_index :event_store_events, :id, unique: true if rails_42
|
71
73
|
add_index :event_store_events, :created_at
|
72
74
|
execute <<-SQL
|
73
75
|
INSERT INTO event_store_events(id, event_type, metadata, data, created_at)
|
@@ -81,7 +83,7 @@ class MigrateResSchemaV1ToV2 < ActiveRecord::Migration<%= migration_version %>
|
|
81
83
|
end
|
82
84
|
|
83
85
|
def preserve_positions?(stream_name)
|
84
|
-
#
|
86
|
+
# https://railseventstore.org/docs/expected_version/
|
85
87
|
#
|
86
88
|
# return true if you use given stream for event sourcing
|
87
89
|
# (especially with AggregateRoot gem)
|
@@ -94,4 +96,4 @@ class MigrateResSchemaV1ToV2 < ActiveRecord::Migration<%= migration_version %>
|
|
94
96
|
raise NotImplementedError
|
95
97
|
# false
|
96
98
|
end
|
97
|
-
end
|
99
|
+
end
|
@@ -11,6 +11,18 @@ module RailsEventStoreActiveRecord
|
|
11
11
|
|
12
12
|
private_constant :LegacyEvent
|
13
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
|
+
end
|
25
|
+
|
14
26
|
def append_to_stream(events, stream_name, expected_version)
|
15
27
|
validate_expected_version_is_not_auto(expected_version)
|
16
28
|
validate_expected_version_is_any_for_global_stream(expected_version, stream_name)
|
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
|
13
13
|
spec.summary = %q{Active Record events repository for Rails Event Store}
|
14
14
|
spec.description = %q{Implementation of events repository based on Rails Active Record for Rails Event Store'}
|
15
|
-
spec.homepage = 'https://github.com/
|
15
|
+
spec.homepage = 'https://github.com/RailsEventStore/rails_event_store_active_record'
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
18
|
spec.bindir = 'exe'
|
@@ -30,8 +30,8 @@ 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.
|
33
|
+
spec.add_dependency 'ruby_event_store', '= 0.20.0'
|
34
34
|
spec.add_dependency 'activesupport', '>= 3.0'
|
35
35
|
spec.add_dependency 'activemodel', '>= 3.0'
|
36
|
-
spec.add_dependency 'activerecord-import'
|
36
|
+
spec.add_dependency 'activerecord-import', '~> 0.21'
|
37
37
|
end
|
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.
|
4
|
+
version: 0.20.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-11-
|
11
|
+
date: 2017-11-24 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.
|
159
|
+
version: 0.20.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.
|
166
|
+
version: 0.20.0
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: activesupport
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -196,16 +196,16 @@ dependencies:
|
|
196
196
|
name: activerecord-import
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
198
198
|
requirements:
|
199
|
-
- - "
|
199
|
+
- - "~>"
|
200
200
|
- !ruby/object:Gem::Version
|
201
|
-
version: '0'
|
201
|
+
version: '0.21'
|
202
202
|
type: :runtime
|
203
203
|
prerelease: false
|
204
204
|
version_requirements: !ruby/object:Gem::Requirement
|
205
205
|
requirements:
|
206
|
-
- - "
|
206
|
+
- - "~>"
|
207
207
|
- !ruby/object:Gem::Version
|
208
|
-
version: '0'
|
208
|
+
version: '0.21'
|
209
209
|
description: Implementation of events repository based on Rails Active Record for
|
210
210
|
Rails Event Store'
|
211
211
|
email:
|
@@ -230,7 +230,7 @@ files:
|
|
230
230
|
- lib/rails_event_store_active_record/legacy_event_repository.rb
|
231
231
|
- lib/rails_event_store_active_record/version.rb
|
232
232
|
- rails_event_store_active_record.gemspec
|
233
|
-
homepage: https://github.com/
|
233
|
+
homepage: https://github.com/RailsEventStore/rails_event_store_active_record
|
234
234
|
licenses:
|
235
235
|
- MIT
|
236
236
|
metadata: {}
|
@@ -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.
|
253
|
+
rubygems_version: 2.6.11
|
254
254
|
signing_key:
|
255
255
|
specification_version: 4
|
256
256
|
summary: Active Record events repository for Rails Event Store
|