rails_event_store_active_record-legacy 0.31.1 → 0.32.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/Makefile +2 -2
- data/lib/rails_event_store_active_record/legacy.rb +1 -0
- data/lib/rails_event_store_active_record/legacy/event_repository.rb +18 -16
- data/lib/rails_event_store_active_record/legacy/generators/templates/v1_v2_migration_template.rb +99 -0
- data/lib/rails_event_store_active_record/legacy/generators/v1_v2_migration_generator.rb +31 -0
- data/lib/rails_event_store_active_record/legacy/version.rb +1 -1
- data/rails_event_store_active_record-legacy.gemspec +4 -2
- metadata +36 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eff68a246249af3de610dcd3726b949438b066e9691bb11ab50a880cd9fec5be
|
4
|
+
data.tar.gz: 11e680a6c72139c632e4b450e319bd9e8f109694bb286921d38b0516d12b5efe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8f62c1d22349f5bd9986ab570c6c75394fc202a6507eb947dcde24f660f1b93bb6d01f19d41dee54370633a6633f046d93c057e8025cce7589323367e3c34ee
|
7
|
+
data.tar.gz: 5a806d547c43b3e067bcfa64569caee427d732f0c3199be9878f6160f5d453231f114036106ceb46c60ec9398c806f23c10e0ab320bbab80adbc09cb901b57db
|
data/Gemfile
CHANGED
data/Makefile
CHANGED
@@ -26,14 +26,14 @@ test: ## Run unit tests
|
|
26
26
|
|
27
27
|
mutate: test ## Run mutation tests
|
28
28
|
@echo "Running mutation tests"
|
29
|
-
@BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
|
29
|
+
@MUTATING=true BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
|
30
30
|
$(addprefix --require ,$(REQUIRE)) \
|
31
31
|
$(addprefix --ignore-subject ,$(IGNORE)) \
|
32
32
|
--use rspec "$(SUBJECT)"
|
33
33
|
|
34
34
|
mutate-fast: ## Run mutation tests with --fail-fast
|
35
35
|
@echo "Running mutation tests with --fail-fast"
|
36
|
-
@BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
|
36
|
+
@MUTATING=true BUNDLE_GEMFILE=Gemfile DATABASE_URL=$(DATABASE_URL) bundle exec mutant --include lib \
|
37
37
|
$(addprefix --require ,$(REQUIRE)) \
|
38
38
|
$(addprefix --ignore-subject ,$(IGNORE)) \
|
39
39
|
--fail-fast \
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "rails_event_store_active_record/legacy/version"
|
2
2
|
require "rails_event_store_active_record/legacy/event_repository"
|
3
|
+
require "rails_event_store_active_record/legacy/generators/v1_v2_migration_generator"
|
3
4
|
|
4
5
|
module RailsEventStoreActiveRecord
|
5
6
|
LegacyEventRepository = Legacy::EventRepository
|
@@ -60,38 +60,40 @@ instead:
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def read(spec)
|
63
|
-
stream = LegacyEvent.order(id: order(spec
|
64
|
-
stream = stream.limit(spec.
|
63
|
+
stream = LegacyEvent.order(id: order(spec))
|
64
|
+
stream = stream.limit(spec.limit) if spec.limit?
|
65
65
|
stream = stream.where(start_condition(spec)) unless spec.head?
|
66
|
-
stream = stream.where(stream: spec.
|
66
|
+
stream = stream.where(stream: spec.stream.name) unless spec.stream.global?
|
67
67
|
|
68
68
|
if spec.batched?
|
69
69
|
batch_reader = ->(offset, limit) { stream.offset(offset).limit(limit).map(&method(:build_event_entity)) }
|
70
|
-
RubyEventStore::BatchEnumerator.new(spec.batch_size,
|
70
|
+
RubyEventStore::BatchEnumerator.new(spec.batch_size, spec.limit, batch_reader).each
|
71
|
+
elsif spec.first?
|
72
|
+
build_event_entity(stream.first)
|
73
|
+
elsif spec.last?
|
74
|
+
build_event_entity(stream.last)
|
71
75
|
else
|
72
76
|
stream.map(&method(:build_event_entity)).each
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
+
def streams_of(event_id)
|
81
|
+
LegacyEvent.where(event_id: event_id)
|
82
|
+
.pluck(:stream)
|
83
|
+
.map{|name| RubyEventStore::Stream.new(name)}
|
80
84
|
end
|
81
85
|
|
86
|
+
private
|
87
|
+
|
82
88
|
def start_condition(specification)
|
83
89
|
event_record =
|
84
90
|
LegacyEvent.find_by!(event_id: specification.start)
|
85
|
-
|
86
|
-
|
87
|
-
['id > ?', event_record]
|
88
|
-
else
|
89
|
-
['id < ?', event_record]
|
90
|
-
end
|
91
|
+
condition = specification.forward? ? 'id > ?' : 'id < ?'
|
92
|
+
[condition, event_record]
|
91
93
|
end
|
92
94
|
|
93
|
-
def order(
|
94
|
-
|
95
|
+
def order(spec)
|
96
|
+
spec.forward? ? 'ASC' : 'DESC'
|
95
97
|
end
|
96
98
|
|
97
99
|
|
data/lib/rails_event_store_active_record/legacy/generators/templates/v1_v2_migration_template.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# This migration is not intended for live data migration
|
2
|
+
# It assumes no data is added at when it is running.
|
3
|
+
# So stop your application servers from accepting new requests
|
4
|
+
# and processing background jobs before running
|
5
|
+
|
6
|
+
# Make sure you have a backup before running on production
|
7
|
+
|
8
|
+
# 10_000_000 distinct stream names
|
9
|
+
# can cause around 2GB of RAM usage
|
10
|
+
# make sure you can run this migration on your production system
|
11
|
+
class MigrateResSchemaV1ToV2 < ActiveRecord::Migration<%= migration_version %>
|
12
|
+
def up
|
13
|
+
postgres = ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
14
|
+
mysql = ActiveRecord::Base.connection.adapter_name == "Mysql2"
|
15
|
+
sqlite = ActiveRecord::Base.connection.adapter_name == "SQLite"
|
16
|
+
rails_42 = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5.0.0")
|
17
|
+
enable_extension "pgcrypto" if postgres
|
18
|
+
create_table(:event_store_events_in_streams, force: false) do |t|
|
19
|
+
t.string :stream, null: false
|
20
|
+
t.integer :position, null: true
|
21
|
+
if postgres
|
22
|
+
t.references :event, null: false, type: :uuid
|
23
|
+
else
|
24
|
+
t.references :event, null: false, type: :string
|
25
|
+
end
|
26
|
+
t.datetime :created_at, null: false
|
27
|
+
end
|
28
|
+
streams = {}
|
29
|
+
RailsEventStoreActiveRecord::Event.reset_column_information
|
30
|
+
RailsEventStoreActiveRecord::Event.find_each do |ev|
|
31
|
+
position = nil
|
32
|
+
if preserve_positions?(ev.stream)
|
33
|
+
streams[ev.stream] ||= -1
|
34
|
+
position = streams[ev.stream] += 1
|
35
|
+
end
|
36
|
+
RailsEventStoreActiveRecord::EventInStream.create!(
|
37
|
+
stream: ev.stream,
|
38
|
+
position: position,
|
39
|
+
event_id: ev.event_id,
|
40
|
+
created_at: ev.created_at,
|
41
|
+
)
|
42
|
+
RailsEventStoreActiveRecord::EventInStream.create!(
|
43
|
+
stream: 'all',
|
44
|
+
position: nil,
|
45
|
+
event_id: ev.event_id,
|
46
|
+
created_at: ev.created_at,
|
47
|
+
) unless ev.stream == 'all'
|
48
|
+
end
|
49
|
+
|
50
|
+
add_index :event_store_events_in_streams, [:stream, :position], unique: true
|
51
|
+
add_index :event_store_events_in_streams, [:stream, :event_id], unique: true
|
52
|
+
add_index :event_store_events_in_streams, [:created_at]
|
53
|
+
|
54
|
+
remove_index :event_store_events, :event_type
|
55
|
+
remove_column :event_store_events, :stream
|
56
|
+
remove_column :event_store_events, :id
|
57
|
+
rename_column :event_store_events, :event_id, :id
|
58
|
+
change_column :event_store_events, :id, "uuid using id::uuid", default: -> { "gen_random_uuid()" } if postgres
|
59
|
+
change_column :event_store_events, :id, "string", limit: 36 if mysql || sqlite
|
60
|
+
|
61
|
+
case ActiveRecord::Base.connection.adapter_name
|
62
|
+
when "SQLite"
|
63
|
+
remove_index :event_store_events, name: :index_event_store_events_on_id
|
64
|
+
rename_table :event_store_events, :old_event_store_events
|
65
|
+
create_table(:event_store_events, id: false, force: false) do |t|
|
66
|
+
t.string :id, limit: 36, primary_key: true, null: false
|
67
|
+
t.string :event_type, null: false
|
68
|
+
t.text :metadata
|
69
|
+
t.text :data, null: false
|
70
|
+
t.datetime :created_at, null: false
|
71
|
+
end
|
72
|
+
add_index :event_store_events, :id, unique: true if rails_42
|
73
|
+
add_index :event_store_events, :created_at
|
74
|
+
execute <<-SQL
|
75
|
+
INSERT INTO event_store_events(id, event_type, metadata, data, created_at)
|
76
|
+
SELECT id, event_type, metadata, data, created_at FROM old_event_store_events;
|
77
|
+
SQL
|
78
|
+
drop_table :old_event_store_events
|
79
|
+
else
|
80
|
+
execute "ALTER TABLE event_store_events ADD PRIMARY KEY (id);"
|
81
|
+
remove_index :event_store_events, name: :index_event_store_events_on_id
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def preserve_positions?(stream_name)
|
86
|
+
# https://railseventstore.org/docs/expected_version/
|
87
|
+
#
|
88
|
+
# return true if you use given stream for event sourcing
|
89
|
+
# (especially with AggregateRoot gem)
|
90
|
+
# return true if you use an Integer or :none as
|
91
|
+
# expected_version when publishing in this stream
|
92
|
+
#
|
93
|
+
# return false if use use :any (the default) as expected_version
|
94
|
+
# when publishing to this stream
|
95
|
+
|
96
|
+
raise NotImplementedError
|
97
|
+
# false
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require 'rails/generators'
|
3
|
+
rescue LoadError
|
4
|
+
end
|
5
|
+
|
6
|
+
module RailsEventStoreActiveRecord
|
7
|
+
module Legacy
|
8
|
+
class V1V2MigrationGenerator < Rails::Generators::Base
|
9
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), '../generators/templates'))
|
10
|
+
|
11
|
+
def create_migration
|
12
|
+
template "v1_v2_migration_template.rb", "db/migrate/#{timestamp}_migrate_res_schema_v1_to_v2.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def rails_version
|
18
|
+
Rails::VERSION::STRING
|
19
|
+
end
|
20
|
+
|
21
|
+
def migration_version
|
22
|
+
return nil if Gem::Version.new(rails_version) < Gem::Version.new("5.0.0")
|
23
|
+
"[4.2]"
|
24
|
+
end
|
25
|
+
|
26
|
+
def timestamp
|
27
|
+
Time.now.strftime("%Y%m%d%H%M%S")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end if defined?(Rails::Generators::Base)
|
@@ -35,10 +35,12 @@ Gem::Specification.new do |spec|
|
|
35
35
|
spec.add_development_dependency 'sqlite3', '1.3.13'
|
36
36
|
spec.add_development_dependency 'pg', '0.21'
|
37
37
|
spec.add_development_dependency 'mysql2', '0.4.10'
|
38
|
+
spec.add_development_dependency 'fakefs', '~> 0.11.2'
|
38
39
|
spec.add_development_dependency 'childprocess'
|
39
|
-
spec.add_development_dependency 'mutant-rspec', '~> 0.8.
|
40
|
+
spec.add_development_dependency 'mutant-rspec', '~> 0.8.17'
|
40
41
|
|
41
|
-
spec.add_dependency 'ruby_event_store', '= 0.
|
42
|
+
spec.add_dependency 'ruby_event_store', '= 0.32.0'
|
43
|
+
spec.add_dependency 'rails_event_store_active_record', '= 0.32.0'
|
42
44
|
spec.add_dependency 'activesupport', '>= 3.0'
|
43
45
|
spec.add_dependency 'activemodel', '>= 3.0'
|
44
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_event_store_active_record-legacy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.32.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-
|
11
|
+
date: 2018-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - '='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 0.4.10
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: fakefs
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.11.2
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.11.2
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: childprocess
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,28 +142,42 @@ dependencies:
|
|
128
142
|
requirements:
|
129
143
|
- - "~>"
|
130
144
|
- !ruby/object:Gem::Version
|
131
|
-
version: 0.8.
|
145
|
+
version: 0.8.17
|
132
146
|
type: :development
|
133
147
|
prerelease: false
|
134
148
|
version_requirements: !ruby/object:Gem::Requirement
|
135
149
|
requirements:
|
136
150
|
- - "~>"
|
137
151
|
- !ruby/object:Gem::Version
|
138
|
-
version: 0.8.
|
152
|
+
version: 0.8.17
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: ruby_event_store
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
142
156
|
requirements:
|
143
157
|
- - '='
|
144
158
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
159
|
+
version: 0.32.0
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - '='
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 0.32.0
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rails_event_store_active_record
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - '='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 0.32.0
|
146
174
|
type: :runtime
|
147
175
|
prerelease: false
|
148
176
|
version_requirements: !ruby/object:Gem::Requirement
|
149
177
|
requirements:
|
150
178
|
- - '='
|
151
179
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
180
|
+
version: 0.32.0
|
153
181
|
- !ruby/object:Gem::Dependency
|
154
182
|
name: activesupport
|
155
183
|
requirement: !ruby/object:Gem::Requirement
|
@@ -192,6 +220,8 @@ files:
|
|
192
220
|
- README.md
|
193
221
|
- lib/rails_event_store_active_record/legacy.rb
|
194
222
|
- lib/rails_event_store_active_record/legacy/event_repository.rb
|
223
|
+
- lib/rails_event_store_active_record/legacy/generators/templates/v1_v2_migration_template.rb
|
224
|
+
- lib/rails_event_store_active_record/legacy/generators/v1_v2_migration_generator.rb
|
195
225
|
- lib/rails_event_store_active_record/legacy/version.rb
|
196
226
|
- rails_event_store_active_record-legacy.gemspec
|
197
227
|
homepage: https://railseventstore.org
|