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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2021 -17
  3. data/README.md +22 -9
  4. data/Rakefile +40 -1
  5. data/app/controllers/lyra/application_controller.rb +127 -0
  6. data/app/controllers/lyra/dashboard_controller.rb +33 -31
  7. data/app/controllers/lyra/flow_controller.rb +32 -34
  8. data/app/controllers/lyra/privacy_controller.rb +57 -18
  9. data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
  10. data/app/views/lyra/dashboard/index.html.erb +1 -0
  11. data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
  12. data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
  13. data/app/views/lyra/privacy/policy.html.erb +7 -0
  14. data/app/views/lyra/privacy/rights.html.erb +78 -0
  15. data/config/privacy_policies.rb +0 -3
  16. data/config/routes.rb +1 -0
  17. data/lib/lyra/access_log.rb +187 -6
  18. data/lib/lyra/advisory_lock.rb +236 -0
  19. data/lib/lyra/aggregate.rb +1 -1
  20. data/lib/lyra/associations/habtm_links.rb +382 -19
  21. data/lib/lyra/associations/maintenance.rb +229 -0
  22. data/lib/lyra/attribution.rb +48 -0
  23. data/lib/lyra/bypass_events.rb +446 -46
  24. data/lib/lyra/command_handler.rb +19 -14
  25. data/lib/lyra/configuration.rb +237 -10
  26. data/lib/lyra/consistency/read_your_writes.rb +4 -2
  27. data/lib/lyra/correlation.rb +121 -0
  28. data/lib/lyra/domain_events.rb +4 -0
  29. data/lib/lyra/dual_view.rb +53 -16
  30. data/lib/lyra/encrypted_attributes.rb +5 -0
  31. data/lib/lyra/engine.rb +67 -0
  32. data/lib/lyra/erasure.rb +1011 -106
  33. data/lib/lyra/event.rb +35 -1
  34. data/lib/lyra/event_data.rb +38 -0
  35. data/lib/lyra/event_flow.rb +31 -15
  36. data/lib/lyra/event_store_adapter.rb +28 -0
  37. data/lib/lyra/event_window.rb +58 -0
  38. data/lib/lyra/genesis.rb +548 -44
  39. data/lib/lyra/interceptors/association_interceptor.rb +689 -34
  40. data/lib/lyra/interceptors/crud_interceptor.rb +578 -91
  41. data/lib/lyra/internal.rb +26 -0
  42. data/lib/lyra/mode_sync.rb +51 -3
  43. data/lib/lyra/mode_transition.rb +257 -30
  44. data/lib/lyra/model_selection.rb +35 -0
  45. data/lib/lyra/own_connection.rb +17 -2
  46. data/lib/lyra/privacy/adapters/pam.rb +25 -2
  47. data/lib/lyra/privacy/gdpr_compliance.rb +314 -48
  48. data/lib/lyra/privacy/interface.rb +27 -7
  49. data/lib/lyra/privacy/subject_links.rb +143 -0
  50. data/lib/lyra/privacy/subject_records.rb +369 -0
  51. data/lib/lyra/projection.rb +7 -1
  52. data/lib/lyra/projections/async_checkpoint.rb +32 -0
  53. data/lib/lyra/projections/async_projection_job.rb +166 -23
  54. data/lib/lyra/projections/cached_joins.rb +33 -18
  55. data/lib/lyra/projections/cached_projection.rb +74 -33
  56. data/lib/lyra/projections/cached_relation.rb +3175 -321
  57. data/lib/lyra/projections/default_scopes.rb +254 -0
  58. data/lib/lyra/projections/deferred_relation.rb +23 -3
  59. data/lib/lyra/projections/event_store_reader.rb +47 -8
  60. data/lib/lyra/projections/foreign_keys.rb +92 -0
  61. data/lib/lyra/projections/lazy_projection.rb +165 -27
  62. data/lib/lyra/projections/model_projection.rb +165 -14
  63. data/lib/lyra/projections/rebuild.rb +34 -6
  64. data/lib/lyra/projections/reference_links.rb +463 -72
  65. data/lib/lyra/projections/typed_values.rb +629 -0
  66. data/lib/lyra/projections/unique_indexes.rb +595 -0
  67. data/lib/lyra/projections/uniqueness.rb +784 -0
  68. data/lib/lyra/projections/value_digest.rb +93 -0
  69. data/lib/lyra/purpose_bound_reads.rb +912 -77
  70. data/lib/lyra/repair.rb +119 -28
  71. data/lib/lyra/retention.rb +114 -8
  72. data/lib/lyra/rights/consent_store.rb +415 -0
  73. data/lib/lyra/rights/erasure_guard.rb +135 -0
  74. data/lib/lyra/rights/pending.rb +74 -0
  75. data/lib/lyra/rights/queries.rb +269 -0
  76. data/lib/lyra/rights/store.rb +512 -0
  77. data/lib/lyra/rights/subject_keys.rb +224 -0
  78. data/lib/lyra/rights.rb +281 -0
  79. data/lib/lyra/saves_without_effect.rb +69 -0
  80. data/lib/lyra/stream_tail.rb +319 -0
  81. data/lib/lyra/strict_data_access.rb +916 -106
  82. data/lib/lyra/temporal.rb +4 -2
  83. data/lib/lyra/touch_registry.rb +404 -0
  84. data/lib/lyra/verification/bypass_workflow.rb +452 -42
  85. data/lib/lyra/verification/rights_workflows.rb +170 -0
  86. data/lib/lyra/version.rb +1 -1
  87. data/lib/lyra/visualization/event_graph.rb +1 -1
  88. data/lib/lyra/visualization/timeline.rb +2 -2
  89. data/lib/lyra.rb +43 -2
  90. data/lib/tasks/lyra_mode.rake +110 -8
  91. data/lib/tasks/lyra_projections.rake +43 -2
  92. metadata +34 -4
  93. data/lib/lyra/associations/event_aware.rb +0 -224
