orfeas_lyra 0.9.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2021 -17
- data/README.md +22 -9
- data/Rakefile +40 -1
- data/app/controllers/lyra/application_controller.rb +127 -0
- data/app/controllers/lyra/dashboard_controller.rb +33 -31
- data/app/controllers/lyra/flow_controller.rb +32 -34
- data/app/controllers/lyra/privacy_controller.rb +57 -18
- data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
- data/app/views/lyra/dashboard/index.html.erb +1 -0
- data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
- data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
- data/app/views/lyra/privacy/policy.html.erb +7 -0
- data/app/views/lyra/privacy/rights.html.erb +78 -0
- data/config/privacy_policies.rb +0 -3
- data/config/routes.rb +1 -0
- data/lib/lyra/access_log.rb +187 -6
- data/lib/lyra/advisory_lock.rb +236 -0
- data/lib/lyra/aggregate.rb +1 -1
- data/lib/lyra/associations/habtm_links.rb +382 -19
- data/lib/lyra/associations/maintenance.rb +229 -0
- data/lib/lyra/attribution.rb +48 -0
- data/lib/lyra/bypass_events.rb +446 -46
- data/lib/lyra/command_handler.rb +19 -14
- data/lib/lyra/configuration.rb +237 -10
- data/lib/lyra/consistency/read_your_writes.rb +4 -2
- data/lib/lyra/correlation.rb +121 -0
- data/lib/lyra/domain_events.rb +4 -0
- data/lib/lyra/dual_view.rb +53 -16
- data/lib/lyra/encrypted_attributes.rb +5 -0
- data/lib/lyra/engine.rb +67 -0
- data/lib/lyra/erasure.rb +1011 -106
- data/lib/lyra/event.rb +35 -1
- data/lib/lyra/event_data.rb +38 -0
- data/lib/lyra/event_flow.rb +31 -15
- data/lib/lyra/event_store_adapter.rb +28 -0
- data/lib/lyra/event_window.rb +58 -0
- data/lib/lyra/genesis.rb +548 -44
- data/lib/lyra/interceptors/association_interceptor.rb +689 -34
- data/lib/lyra/interceptors/crud_interceptor.rb +578 -91
- data/lib/lyra/internal.rb +26 -0
- data/lib/lyra/mode_sync.rb +51 -3
- data/lib/lyra/mode_transition.rb +257 -30
- data/lib/lyra/model_selection.rb +35 -0
- data/lib/lyra/own_connection.rb +17 -2
- data/lib/lyra/privacy/adapters/pam.rb +25 -2
- data/lib/lyra/privacy/gdpr_compliance.rb +314 -48
- data/lib/lyra/privacy/interface.rb +27 -7
- data/lib/lyra/privacy/subject_links.rb +143 -0
- data/lib/lyra/privacy/subject_records.rb +369 -0
- data/lib/lyra/projection.rb +7 -1
- data/lib/lyra/projections/async_checkpoint.rb +32 -0
- data/lib/lyra/projections/async_projection_job.rb +166 -23
- data/lib/lyra/projections/cached_joins.rb +33 -18
- data/lib/lyra/projections/cached_projection.rb +74 -33
- data/lib/lyra/projections/cached_relation.rb +3175 -321
- data/lib/lyra/projections/default_scopes.rb +254 -0
- data/lib/lyra/projections/deferred_relation.rb +23 -3
- data/lib/lyra/projections/event_store_reader.rb +47 -8
- data/lib/lyra/projections/foreign_keys.rb +92 -0
- data/lib/lyra/projections/lazy_projection.rb +165 -27
- data/lib/lyra/projections/model_projection.rb +165 -14
- data/lib/lyra/projections/rebuild.rb +34 -6
- data/lib/lyra/projections/reference_links.rb +463 -72
- data/lib/lyra/projections/typed_values.rb +629 -0
- data/lib/lyra/projections/unique_indexes.rb +595 -0
- data/lib/lyra/projections/uniqueness.rb +784 -0
- data/lib/lyra/projections/value_digest.rb +93 -0
- data/lib/lyra/purpose_bound_reads.rb +912 -77
- data/lib/lyra/repair.rb +119 -28
- data/lib/lyra/retention.rb +114 -8
- data/lib/lyra/rights/consent_store.rb +415 -0
- data/lib/lyra/rights/erasure_guard.rb +135 -0
- data/lib/lyra/rights/pending.rb +74 -0
- data/lib/lyra/rights/queries.rb +269 -0
- data/lib/lyra/rights/store.rb +512 -0
- data/lib/lyra/rights/subject_keys.rb +224 -0
- data/lib/lyra/rights.rb +281 -0
- data/lib/lyra/saves_without_effect.rb +69 -0
- data/lib/lyra/stream_tail.rb +319 -0
- data/lib/lyra/strict_data_access.rb +916 -106
- data/lib/lyra/temporal.rb +4 -2
- data/lib/lyra/touch_registry.rb +404 -0
- data/lib/lyra/verification/bypass_workflow.rb +452 -42
- data/lib/lyra/verification/rights_workflows.rb +170 -0
- data/lib/lyra/version.rb +1 -1
- data/lib/lyra/visualization/event_graph.rb +1 -1
- data/lib/lyra/visualization/timeline.rb +2 -2
- data/lib/lyra.rb +43 -2
- data/lib/tasks/lyra_mode.rake +110 -8
- data/lib/tasks/lyra_projections.rake +43 -2
- metadata +34 -4
- data/lib/lyra/associations/event_aware.rb +0 -224
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lyra
|
|
4
|
+
module Projections
|
|
5
|
+
# A model's default scopes in ES-NoProj reads.
|
|
6
|
+
#
|
|
7
|
+
# Active Record builds every read of a table from the model's default
|
|
8
|
+
# scopes (Model.all is default_scoped; an association reads through
|
|
9
|
+
# scope_for_association), unless the read runs under a scope of its own:
|
|
10
|
+
# Model.unscoped { ... } sets an empty current scope, and a default scope
|
|
11
|
+
# being built is not applied to itself. ES-NoProj reads, rebuilt from the
|
|
12
|
+
# event log, used to leave the default scopes out, so soft-deleted records
|
|
13
|
+
# (default_scope { where(deleted_at: nil) }, as paranoia, discard and
|
|
14
|
+
# Solidus use) were read back.
|
|
15
|
+
#
|
|
16
|
+
# The relation Active Record would read through is built here as Active
|
|
17
|
+
# Record builds it (past Lyra's read overrides: no SQL runs) and the
|
|
18
|
+
# event-store records are narrowed by it (CachedRelation#narrowed_by): its
|
|
19
|
+
# where clause, order and limit. A scope narrowed_by cannot evaluate (an
|
|
20
|
+
# SQL string, a join, an ordering by an expression) raises
|
|
21
|
+
# UnsupportedQuery on the read: answering without it would return the
|
|
22
|
+
# records it leaves out. ModeTransition refuses the switch into ES-NoProj
|
|
23
|
+
# for such a model up front (unevaluable).
|
|
24
|
+
#
|
|
25
|
+
# A model without default scopes costs one check per read and nothing
|
|
26
|
+
# else; its reads are as before.
|
|
27
|
+
module DefaultScopes
|
|
28
|
+
class << self
|
|
29
|
+
# Whether +klass+ has default scopes, as Active Record asks it
|
|
30
|
+
# (scope_attributes?): declared ones, or a default_scope class method.
|
|
31
|
+
def any?(klass)
|
|
32
|
+
klass.default_scopes.any? || klass.respond_to?(:default_scope)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The Active Record relation a class-level read of +klass+ selects
|
|
36
|
+
# through, as Active Record's all builds it: the current scope when one
|
|
37
|
+
# is set (Model.unscoped { ... } sets an empty one), otherwise the
|
|
38
|
+
# default scopes. nil when the read selects every record.
|
|
39
|
+
def read_scope(klass, all_queries: nil)
|
|
40
|
+
current = klass.current_scope
|
|
41
|
+
if current.is_a?(ActiveRecord::Relation)
|
|
42
|
+
return nil if current.model == klass && bare?(current)
|
|
43
|
+
|
|
44
|
+
return built { current.model == klass ? current : klass.unscoped.merge(current) }
|
|
45
|
+
end
|
|
46
|
+
return nil if !any?(klass) || klass.send(:ignore_default_scope?)
|
|
47
|
+
|
|
48
|
+
scope = built { klass.default_scoped(all_queries: all_queries) }
|
|
49
|
+
bare?(scope) ? nil : scope
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The default scopes of +klass+ as Active Record applies them to the
|
|
53
|
+
# +reflection+ of +owner+ (scope_for_association), less what the
|
|
54
|
+
# association's own scope unscopes (-> { with_discarded }). nil when
|
|
55
|
+
# they select every record.
|
|
56
|
+
def for_association(klass, reflection, owner)
|
|
57
|
+
return nil unless any?(klass)
|
|
58
|
+
|
|
59
|
+
remembered([:association, klass, reflection]) do
|
|
60
|
+
scope = built do
|
|
61
|
+
default = klass.scope_for_association
|
|
62
|
+
unscoped = reflection.scope ? association_unscopes(klass, reflection, owner) : []
|
|
63
|
+
unscoped.any? ? default.unscope(*unscoped) : default
|
|
64
|
+
end
|
|
65
|
+
bare?(scope) ? nil : scope
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Run the block remembering in +store+ the default scopes it builds
|
|
70
|
+
# (for_association, for_through): run again with the same store, it
|
|
71
|
+
# uses them instead of running the scopes' bodies again. A :through
|
|
72
|
+
# association's relation is built again this way for a write
|
|
73
|
+
# (Rebuildable), so the application's default scopes run once, when
|
|
74
|
+
# the relation is built, as Active Record's do. An association read
|
|
75
|
+
# through it for another owner shares its model's scope, as the
|
|
76
|
+
# owner seldom changes what a default scope unscopes.
|
|
77
|
+
def remembering(store)
|
|
78
|
+
previous = Thread.current[REMEMBERED]
|
|
79
|
+
Thread.current[REMEMBERED] = store
|
|
80
|
+
yield
|
|
81
|
+
ensure
|
|
82
|
+
Thread.current[REMEMBERED] = previous
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
REMEMBERED = :lyra_default_scopes_remembered
|
|
86
|
+
|
|
87
|
+
def remembered(key)
|
|
88
|
+
store = Thread.current[REMEMBERED]
|
|
89
|
+
return yield unless store
|
|
90
|
+
return store[key] if store.key?(key)
|
|
91
|
+
|
|
92
|
+
store[key] = yield
|
|
93
|
+
end
|
|
94
|
+
private :remembered
|
|
95
|
+
|
|
96
|
+
# The default scopes of the target of a :through +reflection+ of
|
|
97
|
+
# +owner+ as Active Record's join applies them: less what any scope
|
|
98
|
+
# evaluated on the target's table unscopes, the source association's
|
|
99
|
+
# (belongs_to :variant, -> { with_discarded }) as well as the through
|
|
100
|
+
# association's own (has_one :x, -> { with_discarded }, through: ...).
|
|
101
|
+
# Active Record evaluates both with the owner (AssociationScope's
|
|
102
|
+
# add_constraints). nil when they select every record.
|
|
103
|
+
def for_through(reflection, owner)
|
|
104
|
+
remembered([:through, reflection]) do
|
|
105
|
+
klass = reflection.klass
|
|
106
|
+
return nil unless any?(klass)
|
|
107
|
+
|
|
108
|
+
scope = built do
|
|
109
|
+
unscoped = reflection.constraints.flat_map do |constraint|
|
|
110
|
+
relation = klass.unscoped
|
|
111
|
+
item = relation.instance_exec(owner, &constraint) || relation
|
|
112
|
+
item.respond_to?(:unscope_values) ? item.unscope_values : []
|
|
113
|
+
end
|
|
114
|
+
default = klass.scope_for_association
|
|
115
|
+
unscoped.any? ? default.unscope(*unscoped) : default
|
|
116
|
+
end
|
|
117
|
+
bare?(scope) ? nil : scope
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# +relation+ (event-store records) narrowed by +scope+, an Active
|
|
122
|
+
# Record relation of the same model. The result's plan starts from
|
|
123
|
+
# +base+ (the records before any condition: +relation+ itself, or a
|
|
124
|
+
# Proc building them) and +steps+ (conditions +relation+ already
|
|
125
|
+
# applied), then the default scope, so that a later unscope
|
|
126
|
+
# (Model.with_discarded) or rewhere can take conditions off again
|
|
127
|
+
# (CachedRelation#unscope).
|
|
128
|
+
def narrow(relation, scope, what = nil, base: relation, steps: [])
|
|
129
|
+
narrowed = scope ? relation.narrowed_by(scope, what || description(relation.model_class)) : relation
|
|
130
|
+
narrowed = narrowed.spawn.inherit_rebuild(relation) if narrowed.equal?(relation) && !base.equal?(relation)
|
|
131
|
+
default = scope ? [[:scope, scope, :default]] : []
|
|
132
|
+
narrowed.plan = [base, default + steps]
|
|
133
|
+
narrowed.default_scope_applied = scope
|
|
134
|
+
narrowed
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Whether +record+ is among those +scope+ selects (a find by key).
|
|
138
|
+
def admits?(klass, record, scope)
|
|
139
|
+
return true unless scope
|
|
140
|
+
|
|
141
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) do
|
|
142
|
+
CachedRelation.new(klass, [record]).narrowed_by(scope, description(klass)).to_a.any?
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The first record matching +attributes+ that +scope+ selects, in its
|
|
147
|
+
# order. Without an order or a limit any match will do, as SQL's
|
|
148
|
+
# LIMIT 1 without ORDER BY: the store's own find_by answers, and only
|
|
149
|
+
# a match the scope leaves out sends the read to every match.
|
|
150
|
+
def find_by(klass, attributes, scope)
|
|
151
|
+
return EventStoreReader.find_by(klass, attributes) unless scope
|
|
152
|
+
|
|
153
|
+
what = description(klass)
|
|
154
|
+
# A primary key names one record: the scope's order cannot choose
|
|
155
|
+
# between matches, and the store answers by key.
|
|
156
|
+
unordered = (scope.order_values.empty? && scope.limit_value.nil?) || pins_primary_key?(klass, attributes)
|
|
157
|
+
if unordered
|
|
158
|
+
CachedRelation.new(klass, []).narrowed_by(scope, what) # refuses what it cannot evaluate, before any read
|
|
159
|
+
record = EventStoreReader.find_by(klass, attributes)
|
|
160
|
+
return record if record.nil? || admits?(klass, record, scope)
|
|
161
|
+
return nil if pins_primary_key?(klass, attributes)
|
|
162
|
+
end
|
|
163
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) do
|
|
164
|
+
EventStoreReader.where(klass, attributes).narrowed_by(scope, what).first
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The monitored +models+ whose default scope ES-NoProj cannot
|
|
169
|
+
# evaluate, with the reason, as [model, message] pairs. A scope that
|
|
170
|
+
# cannot be built here (it needs a request's context) is left to the
|
|
171
|
+
# read.
|
|
172
|
+
def unevaluable(models)
|
|
173
|
+
models.filter_map do |model|
|
|
174
|
+
next unless model.respond_to?(:default_scopes) && any?(model)
|
|
175
|
+
|
|
176
|
+
scope = begin
|
|
177
|
+
built { model.default_scoped }
|
|
178
|
+
rescue StandardError
|
|
179
|
+
next
|
|
180
|
+
end
|
|
181
|
+
begin
|
|
182
|
+
CachedRelation.new(model, []).narrowed_by(scope, description(model))
|
|
183
|
+
nil
|
|
184
|
+
rescue UnsupportedQuery => e
|
|
185
|
+
[model, e.message]
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Model.unscoped from application code (Lyra's read overrides apply)
|
|
191
|
+
# rather than from Active Record's internals or Lyra's own, which
|
|
192
|
+
# build relations on it (association scopes, uniqueness validation,
|
|
193
|
+
# counter caches) or read the table on purpose.
|
|
194
|
+
# +locations+ are the caller's frames; frames of the model's own
|
|
195
|
+
# unscoped (an application's def self.unscoped = super, a gem's
|
|
196
|
+
# override) are passed over, so that ActiveRecord's internal call
|
|
197
|
+
# through such an override still counts as ActiveRecord's.
|
|
198
|
+
def application_caller?(locations)
|
|
199
|
+
location = Array(locations).find { |frame| frame.base_label != "unscoped" }
|
|
200
|
+
path = location&.path
|
|
201
|
+
return false if path.nil?
|
|
202
|
+
|
|
203
|
+
internal_paths.none? { |prefix| path.start_with?(prefix) }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Whether +path+ is a file of ActiveRecord itself.
|
|
207
|
+
def active_record_path?(path) = path.start_with?(internal_paths.first)
|
|
208
|
+
|
|
209
|
+
def description(klass) = "the default scope of #{klass.name}"
|
|
210
|
+
|
|
211
|
+
# Runs the block past Lyra's read overrides: relations built in it
|
|
212
|
+
# are ActiveRecord's own (no SQL runs until one is loaded).
|
|
213
|
+
def built
|
|
214
|
+
previous = Thread.current[:lyra_bypass_read_override]
|
|
215
|
+
Thread.current[:lyra_bypass_read_override] = true
|
|
216
|
+
yield
|
|
217
|
+
ensure
|
|
218
|
+
Thread.current[:lyra_bypass_read_override] = previous
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
# What the association's scope unscopes, evaluated on every read as
|
|
224
|
+
# ActiveRecord evaluates it: a scope may decide at run time
|
|
225
|
+
# (-> { Current.admin? ? with_deleted : all }).
|
|
226
|
+
def association_unscopes(klass, reflection, owner)
|
|
227
|
+
reflection.scope_for(klass.unscoped, owner).unscope_values
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def pins_primary_key?(klass, attributes)
|
|
231
|
+
key = klass.primary_key
|
|
232
|
+
return false unless key.is_a?(String) && attributes.is_a?(Hash)
|
|
233
|
+
|
|
234
|
+
value = attributes[key] || attributes[key.to_sym]
|
|
235
|
+
!value.nil? && !value.is_a?(Array) && !value.is_a?(Range)
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# A relation with nothing to narrow by: no conditions, order or limit.
|
|
239
|
+
def bare?(scope)
|
|
240
|
+
scope.where_clause.empty? && scope.order_values.empty? && scope.limit_value.nil? &&
|
|
241
|
+
scope.offset_value.nil? && scope.joins_values.empty? && scope.left_outer_joins_values.empty? &&
|
|
242
|
+
!scope.distinct_value && scope.group_values.empty? && scope.from_clause.empty?
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def internal_paths
|
|
246
|
+
@internal_paths ||= [
|
|
247
|
+
"#{File.dirname(ActiveRecord::Scoping::Default::ClassMethods.instance_method(:unscoped).source_location.first, 2)}/",
|
|
248
|
+
"#{File.expand_path('..', __dir__)}/"
|
|
249
|
+
].freeze
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
@@ -19,6 +19,9 @@ module Lyra
|
|
|
19
19
|
# condition, a key already conditioned) reads the records and behaves as
|
|
20
20
|
# CachedRelation does, with the same answers.
|
|
21
21
|
class DeferredRelation < CachedRelation
|
|
22
|
+
# Its own where comes before CachedRelation's recording (Rebuildable).
|
|
23
|
+
prepend Rebuildable.wrappers
|
|
24
|
+
|
|
22
25
|
# The records, loaded on first use. Stands in for the Array a
|
|
23
26
|
# CachedRelation keeps.
|
|
24
27
|
class Records < Delegator
|
|
@@ -63,14 +66,26 @@ module Lyra
|
|
|
63
66
|
super(model_class, [])
|
|
64
67
|
@model_class = model_class
|
|
65
68
|
@records = Records.new { load_records }
|
|
69
|
+
# What the query named, for the purpose check (CachedRelation#query_terms).
|
|
70
|
+
self.query_terms = [[:conditions, conditions]]
|
|
66
71
|
scalars = conditions.select { |key, value| plain_value?(model_class, key, value) && !value.is_a?(Array) && !value.is_a?(Range) }
|
|
67
72
|
self.create_attributes = scalars.transform_keys(&:to_s)
|
|
73
|
+
# A write reads the records again (Rebuildable), by a copy of the
|
|
74
|
+
# conditions.
|
|
75
|
+
copied = Rebuildable.copy(conditions)
|
|
76
|
+
unless copied.equal?(Rebuildable::UNSAFE)
|
|
77
|
+
self.rebuild_recipe = [lambda {
|
|
78
|
+
self.class.new(model_class, conditions: Rebuildable.copy(copied, freeze: false), on_load: on_load)
|
|
79
|
+
}, []]
|
|
80
|
+
end
|
|
68
81
|
else
|
|
69
82
|
super(model_class, records || [])
|
|
70
83
|
end
|
|
71
84
|
end
|
|
72
85
|
|
|
73
|
-
|
|
86
|
+
# Whether the records have been read (loaded? is Active Record's: the
|
|
87
|
+
# application's load of them).
|
|
88
|
+
def records_read?
|
|
74
89
|
!(Records === @records) || @records.loaded?
|
|
75
90
|
end
|
|
76
91
|
|
|
@@ -81,15 +96,20 @@ module Lyra
|
|
|
81
96
|
relation = self.class.new(model_class, conditions: merged, on_load: @on_load)
|
|
82
97
|
relation.selected_columns = selected_columns
|
|
83
98
|
relation.group_columns = group_columns
|
|
99
|
+
relation.query_terms = query_terms + relation.query_terms
|
|
84
100
|
relation.extending!(*extensions) if extensions.any?
|
|
85
101
|
relation.create_attributes = create_attributes.merge(relation.create_attributes)
|
|
102
|
+
relation.default_scope_applied = default_scope_applied
|
|
103
|
+
relation.windowed = windowed?
|
|
104
|
+
relation.create_with_attributes = create_with_attributes
|
|
105
|
+
relation.continue_plan(self, [:where, args.first])
|
|
86
106
|
relation
|
|
87
107
|
end
|
|
88
108
|
|
|
89
109
|
private
|
|
90
110
|
|
|
91
111
|
def load_records
|
|
92
|
-
found = EventStoreReader.where(model_class, @conditions).
|
|
112
|
+
found = EventStoreReader.where(model_class, @conditions).raw_records.to_a
|
|
93
113
|
@on_load&.call(found)
|
|
94
114
|
found
|
|
95
115
|
end
|
|
@@ -98,7 +118,7 @@ module Lyra
|
|
|
98
118
|
# already among the pending conditions (two conditions on one key are an
|
|
99
119
|
# AND that a merge would turn into an override).
|
|
100
120
|
def mergeable?(args)
|
|
101
|
-
return false if
|
|
121
|
+
return false if records_read? || @conditions.nil? || args.size != 1
|
|
102
122
|
|
|
103
123
|
added = args.first
|
|
104
124
|
return false unless added.is_a?(Hash) && added.any?
|
|
@@ -14,7 +14,9 @@ module Lyra
|
|
|
14
14
|
#
|
|
15
15
|
# Every read first runs Genesis.first_use: on the model's first read in the
|
|
16
16
|
# process, rows that predate Lyra get their Imported events, so the answer
|
|
17
|
-
# -- aggregates included -- comes from streams alone and covers them.
|
|
17
|
+
# -- aggregates included -- comes from streams alone and covers them. A
|
|
18
|
+
# find or exists? of a row Genesis skipped (genesis_unrepresentable :skip)
|
|
19
|
+
# imports it if it has become representable, or raises Genesis::Refused.
|
|
18
20
|
class EventStoreReader
|
|
19
21
|
class << self
|
|
20
22
|
# Find a record by ID (cached)
|
|
@@ -23,7 +25,7 @@ module Lyra
|
|
|
23
25
|
# @param id [Integer, String] The record ID
|
|
24
26
|
# @return [ActiveRecord::Base, nil] The reconstructed record or nil
|
|
25
27
|
def find(model_class, id)
|
|
26
|
-
Genesis.first_use(model_class)
|
|
28
|
+
Genesis.first_use(model_class, id)
|
|
27
29
|
attributes = CachedProjection.find(model_class, id)
|
|
28
30
|
return nil unless attributes
|
|
29
31
|
|
|
@@ -43,13 +45,22 @@ module Lyra
|
|
|
43
45
|
build_instance(model_class, result)
|
|
44
46
|
end
|
|
45
47
|
|
|
48
|
+
# The live records with these ids, in the order given, as their
|
|
49
|
+
# streams have them now: the streams' last events are read afresh,
|
|
50
|
+
# and a cached entry is used only when it is stamped with that event
|
|
51
|
+
# (CachedProjection.find_many).
|
|
52
|
+
def find_many(model_class, ids)
|
|
53
|
+
Genesis.first_use(model_class)
|
|
54
|
+
CachedProjection.find_many(model_class, ids).map { |attrs| build_instance(model_class, attrs) }.compact
|
|
55
|
+
end
|
|
56
|
+
|
|
46
57
|
# Check if a record exists (cached)
|
|
47
58
|
#
|
|
48
59
|
# @param model_class [Class] The ActiveRecord model class
|
|
49
60
|
# @param id [Integer, String] The record ID
|
|
50
61
|
# @return [Boolean] True if record exists and is not destroyed
|
|
51
62
|
def exists?(model_class, id)
|
|
52
|
-
Genesis.first_use(model_class)
|
|
63
|
+
Genesis.first_use(model_class, id)
|
|
53
64
|
CachedProjection.exists?(model_class, id)
|
|
54
65
|
end
|
|
55
66
|
|
|
@@ -61,7 +72,14 @@ module Lyra
|
|
|
61
72
|
Genesis.first_use(model_class)
|
|
62
73
|
results = CachedProjection.all(model_class)
|
|
63
74
|
records = results.map { |attrs| build_instance(model_class, attrs) }.compact
|
|
64
|
-
|
|
75
|
+
# A write on it, or on a relation made from it, reads it again
|
|
76
|
+
# (Rebuildable).
|
|
77
|
+
# Not loaded (false, as Active Record's Model.all; a relation
|
|
78
|
+
# derived from it answers nil).
|
|
79
|
+
CachedRelation.new(model_class, records).tap do |relation|
|
|
80
|
+
relation.rebuild_recipe = [Rebuildable::AllRecords.new(model_class), []]
|
|
81
|
+
relation.instance_variable_set(:@loaded_records, false)
|
|
82
|
+
end
|
|
65
83
|
end
|
|
66
84
|
|
|
67
85
|
# Load all records of a given type (cached)
|
|
@@ -92,7 +110,24 @@ module Lyra
|
|
|
92
110
|
return nil unless candidates
|
|
93
111
|
|
|
94
112
|
records = candidates.map { |attrs| build_instance(model_class, attrs) }.compact
|
|
95
|
-
CachedRelation.new(model_class, records).where(conditions)
|
|
113
|
+
found = CachedRelation.new(model_class, records).where(conditions)
|
|
114
|
+
# Read again by the same lookup (Rebuildable), with the conditions
|
|
115
|
+
# as they are now: a copy, which the caller's changes do not reach.
|
|
116
|
+
copied = Rebuildable.copy(conditions)
|
|
117
|
+
unless copied.equal?(Rebuildable::UNSAFE)
|
|
118
|
+
found.rebuild_recipe = [-> { where(model_class, Rebuildable.copy(copied, freeze: false)) }, []]
|
|
119
|
+
end
|
|
120
|
+
found
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The live records with these primary keys, as a relation that a
|
|
124
|
+
# write reads again the same way (Rebuildable::Keyed): its cost is
|
|
125
|
+
# that of the records, not of the model's size.
|
|
126
|
+
def keyed(model_class, ids)
|
|
127
|
+
ids = Array(ids).uniq
|
|
128
|
+
CachedRelation.new(model_class, find_many(model_class, ids)).tap do |found|
|
|
129
|
+
found.rebuild_recipe = [Rebuildable::Keyed.new(model_class, ids.map { Rebuildable.copy(_1) }.freeze), []]
|
|
130
|
+
end
|
|
96
131
|
end
|
|
97
132
|
|
|
98
133
|
# Count records (cached)
|
|
@@ -129,20 +164,24 @@ module Lyra
|
|
|
129
164
|
|
|
130
165
|
# Filter to only known columns
|
|
131
166
|
column_names = model_class.column_names
|
|
167
|
+
# (CachedProjection's entries hold them as the table does.)
|
|
132
168
|
filtered_attrs = attrs.slice(*column_names)
|
|
133
169
|
|
|
134
170
|
# Use AR's instantiate method - proper way to build from DB-style attributes
|
|
135
171
|
# Lyra's own reconstruction: the purpose check is made on the records
|
|
136
172
|
# a query returns (CachedRelationDelivery), not on every one built.
|
|
137
|
-
record = Lyra::PurposeBoundReads.internal { model_class.instantiate(filtered_attrs) }
|
|
173
|
+
record = Lyra::PurposeBoundReads.internal(INTERNAL) { model_class.instantiate(filtered_attrs) }
|
|
138
174
|
|
|
139
175
|
# Note: We don't mark as readonly because the app may need to modify
|
|
140
176
|
# and re-save (which will go through event sourcing)
|
|
141
177
|
|
|
142
178
|
record
|
|
143
179
|
rescue => e
|
|
144
|
-
|
|
145
|
-
|
|
180
|
+
# Fail closed: a record that cannot be built from its events (a
|
|
181
|
+
# value its type cannot read) is an error, not a row left out of
|
|
182
|
+
# the answer, which it used to be.
|
|
183
|
+
Rails.logger.error("Lyra::EventStoreReader: cannot build #{model_class.name} from its events - #{e.class}: #{e.message}")
|
|
184
|
+
raise
|
|
146
185
|
end
|
|
147
186
|
end
|
|
148
187
|
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lyra
|
|
4
|
+
module Projections
|
|
5
|
+
# Database foreign keys ES-NoProj cannot honour.
|
|
6
|
+
#
|
|
7
|
+
# ES-NoProj (event sourcing, projections disabled) leaves the table of
|
|
8
|
+
# every monitored model empty: its records live in the event log. A
|
|
9
|
+
# database foreign key from a table no monitored model writes into such a
|
|
10
|
+
# table therefore refuses every write there that references a record
|
|
11
|
+
# (PG::ForeignKeyViolation, ActiveRecord::InvalidForeignKey): the row it
|
|
12
|
+
# points at is never inserted. ModeTransition refuses the switch into
|
|
13
|
+
# ES-NoProj while one exists, and the boot warns of it.
|
|
14
|
+
#
|
|
15
|
+
# The other foreign keys never fire in ES-NoProj: one between two
|
|
16
|
+
# monitored tables, or from a monitored table into an unmonitored one,
|
|
17
|
+
# sits on a table that gets no rows.
|
|
18
|
+
#
|
|
19
|
+
# One query per table of each database the monitored models use
|
|
20
|
+
# (connection.foreign_keys), at the switch and at boot only.
|
|
21
|
+
module ForeignKeys
|
|
22
|
+
Blocking = Struct.new(:from_table, :column, :to_table, :name, :referencing_models, keyword_init: true) do
|
|
23
|
+
def to_s
|
|
24
|
+
by = referencing_models.empty? ? "" : ", written by #{referencing_models.join(', ')}"
|
|
25
|
+
"#{from_table}.#{column} -> #{to_table} (constraint #{name}#{by})"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
# The foreign keys from a table no model in +models+ writes into one a
|
|
31
|
+
# model in +models+ writes, in each database those models use.
|
|
32
|
+
def blocking(models)
|
|
33
|
+
tabled = models.select { |model| table_of(model) }
|
|
34
|
+
tabled.group_by(&:connection_pool).flat_map do |pool, in_pool|
|
|
35
|
+
monitored = in_pool.map { |model| table_of(model) }.uniq
|
|
36
|
+
pool.with_connection { |connection| blocking_in(connection, monitored) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# How to fix +blocking+, for the refusal and the boot warning.
|
|
41
|
+
def remedy(blocking)
|
|
42
|
+
drops = blocking.map { |fk| "remove_foreign_key :#{fk.from_table}, name: #{fk.name.to_s.inspect}" }.uniq
|
|
43
|
+
referencing = blocking.flat_map(&:referencing_models).uniq
|
|
44
|
+
monitor = "monitor the referencing model#{" (#{referencing.join(', ')})" unless referencing.empty?}"
|
|
45
|
+
"Drop the constraint (#{drops.join('; ')}) and keep the reference in the model (belongs_to " \
|
|
46
|
+
"validates that the record exists), or #{monitor}, or use another projection mode."
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def description(blocking)
|
|
50
|
+
"ES-NoProj keeps the records of monitored models in the event store and leaves their tables empty, " \
|
|
51
|
+
"so a database foreign key into one of those tables from a table no monitored model writes refuses " \
|
|
52
|
+
"every write there that references a record: #{blocking.map(&:to_s).join('; ')}."
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def blocking_in(connection, monitored)
|
|
58
|
+
return [] unless connection.supports_foreign_keys?
|
|
59
|
+
|
|
60
|
+
known = monitored.flat_map { |table| [table, unqualified(table)] }.uniq
|
|
61
|
+
(connection.tables - known).flat_map do |table|
|
|
62
|
+
connection.foreign_keys(table).filter_map do |fk|
|
|
63
|
+
to_table = fk.to_table.to_s
|
|
64
|
+
next unless known.include?(to_table) || known.include?(unqualified(to_table))
|
|
65
|
+
|
|
66
|
+
Blocking.new(from_table: fk.from_table.to_s, column: Array(fk.column).join(", "), to_table: to_table,
|
|
67
|
+
name: fk.name.to_s, referencing_models: models_of(fk.from_table.to_s))
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def table_of(model)
|
|
73
|
+
return nil if !model.respond_to?(:table_name) || (model.respond_to?(:abstract_class?) && model.abstract_class?)
|
|
74
|
+
|
|
75
|
+
model.table_name.presence
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def unqualified(table) = table.to_s.split(".").last
|
|
79
|
+
|
|
80
|
+
# The loaded models that write +table+ (none of them monitored).
|
|
81
|
+
def models_of(table)
|
|
82
|
+
ActiveRecord::Base.descendants.filter_map do |model|
|
|
83
|
+
next if model.abstract_class? || model.name.nil?
|
|
84
|
+
next unless (model.table_name rescue nil) == table
|
|
85
|
+
|
|
86
|
+
model.base_class.name
|
|
87
|
+
end.uniq.sort
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|