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
data/lib/lyra/repair.rb
CHANGED
|
@@ -30,10 +30,32 @@ module Lyra
|
|
|
30
30
|
|
|
31
31
|
class Refused < StandardError; end
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
# Records whose repair event would hold a value their model cannot
|
|
34
|
+
# represent (Lyra::Genesis's check): nothing was repaired. +rows+: how many
|
|
35
|
+
# records; +values+: the first Genesis::LISTED Lyra::Genesis::Unrepresentable
|
|
36
|
+
# of the +found+ in all.
|
|
37
|
+
class Unrepresentable < Refused
|
|
38
|
+
attr_reader :rows, :values, :found
|
|
39
|
+
|
|
40
|
+
def initialize(rows:, values:, found:)
|
|
41
|
+
@rows = rows
|
|
42
|
+
@values = values
|
|
43
|
+
@found = found
|
|
44
|
+
super("lyra:repair will not write events for #{rows} record(s) whose rows hold values their model cannot " \
|
|
45
|
+
"represent, so the events would not match them; nothing was repaired. Fix the rows or the models; or " \
|
|
46
|
+
"skip those records (config.genesis_unrepresentable = :skip, or UNREPRESENTABLE=skip for lyra:repair). " \
|
|
47
|
+
"#{values.map(&:to_s).join('; ')}#{" (and #{found - values.size} more)" if found > values.size}")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# +unrepresentable+: the first Genesis::LISTED values the model cannot
|
|
52
|
+
# represent, of the +skipped+ records left out of line (with :skip) or
|
|
53
|
+
# that would be (a dry run).
|
|
54
|
+
Result = Struct.new(:checked, :found, :repaired, :remaining, :skipped, :unrepresentable, keyword_init: true) do
|
|
34
55
|
def summary
|
|
35
56
|
"checked #{checked} records: #{found.size} out of line, #{repaired.size} repaired, " \
|
|
36
|
-
"#{remaining.size} still out of line"
|
|
57
|
+
"#{remaining.size} still out of line" \
|
|
58
|
+
"#{", #{skipped} with values their model cannot represent (not repaired)" if skipped.to_i.positive?}"
|
|
37
59
|
end
|
|
38
60
|
end
|
|
39
61
|
|
|
@@ -41,7 +63,18 @@ module Lyra
|
|
|
41
63
|
# Find, and unless +dry_run+, repair every out-of-line record of
|
|
42
64
|
# +models+. Returns a Result whose found, repaired and remaining hold
|
|
43
65
|
# ModeTransition::Discrepancy values.
|
|
44
|
-
|
|
66
|
+
#
|
|
67
|
+
# A repair writes the row as its model reads it. A record whose event
|
|
68
|
+
# would hold a value the model cannot represent (an enum value outside
|
|
69
|
+
# the mapping, read as nil; Lyra::Genesis's check, on the whole row for
|
|
70
|
+
# an Imported event, on the changed columns for an Updated one) follows
|
|
71
|
+
# +unrepresentable+, config.genesis_unrepresentable by default, as
|
|
72
|
+
# Genesis does: :refuse raises Unrepresentable before anything is
|
|
73
|
+
# written; :skip leaves those records out of line and repairs the rest.
|
|
74
|
+
# Either way the Result lists them (a dry run never raises).
|
|
75
|
+
def run(models: Lyra.config.monitored_models, dry_run: false, operator: nil,
|
|
76
|
+
unrepresentable: Lyra.config.genesis_unrepresentable)
|
|
77
|
+
skip = Lyra::Genesis.unrepresentable_choice(unrepresentable) == :skip
|
|
45
78
|
unless Lyra.config.monitor_mode? || Lyra.config.disabled_mode?
|
|
46
79
|
raise Refused, "lyra:repair writes the tables' state into the event log, so it runs only where the " \
|
|
47
80
|
"tables are authoritative (monitor or disabled). In #{ModeTransition.current} the events " \
|
|
@@ -56,54 +89,112 @@ module Lyra
|
|
|
56
89
|
ids.each { |id| (problem = ModeTransition.discrepancy(model, id)) && found << [model, problem] }
|
|
57
90
|
end
|
|
58
91
|
|
|
59
|
-
|
|
92
|
+
# Refusing (or a dry run): every record is checked before anything is
|
|
93
|
+
# written. Skipping: each repair checks its record, under its lock.
|
|
94
|
+
held = []
|
|
95
|
+
unless skip && !dry_run
|
|
96
|
+
found.each do |model, problem|
|
|
97
|
+
values = unrepresentable_values(model, problem, load_row(model, problem.id))
|
|
98
|
+
held << values if values.any?
|
|
99
|
+
end
|
|
100
|
+
if held.any? && !skip && !dry_run
|
|
101
|
+
values = held.flatten
|
|
102
|
+
raise Unrepresentable.new(rows: held.size, values: values.first(Lyra::Genesis::LISTED), found: values.size)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
repaired = if dry_run
|
|
107
|
+
[]
|
|
108
|
+
else
|
|
109
|
+
found.filter_map { |model, problem| repair_record(model, problem.id, operator: operator, skip: true, held: held) }
|
|
110
|
+
end
|
|
60
111
|
remaining = found.filter_map { |model, problem| ModeTransition.discrepancy(model, problem.id) }
|
|
61
|
-
Result.new(checked: checked, found: found.map(&:last), repaired: repaired, remaining: remaining
|
|
112
|
+
Result.new(checked: checked, found: found.map(&:last), repaired: repaired, remaining: remaining,
|
|
113
|
+
skipped: held.size, unrepresentable: held.flatten.first(Lyra::Genesis::LISTED))
|
|
62
114
|
end
|
|
63
115
|
|
|
64
116
|
# Repair one record. Returns the Discrepancy it repaired, or nil if the
|
|
65
|
-
# record was (or had come) back in line
|
|
66
|
-
|
|
117
|
+
# record was (or had come) back in line, or holds a value its model
|
|
118
|
+
# cannot represent and +unrepresentable+ is :skip (:refuse raises
|
|
119
|
+
# Unrepresentable; see run).
|
|
120
|
+
def repair(model, id, operator: nil, unrepresentable: Lyra.config.genesis_unrepresentable)
|
|
121
|
+
repair_record(model, id, operator: operator, skip: Lyra::Genesis.unrepresentable_choice(unrepresentable) == :skip)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
# repair; a record skipped for its values adds them to +held+ (run checks
|
|
127
|
+
# first only when refusing, so a record is never held twice).
|
|
128
|
+
def repair_record(model, id, operator:, skip:, held: nil)
|
|
67
129
|
model.transaction do
|
|
68
|
-
row =
|
|
130
|
+
row = load_row(model, id, lock: true)
|
|
69
131
|
problem = ModeTransition.discrepancy(model, id)
|
|
70
132
|
next nil unless problem
|
|
71
133
|
|
|
134
|
+
# DualView's differences, computed once for the check and the event.
|
|
135
|
+
differs = problem.problem == "row differs from its events"
|
|
136
|
+
diffs = differences(model, row) if differs
|
|
137
|
+
values = unrepresentable_values(model, problem, row, diffs)
|
|
138
|
+
if values.any?
|
|
139
|
+
raise Unrepresentable.new(rows: 1, values: values.first(Lyra::Genesis::LISTED), found: values.size) unless skip
|
|
140
|
+
|
|
141
|
+
held&.push(values)
|
|
142
|
+
next nil
|
|
143
|
+
end
|
|
144
|
+
|
|
72
145
|
case problem.problem
|
|
73
146
|
when "row but no events", "row of a destroyed record"
|
|
74
|
-
append(model, id, :imported, problem, attributes: row.attributes)
|
|
147
|
+
append(model, id, :imported, problem, attributes: row.attributes, operator: operator)
|
|
75
148
|
when "row differs from its events"
|
|
76
149
|
replayed = Lyra::DualView.new(model, id).event_sourced_state[:state] || {}
|
|
77
|
-
changes = changed_columns(
|
|
78
|
-
append(model, id, :updated, problem, attributes: changes.transform_values(&:last), changes: changes)
|
|
150
|
+
changes = changed_columns(row, replayed, diffs)
|
|
151
|
+
append(model, id, :updated, problem, attributes: changes.transform_values(&:last), changes: changes, operator: operator)
|
|
79
152
|
when "events but no row"
|
|
80
153
|
replayed = Lyra::DualView.new(model, id).event_sourced_state[:state] || {}
|
|
81
|
-
append(model, id, :destroyed, problem, attributes: replayed)
|
|
154
|
+
append(model, id, :destroyed, problem, attributes: replayed, operator: operator)
|
|
82
155
|
end
|
|
83
156
|
problem
|
|
84
157
|
end
|
|
85
158
|
end
|
|
86
159
|
|
|
87
|
-
|
|
160
|
+
def load_row(model, id, lock: false)
|
|
161
|
+
scope = lock ? model.unscoped.lock : model.unscoped
|
|
162
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) { scope.find_by(model.primary_key => id) }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# The values of +row+ the repair of +problem+ would write that the model
|
|
166
|
+
# cannot represent: the whole row for an Imported event, the changed
|
|
167
|
+
# columns (+diffs+, DualView's differences, computed if not given) for an
|
|
168
|
+
# Updated one (Lyra::Genesis.unrepresentable).
|
|
169
|
+
def unrepresentable_values(model, problem, row, diffs = nil)
|
|
170
|
+
return [] if row.nil?
|
|
171
|
+
|
|
172
|
+
case problem.problem
|
|
173
|
+
when "row but no events", "row of a destroyed record" then Lyra::Genesis.unrepresentable(row)
|
|
174
|
+
when "row differs from its events"
|
|
175
|
+
Lyra::Genesis.unrepresentable(row, columns: (diffs || differences(model, row)).keys)
|
|
176
|
+
else []
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def differences(model, row)
|
|
181
|
+
Lyra::PurposeBoundReads.internal(INTERNAL) { Lyra::DualView.new(model, row.id).compare[:differences] }
|
|
182
|
+
end
|
|
88
183
|
|
|
89
|
-
# Column => [replayed, row] for the columns DualView
|
|
90
|
-
def changed_columns(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
[column.to_s, [replayed[column] || replayed[column.to_s], row.attributes[column.to_s]]]
|
|
184
|
+
# Column => [replayed, row] for the columns DualView found different.
|
|
185
|
+
def changed_columns(row, replayed, diffs)
|
|
186
|
+
diffs.keys.to_h do |column|
|
|
187
|
+
[column.to_s, [replayed.fetch(column.to_s) { replayed[column.to_sym] }, row.attributes[column.to_s]]]
|
|
94
188
|
end
|
|
95
189
|
end
|
|
96
190
|
|
|
97
|
-
def append(model, id, operation, problem, attributes:, changes: {})
|
|
98
|
-
data =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
timestamp: Time.current
|
|
105
|
-
}
|
|
106
|
-
metadata = { source: SOURCE, repaired: problem.problem, correlation_id: Lyra::Correlation.current_id }.compact
|
|
191
|
+
def append(model, id, operation, problem, attributes:, changes: {}, operator: nil)
|
|
192
|
+
data = Lyra::EventData.build(model, id, operation, attributes: attributes, changes: changes)
|
|
193
|
+
# Who ran the repair: the label (OPERATOR, Current.user, ...), which nobody checks,
|
|
194
|
+
# and the operating system's user and host beside it (Lyra::Attribution).
|
|
195
|
+
who = Lyra::Attribution.of(operator)
|
|
196
|
+
metadata = { source: SOURCE, repaired: problem.problem, repaired_by: who[:actor], os_user: who[:os_user],
|
|
197
|
+
host: who[:host], pid: who[:pid], correlation_id: Lyra::Correlation.current_id }.compact
|
|
107
198
|
event = Lyra::BypassEvents.event_class_for(model, operation)
|
|
108
199
|
.new(data: data, metadata: Lyra::Privacy.stamp(model, data, metadata))
|
|
109
200
|
Lyra.append_events(event, stream_name: Lyra.stream_name(model, id))
|
data/lib/lyra/retention.rb
CHANGED
|
@@ -21,6 +21,27 @@ module Lyra
|
|
|
21
21
|
# config.retention_anchors names for the model ({ "Registration" =>
|
|
22
22
|
# :registered_at }). Rule conditions (when { ... }) are given the record.
|
|
23
23
|
# Already-erased attributes are not erased again, so a run is idempotent.
|
|
24
|
+
# In ES-NoProj the records, and their anchor, are read from the event store
|
|
25
|
+
# (no default scope applied, as in every mode: a soft-deleted record is
|
|
26
|
+
# still due), and a deletion or soft deletion is written as an event.
|
|
27
|
+
#
|
|
28
|
+
# A record under a restriction of processing (Art. 18, with PAM DSL's
|
|
29
|
+
# rights and Lyra::Rights) is skipped and reported (outcome :restricted,
|
|
30
|
+
# the restrictions in +detail+; Result#restricted lists them): the subject
|
|
31
|
+
# asked for it to be kept (18(1)(b), (c)), and erasing it is processing
|
|
32
|
+
# beyond storage. It is acted on at the first run after the restriction is
|
|
33
|
+
# lifted. An objection's 18(1)(d) restriction, which covers one purpose,
|
|
34
|
+
# does not hold it.
|
|
35
|
+
#
|
|
36
|
+
# Only a model with a retention rule of its own (for_model) is acted on:
|
|
37
|
+
# the policy's default period is not executed.
|
|
38
|
+
#
|
|
39
|
+
# :soft_delete is the strategy of a rule that names none (PAM DSL's
|
|
40
|
+
# default), and it needs a deleted_at or discarded_at column: on a table
|
|
41
|
+
# without one, every record past its period is skipped and kept. Lyra
|
|
42
|
+
# warns about such a rule (Rails.logger, else Kernel#warn) at boot, for
|
|
43
|
+
# the models enrolled by then (check_rules!), and again in each run that
|
|
44
|
+
# skips records for it, once per model, not once per record.
|
|
24
45
|
#
|
|
25
46
|
# Run it on a schedule: bin/rails lyra:retention:apply (DRY_RUN=1 lists
|
|
26
47
|
# what would happen, and works with the executor off), or enqueue
|
|
@@ -37,6 +58,9 @@ module Lyra
|
|
|
37
58
|
end
|
|
38
59
|
|
|
39
60
|
Result = Struct.new(:actions, :dry_run, keyword_init: true) do
|
|
61
|
+
# The records skipped because a restriction of processing holds them.
|
|
62
|
+
def restricted = actions.select { |a| a.outcome == :restricted }
|
|
63
|
+
|
|
40
64
|
def summary
|
|
41
65
|
counts = actions.group_by(&:outcome).transform_values(&:size)
|
|
42
66
|
"#{dry_run ? 'would act on' : 'acted on'} #{actions.size} records: " +
|
|
@@ -54,6 +78,27 @@ module Lyra
|
|
|
54
78
|
Result.new(actions: actions, dry_run: dry_run)
|
|
55
79
|
end
|
|
56
80
|
|
|
81
|
+
# Warns about each of +models+ whose retention rule is :soft_delete
|
|
82
|
+
# while its table has neither deleted_at nor discarded_at, so the rule
|
|
83
|
+
# can never act; returns their names. Lyra calls it at boot for the
|
|
84
|
+
# models enrolled by then. A model whose table does not exist, or
|
|
85
|
+
# whose policy cannot be read, is passed over.
|
|
86
|
+
def check_rules!(models: Lyra.config.monitored_models)
|
|
87
|
+
models.filter_map do |model|
|
|
88
|
+
policy = Lyra::Privacy.policy_for(model)
|
|
89
|
+
rule = policy.retention_rule(model)
|
|
90
|
+
next unless rule && rule.strategy == :soft_delete && model.table_exists?
|
|
91
|
+
next if soft_delete_column(model)
|
|
92
|
+
|
|
93
|
+
warn_soft_delete(model, policy, rule)
|
|
94
|
+
model.name
|
|
95
|
+
rescue StandardError => e
|
|
96
|
+
raise unless e.is_a?(ActiveRecord::ActiveRecordError) || e.class.name.start_with?("PamDsl::")
|
|
97
|
+
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
57
102
|
private
|
|
58
103
|
|
|
59
104
|
def apply_model(model, dry_run, now)
|
|
@@ -66,13 +111,39 @@ module Lyra
|
|
|
66
111
|
shortest = ([rule.duration] + rule.field_durations.values).compact.min
|
|
67
112
|
return [] unless shortest
|
|
68
113
|
|
|
69
|
-
PurposeBoundReads.internal do
|
|
114
|
+
actions = PurposeBoundReads.internal(INTERNAL) do
|
|
70
115
|
candidates(model, anchor, now - shortest).filter_map do |record|
|
|
71
116
|
next unless rule.applies.call(record)
|
|
72
117
|
|
|
73
118
|
act(model, record, rule, personal, anchor, dry_run, now)
|
|
74
119
|
end
|
|
75
120
|
end
|
|
121
|
+
skipped = actions.count { |a| a.strategy == :soft_delete && a.outcome == :skipped }
|
|
122
|
+
warn_soft_delete(model, policy, rule, skipped: skipped) if skipped.positive?
|
|
123
|
+
actions
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def soft_delete_column(model)
|
|
127
|
+
(SOFT_DELETE_COLUMNS & model.column_names).first
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Once per model per call: the model, its policy and rule, what
|
|
131
|
+
# happens to its records, and the two ways to fix it.
|
|
132
|
+
def warn_soft_delete(model, policy, rule, skipped: nil)
|
|
133
|
+
period = rule.duration ? "keep_for #{rule.duration.inspect}, " : ""
|
|
134
|
+
kept = if skipped
|
|
135
|
+
"#{skipped} #{skipped == 1 ? 'record past the period was' : 'records past the period were'} " \
|
|
136
|
+
"skipped and kept"
|
|
137
|
+
else
|
|
138
|
+
"records past the period will be skipped and kept"
|
|
139
|
+
end
|
|
140
|
+
message = "Lyra: the retention rule for #{model.name} (policy :#{policy.name}, #{period}" \
|
|
141
|
+
"on_expiry :soft_delete, also what a rule that names no strategy gets) cannot act: " \
|
|
142
|
+
"#{model.table_name} has no deleted_at or discarded_at column, so #{kept}. " \
|
|
143
|
+
"Add a deleted_at (or discarded_at) column, or give the rule another strategy " \
|
|
144
|
+
"(on_expiry :anonymize or :hard_delete)."
|
|
145
|
+
logger = defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
146
|
+
logger ? logger.warn(message) : Kernel.warn(message)
|
|
76
147
|
end
|
|
77
148
|
|
|
78
149
|
def anchor_for(model)
|
|
@@ -81,10 +152,13 @@ module Lyra
|
|
|
81
152
|
|
|
82
153
|
def candidates(model, anchor, cutoff)
|
|
83
154
|
if events_only?
|
|
84
|
-
# ES-NoProj:
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
155
|
+
# ES-NoProj: the events are the only store (the table is empty or
|
|
156
|
+
# stale). Every live record, rebuilt from its stream with no default
|
|
157
|
+
# scope applied (as unscoped: a soft-deleted record is still due),
|
|
158
|
+
# its anchor read from that state.
|
|
159
|
+
return Lyra::Projections::EventStoreReader.all(model).to_a.select do |record|
|
|
160
|
+
(started = record.public_send(anchor)) && started < cutoff
|
|
161
|
+
end
|
|
88
162
|
end
|
|
89
163
|
|
|
90
164
|
Lyra::Projections::LazyProjection.catch_up! if lazy?
|
|
@@ -119,6 +193,8 @@ module Lyra
|
|
|
119
193
|
def erase(model, record, fields, strategy, dry_run, now)
|
|
120
194
|
fields -= erased_fields(model, record.id)
|
|
121
195
|
return nil if fields.empty?
|
|
196
|
+
held = restricted(record, fields)
|
|
197
|
+
return action(model, record, strategy, fields, :restricted, held) if held
|
|
122
198
|
return action(model, record, strategy, fields, :would_erase) if dry_run
|
|
123
199
|
|
|
124
200
|
Lyra::Erasure.erase!(model, record.id, fields: fields, reason: reason(model, strategy, now), erased_by: SOURCE)
|
|
@@ -127,26 +203,56 @@ module Lyra
|
|
|
127
203
|
|
|
128
204
|
def hard_delete(model, record, personal, dry_run, now)
|
|
129
205
|
fields = personal - erased_fields(model, record.id)
|
|
206
|
+
held = restricted(record, nil)
|
|
207
|
+
return action(model, record, :hard_delete, fields, :restricted, held) if held
|
|
130
208
|
return action(model, record, :hard_delete, fields, :would_delete) if dry_run
|
|
131
209
|
|
|
132
210
|
if fields.any?
|
|
133
211
|
Lyra::Erasure.erase!(model, record.id, fields: fields, reason: reason(model, :hard_delete, now),
|
|
134
212
|
erased_by: SOURCE)
|
|
213
|
+
elsif Lyra.config.erase_access_log
|
|
214
|
+
# Erased before, with its access log kept then: the record goes now, and its log with it.
|
|
215
|
+
Lyra::Erasure.erase_access_log!(model, record.id, reason: reason(model, :hard_delete, now), erased_by: SOURCE)
|
|
135
216
|
end
|
|
136
|
-
model
|
|
217
|
+
current(model, record.id).destroy!
|
|
137
218
|
action(model, record, :hard_delete, fields, :deleted)
|
|
138
219
|
end
|
|
139
220
|
|
|
140
221
|
def soft_delete(model, record, dry_run, now)
|
|
141
|
-
column = (
|
|
222
|
+
column = soft_delete_column(model)
|
|
142
223
|
return action(model, record, :soft_delete, [], :skipped, "no deleted_at or discarded_at column") unless column
|
|
143
224
|
return nil if record.public_send(column)
|
|
225
|
+
held = restricted(record, nil)
|
|
226
|
+
return action(model, record, :soft_delete, [], :restricted, held) if held
|
|
144
227
|
return action(model, record, :soft_delete, [], :would_soft_delete) if dry_run
|
|
145
228
|
|
|
146
|
-
model
|
|
229
|
+
current(model, record.id).update!(column => now)
|
|
147
230
|
action(model, record, :soft_delete, [], :soft_deleted)
|
|
148
231
|
end
|
|
149
232
|
|
|
233
|
+
# The record +id+ as it is now (after an erasure), to write through:
|
|
234
|
+
# its row, or in ES-NoProj its state rebuilt from its events, whose
|
|
235
|
+
# destroy! or update! the interceptor records as an event (there is no
|
|
236
|
+
# row to read or write). No default scope either way.
|
|
237
|
+
def current(model, id)
|
|
238
|
+
return model.unscoped.find(id) unless events_only?
|
|
239
|
+
|
|
240
|
+
Lyra::Projections::EventStoreReader.find(model, id) ||
|
|
241
|
+
raise(ActiveRecord::RecordNotFound.new("#{Lyra.stream_name(model, id)} has no live record in its events",
|
|
242
|
+
model.name, model.primary_key, id))
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# The restrictions holding +record+ (for +fields+; nil: all), as the
|
|
246
|
+
# action's detail, or nil.
|
|
247
|
+
def restricted(record, fields)
|
|
248
|
+
return nil unless defined?(Lyra::Rights::ErasureGuard)
|
|
249
|
+
|
|
250
|
+
held = Lyra::Rights::ErasureGuard.covering_record(record, fields: fields)
|
|
251
|
+
return nil if held.empty?
|
|
252
|
+
|
|
253
|
+
"restricted: #{held.map { |r| Lyra::Rights::ErasureGuard.describe(r) }.join('; ')}"
|
|
254
|
+
end
|
|
255
|
+
|
|
150
256
|
# Attributes an earlier erasure of this record already covered.
|
|
151
257
|
def erased_fields(model, id)
|
|
152
258
|
Lyra.config.event_store.read.stream(Lyra.stream_name(model, id)).to_a
|