@@ -0,0 +1,512 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Lyra
6
+ module Rights
7
+ # PamDsl.rights_store, durable: the PamDsl::RightsStore protocol on Lyra's
8
+ # event store.
9
+ #
10
+ # Events. A subject's rights events go to Lyra::Rights$<subject key>, in
11
+ # the order appended, each linked to the index stream Lyra::Rights.
12
+ # append(events, expected_version:) folds the subject's events with the
13
+ # new ones first (PamDsl::Rights.project, which refuses an illegal
14
+ # transition), so nothing is stored that does not replay; then it appends
15
+ # with RubyEventStore's expected version on the subject's stream (the
16
+ # stream's last position), links, and rewrites the subject's rows in
17
+ # lyra_rights, in one transaction. Two processes that read the same
18
+ # version and both try to record (lift, decide) cannot both succeed: the
19
+ # second insert of the same stream position violates the stream's unique
20
+ # index, after waiting for the first to commit, and gets
21
+ # PamDsl::RightsConcurrencyError, with nothing stored.
22
+ #
23
+ # Projection. lyra_rights holds one row per restriction or objection: its
24
+ # subject key (and type and id), kind, policy, purpose, fields, ground,
25
+ # article, state, whether it is in force, its times, and the subject
26
+ # stream's version and last event id after the append that wrote it.
27
+ #
28
+ # Snapshot. Condition 6 asks any_active? and active_for(keys) on every
29
+ # access with a subject, so they answer from a snapshot of the committed
30
+ # requests in force, shared by the process's threads: one indexed query
31
+ # (the subject keys in force, with their stream version and last event id)
32
+ # refreshes it, and only a subject whose last event changed is read again.
33
+ # An event id is never reused, as a stream version is after a rollback.
34
+ # The snapshot is refreshed when it is older than
35
+ # config.rights_refresh_interval seconds (default 1), when it was read
36
+ # before the current checking scope began (Lyra::Rights.scope: a purpose
37
+ # block, a request, a job, a batch), and on Lyra::Rights.refresh!. A
38
+ # transaction that recorded a right sees its own (the keys it wrote are
39
+ # read afresh through its connection, see Rights::Pending) and never
40
+ # refreshes the shared snapshot; when it commits or rolls back, its keys
41
+ # leave the snapshot and the next check reads them again. A right recorded
42
+ # in another process applies here from the next refresh, at the latest
43
+ # from the next checking scope. When nothing is in force the check costs a
44
+ # clock read, and a query at most once per interval and scope. active
45
+ # (open requests, reports) always refreshes first.
46
+ #
47
+ # Rebuild (Lyra::Rights.rebuild!) locks lyra_rights against appends while
48
+ # it rewrites it (on PostgreSQL), so a transition waits for it rather than
49
+ # failing or losing its row.
50
+ class Store
51
+ include PamDsl::RightsStore::Base
52
+ include Pending
53
+
54
+ # Times are kept with microseconds, the resolution of the columns.
55
+ COLUMNS = %w[request_id kind subject_key subject_type subject_id policy purpose fields ground article
56
+ decision_grounds objection_id restriction_id state in_force requested_at recorded_at
57
+ lift_notified_at lifted_at decided_at withdrawn_at stream_version last_event_id updated_at].freeze
58
+
59
+ attr_reader :generation
60
+
61
+ def initialize(event_store: nil)
62
+ @event_store = event_store
63
+ @mutex = Mutex.new
64
+ @snapshot = {}.freeze
65
+ @versions = {}.freeze
66
+ @checked_at = nil
67
+ @overlays = {}.freeze
68
+ @overlays_generation = 0
69
+ @generation = 0
70
+ @table_ready = false
71
+ init_pending
72
+ end
73
+
74
+ def event_store
75
+ @event_store || Lyra.config.event_store
76
+ end
77
+
78
+ # Kept in the database, seen by every process.
79
+ def durable?
80
+ true
81
+ end
82
+
83
+ # ── The protocol ──
84
+
85
+ def append(events, expected_version:)
86
+ events = Array(events)
87
+ raise ArgumentError, "append takes at least one event" if events.empty?
88
+
89
+ key = single_subject_key(events)
90
+ existing = events_for(key)
91
+ refuse_stale!(key, expected_version, existing.size) unless existing.size == expected_version
92
+
93
+ # Fold first: an illegal transition raises here, and nothing is stored.
94
+ all = existing + events
95
+ requests = PamDsl::Rights.project(all)
96
+ ensure_table!
97
+
98
+ ActiveRecord::Base.transaction(requires_new: true) do
99
+ records = events.map { |event| to_record(event) }
100
+ event_store.append(records, stream_name: Rights.stream_for(key),
101
+ expected_version: expected_version.zero? ? :none : expected_version - 1)
102
+ event_store.link(records.map(&:event_id), stream_name: STREAM)
103
+ write_rows(key, requests, all.size, records.last.event_id)
104
+ end
105
+ changed!([key])
106
+ events
107
+ rescue RubyEventStore::WrongExpectedEventVersion, ActiveRecord::RecordNotUnique
108
+ refuse_stale!(key, expected_version, nil)
109
+ end
110
+
111
+ def events_for(subject_key)
112
+ read_stream(subject_key).map { |record| from_record(record) }
113
+ end
114
+
115
+ def subject_key_for(request_id)
116
+ if table_ready?
117
+ key = with_connection do |c|
118
+ c.select_value("SELECT subject_key FROM #{TABLE} WHERE request_id = #{c.quote(request_id.to_s)}")
119
+ end
120
+ return key if key
121
+ end
122
+ # Not in the table (being rebuilt, or written before it): the index.
123
+ event_store.read.stream(STREAM).to_a.reverse_each do |record|
124
+ data = record_data(record)
125
+ return data["subject_key"] if data["request_id"] == request_id.to_s
126
+ end
127
+ nil
128
+ end
129
+
130
+ # Every request in force, read afresh.
131
+ def active
132
+ refresh!
133
+ pending = pending_here
134
+ current = snapshot_here(pending)
135
+ current = current.merge(pending.to_h { |key| [key, load_in_force(key)] }) if pending
136
+ current.values.flatten
137
+ end
138
+
139
+ def active_for(keys)
140
+ pending = refresh_if_due
141
+ snapshot = snapshot_here(pending)
142
+ Array(keys).flat_map do |key|
143
+ key = key.to_s
144
+ pending&.include?(key) ? load_in_force(key) : snapshot.fetch(key, [])
145
+ end
146
+ end
147
+
148
+ def any_active?
149
+ refresh_if_due
150
+ !@snapshot.empty? || !pending_here.nil?
151
+ end
152
+
153
+ # Every request in force, from the snapshot (and, read afresh, those
154
+ # this connection's open transaction wrote): what Condition 6 asks
155
+ # before it resolves a subject's keys (PamDsl::Rights.could_apply?).
156
+ def in_force_requests
157
+ pending = refresh_if_due
158
+ current = snapshot_here(pending)
159
+ current = current.merge(pending.to_h { |key| [key, load_in_force(key)] }) if pending
160
+ current.values.flatten
161
+ end
162
+
163
+ # An object that changes whenever the snapshot's content does, or nil
164
+ # while this connection's transaction has rights of its own: callers
165
+ # remember what they derived from the rights against it
166
+ # (Rights::Queries).
167
+ def snapshot_token
168
+ refresh_if_due
169
+ pending_here ? nil : @snapshot
170
+ end
171
+
172
+ # The subject keys with a request in force (the snapshot's, and those
173
+ # this connection's open transaction wrote), for model-wide queries.
174
+ def keys_in_force
175
+ pending = refresh_if_due
176
+ keys = snapshot_here(pending).keys
177
+ return keys unless pending
178
+
179
+ # A pending key is in force as this transaction left it, not as the
180
+ # snapshot holds it (a restriction it lifted is not).
181
+ (keys - pending.to_a) + pending.select { |key| load_in_force(key).any? }
182
+ end
183
+
184
+ # Which of +keys+ have any request recorded (in force or not): one
185
+ # query, for the subject's copy.
186
+ def keys_with_requests(keys)
187
+ keys = Array(keys).map(&:to_s).uniq
188
+ return [] if keys.empty? || !table_ready?
189
+
190
+ with_connection do |c|
191
+ keys.each_slice(1000).flat_map do |slice|
192
+ c.select_values("SELECT DISTINCT subject_key FROM #{TABLE} WHERE subject_key IN " \
193
+ "(#{slice.map { |key| c.quote(key) }.join(', ')})")
194
+ end
195
+ end
196
+ end
197
+
198
+ # ── The snapshot ──
199
+
200
+ # Read the committed requests in force again. From a connection whose
201
+ # open transaction recorded rights, into a snapshot of that
202
+ # connection's own (refresh_around): what it reads may be its own
203
+ # uncommitted rights, or, under REPEATABLE READ or SERIALIZABLE, the
204
+ # database as its transaction began, and neither may reach the
205
+ # snapshot every thread reads.
206
+ def refresh!
207
+ pending = pending_here
208
+ return refresh_around(pending) if pending
209
+
210
+ @mutex.synchronize do
211
+ # One row first: how many requests are in force and when the last
212
+ # of them was written. Unchanged, the snapshot stands without
213
+ # reading every subject's marker (hundreds of rows per scope).
214
+ digest = in_force_digest
215
+ if digest && digest == @digest
216
+ @checked_at = Rights.now
217
+ next
218
+ end
219
+
220
+ markers = in_force_markers
221
+ @digest = digest
222
+ if markers == @versions
223
+ # Nothing changed: the same snapshot object, so what was derived
224
+ # from it (snapshot_token) still holds.
225
+ @checked_at = Rights.now
226
+ next
227
+ end
228
+
229
+ snapshot = {}
230
+ markers.each do |key, marker|
231
+ cached = @snapshot[key]
232
+ in_force = cached && @versions[key] == marker ? cached : load_in_force(key)
233
+ snapshot[key] = in_force.freeze unless in_force.empty?
234
+ end
235
+ @snapshot = snapshot.freeze
236
+ @versions = markers.freeze
237
+ @checked_at = Rights.now
238
+ end
239
+ self
240
+ end
241
+
242
+ # The next check reads the table again.
243
+ def invalidate!
244
+ @checked_at = nil
245
+ @mutex.synchronize do
246
+ @overlays = {}.freeze
247
+ @overlays_generation += 1
248
+ end
249
+ end
250
+
251
+ # ── Rebuild ──
252
+
253
+ # Rewrites lyra_rights from the index stream. Returns the rows written.
254
+ def rebuild!
255
+ ensure_table!
256
+ written = 0
257
+ keys = []
258
+ ActiveRecord::Base.transaction(requires_new: true) do
259
+ # The lock first, then the index: an append either committed before
260
+ # it (and is read here) or writes its rows after the rebuild commits.
261
+ with_connection { |c| Rights.lock_table(c, TABLE) }
262
+ keys = event_store.read.stream(STREAM).to_a.map { |record| record_data(record)["subject_key"] }.uniq
263
+ with_connection { |c| c.execute("DELETE FROM #{TABLE}") }
264
+ keys.each do |key|
265
+ records = read_stream(key)
266
+ requests = PamDsl::Rights.project(records.map { |record| from_record(record) })
267
+ write_rows(key, requests, records.size, records.last&.event_id)
268
+ written += requests.size
269
+ end
270
+ end
271
+ changed!(keys)
272
+ written
273
+ end
274
+
275
+ private
276
+
277
+ # Refreshes what this connection answers from when it is due, and
278
+ # returns the keys its open transaction wrote (pending_here), or nil.
279
+ # A connection without rights of its own refreshes the shared snapshot,
280
+ # as before; one with them, its own (refresh_around), on the same
281
+ # schedule.
282
+ def refresh_if_due
283
+ pending = pending_here
284
+ if pending
285
+ refresh_around(pending) if !Rights.current?(@overlays[current_connection]&.last)
286
+ elsif !Rights.current?(@checked_at)
287
+ refresh!
288
+ end
289
+ pending
290
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
291
+ pending
292
+ end
293
+
294
+ # What this connection answers from: the shared snapshot, or, while its
295
+ # open transaction has rights of its own (+pending+), the snapshot
296
+ # refresh_around read for it.
297
+ def snapshot_here(pending)
298
+ return @snapshot unless pending
299
+
300
+ @overlays[current_connection]&.first || @snapshot
301
+ end
302
+
303
+ # refresh! from a connection whose open transaction recorded rights for
304
+ # +pending+ keys: the requests in force of every other subject, as
305
+ # this connection reads them, into a snapshot used by this connection
306
+ # only (snapshot_here), until its transaction ends (settled clears it)
307
+ # or it is due again. Its pending keys are read afresh by the readers.
308
+ # The shared snapshot is not written: under READ COMMITTED this
309
+ # connection reads what others committed, but under REPEATABLE READ or
310
+ # SERIALIZABLE it reads the database as its transaction began, and
311
+ # another thread must not lose a restriction committed since. It used
312
+ # to return at once, and a snapshot another write had just left
313
+ # without a subject (settled) stayed so until the transaction ended: a
314
+ # restriction committed before it began was not in force inside it.
315
+ #
316
+ # The rows are read outside the mutex. A write settled meanwhile (by
317
+ # another thread: invalidate! moves @overlays_generation) may be
318
+ # missing from what was read, so the read is made again; a read that
319
+ # still raced is used for this check only (no read time: the next
320
+ # check reads again), never kept as fresh.
321
+ def refresh_around(pending, attempts = OVERLAY_READS)
322
+ generation = @overlays_generation
323
+ shared = @snapshot
324
+ versions = @versions
325
+ overlay = {}
326
+ in_force_markers.each do |key, marker|
327
+ next if pending.include?(key)
328
+
329
+ cached = shared[key]
330
+ in_force = cached && versions[key] == marker ? cached : load_in_force(key)
331
+ overlay[key] = in_force.freeze unless in_force.empty?
332
+ end
333
+ connection = current_connection
334
+ stored = @mutex.synchronize do
335
+ raced = @overlays_generation != generation
336
+ next false if raced && attempts > 1
337
+
338
+ @overlays = @overlays.merge(connection => [overlay.freeze, raced ? nil : Rights.now]).freeze
339
+ true
340
+ end
341
+ stored ? self : refresh_around(pending, attempts - 1)
342
+ end
343
+
344
+ # How many times refresh_around reads before it settles for a read
345
+ # that raced with a write.
346
+ OVERLAY_READS = 3
347
+
348
+ def load_in_force(key)
349
+ PamDsl::Rights.project(events_for(key)).select(&:in_force?)
350
+ end
351
+
352
+ # Lyra's own read (not checked for a purpose: a check would ask this
353
+ # store again, from inside its refresh).
354
+ def read_stream(key)
355
+ PurposeBoundReads.internal(INTERNAL) { event_store.read.stream(Rights.stream_for(key)).to_a }
356
+ end
357
+
358
+ # { subject key => [stream version, last event id] } for the keys with
359
+ # a request in force.
360
+ def in_force_digest
361
+ return [0, nil] unless table_ready?
362
+
363
+ with_connection do |c|
364
+ c.select_rows("SELECT COUNT(*), MAX(updated_at), SUM(stream_version) FROM #{TABLE} " \
365
+ "WHERE in_force = #{c.quoted_true}").first&.then { |count, at, versions| [count.to_i, at.to_s, versions.to_i] }
366
+ end
367
+ end
368
+
369
+ def in_force_markers
370
+ return {} unless table_ready?
371
+
372
+ with_connection do |c|
373
+ c.select_rows("SELECT subject_key, MAX(stream_version), MAX(last_event_id) FROM #{TABLE} " \
374
+ "WHERE in_force = #{c.quoted_true} GROUP BY subject_key")
375
+ .to_h { |key, version, event_id| [key, [version.to_i, event_id]] }
376
+ end
377
+ end
378
+
379
+ # A change to +keys+ by this thread: read through its connection until
380
+ # its transaction ends, then out of the shared snapshot.
381
+ def changed!(keys)
382
+ @mutex.synchronize { @generation += 1 }
383
+ Rights.note_write!
384
+ mark_pending(keys)
385
+ end
386
+
387
+ # +keys+ committed or rolled back: drop them from the shared snapshot,
388
+ # so the next check reads them from the database.
389
+ def settled(keys)
390
+ @mutex.synchronize do
391
+ @snapshot = @snapshot.except(*keys).freeze
392
+ @versions = @versions.except(*keys).freeze
393
+ @digest = nil
394
+ end
395
+ invalidate!
396
+ end
397
+
398
+ def refuse_stale!(key, expected, found)
399
+ found_text = found.nil? ? "another process recorded first" : "found #{found}"
400
+ raise PamDsl::RightsConcurrencyError,
401
+ "The rights of #{key} changed while this request was handled (expected version #{expected}, " \
402
+ "#{found_text}): read them again and retry"
403
+ end
404
+
405
+ def single_subject_key(events)
406
+ keys = events.map(&:subject_key).uniq
407
+ raise ArgumentError, "append takes events for exactly one subject key, got #{keys.inspect}" unless keys.size == 1
408
+
409
+ keys.first.to_s
410
+ end
411
+
412
+ def to_record(event)
413
+ Events.const_get(event.type).new(
414
+ data: {
415
+ "request_id" => event.request_id.to_s,
416
+ "subject_key" => event.subject_key.to_s,
417
+ "at" => Codec.encode(event.at),
418
+ "recorded_at" => Codec.encode(event.recorded_at),
419
+ "recorded_by" => event.recorded_by&.to_s,
420
+ "data" => Codec.encode(event.data.to_h)
421
+ },
422
+ metadata: { source: "lyra_rights", correlation_id: Lyra::Correlation.current_id }.compact
423
+ )
424
+ end
425
+
426
+ def from_record(record)
427
+ data = record_data(record)
428
+ PamDsl::Rights::Event.new(
429
+ type: record.class.name.demodulize, request_id: data["request_id"], subject_key: data["subject_key"],
430
+ data: (Codec.decode(data["data"]) || {}).freeze, at: Codec.decode(data["at"]),
431
+ recorded_at: Codec.decode(data["recorded_at"]), recorded_by: data["recorded_by"]
432
+ )
433
+ end
434
+
435
+ def record_data(record)
436
+ record.data.to_h.transform_keys(&:to_s)
437
+ end
438
+
439
+ # Rewrite the subject's rows: few per subject, and serialised by the
440
+ # append's lock on the subject's stream position.
441
+ def write_rows(key, requests, version, last_event_id)
442
+ type, id = Rights.split_key(key)
443
+ now = Time.current
444
+ with_connection do |c|
445
+ c.execute("DELETE FROM #{TABLE} WHERE subject_key = #{c.quote(key)}")
446
+ requests.each do |r|
447
+ restriction = r.kind == :restriction
448
+ values = [
449
+ r.id, r.kind.to_s, key, type, id, r.policy&.to_s, r.purpose&.to_s,
450
+ restriction ? JSON.generate(r.fields.map(&:to_s)) : nil,
451
+ restriction ? r.ground&.to_s : nil, r.article, restriction ? nil : r.decision_grounds&.to_s,
452
+ restriction ? r.objection_id : nil, restriction ? nil : r.restriction_id,
453
+ r.state.to_s, r.in_force?, r.requested_at, r.recorded_at,
454
+ restriction ? r.lift_notified_at : nil, restriction ? r.lifted_at : nil,
455
+ restriction ? nil : r.decided_at, restriction ? nil : r.withdrawn_at, version, last_event_id, now
456
+ ]
457
+ c.execute("INSERT INTO #{TABLE} (#{COLUMNS.join(', ')}) VALUES (#{values.map { |v| Rights.quote(c, v) }.join(', ')})")
458
+ end
459
+ end
460
+ end
461
+
462
+ def with_connection(&block)
463
+ ActiveRecord::Base.with_connection(&block)
464
+ end
465
+
466
+ def table_ready?
467
+ return true if @table_ready
468
+
469
+ @table_ready = Rights.table_exists?(TABLE)
470
+ end
471
+
472
+ def ensure_table!
473
+ return if @table_ready && table_ready?
474
+
475
+ ready = Rights.with_table(TABLE) do |c|
476
+ c.create_table(TABLE) do |t|
477
+ t.string :request_id, limit: 36, null: false
478
+ t.string :kind, null: false
479
+ t.string :subject_key, null: false
480
+ t.string :subject_type
481
+ t.string :subject_id
482
+ t.string :policy
483
+ t.string :purpose
484
+ t.text :fields
485
+ t.string :ground
486
+ t.string :article
487
+ t.string :decision_grounds
488
+ t.string :objection_id, limit: 36
489
+ t.string :restriction_id, limit: 36
490
+ t.string :state, null: false
491
+ t.boolean :in_force, null: false, default: false
492
+ t.datetime :requested_at, precision: 6
493
+ t.datetime :recorded_at, precision: 6
494
+ t.datetime :lift_notified_at, precision: 6
495
+ t.datetime :lifted_at, precision: 6
496
+ t.datetime :decided_at, precision: 6
497
+ t.datetime :withdrawn_at, precision: 6
498
+ t.integer :stream_version, null: false
499
+ t.string :last_event_id, limit: 36
500
+ t.datetime :updated_at, precision: 6, null: false
501
+ end
502
+ c.add_index TABLE, :request_id, unique: true
503
+ c.add_index TABLE, %i[in_force subject_key]
504
+ c.add_index TABLE, :subject_key
505
+ end
506
+ raise ActiveRecord::ConnectionNotEstablished, "no database for #{TABLE}" unless ready
507
+
508
+ @table_ready = true
509
+ end
510
+ end
511
+ end
512
+ end