active_version 2.0.0 → 2.1.1
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 +28 -0
- data/README.md +3 -3
- data/SECURITY.md +10 -12
- data/lib/active_version/audits/audit_record/serializers.rb +1 -1
- data/lib/active_version/audits/has_audits/audit_combiner.rb +6 -4
- data/lib/active_version/audits/has_audits/audit_writer.rb +22 -7
- data/lib/active_version/audits/has_audits/change_filters.rb +3 -1
- data/lib/active_version/audits/has_audits.rb +23 -8
- data/lib/active_version/audits/sql_builder.rb +2 -2
- data/lib/active_version/configuration.rb +15 -0
- data/lib/active_version/database/triggers/postgresql.rb +43 -25
- data/lib/active_version/instrumentation.rb +9 -1
- data/lib/active_version/migrators/audited.rb +18 -1
- data/lib/active_version/query.rb +36 -25
- data/lib/active_version/railtie.rb +8 -0
- data/lib/active_version/revisions/has_revisions/revision_manipulation.rb +29 -9
- data/lib/active_version/revisions/has_revisions/revision_queries.rb +7 -6
- data/lib/active_version/revisions/has_revisions.rb +26 -27
- data/lib/active_version/revisions/sql_builder.rb +1 -1
- data/lib/active_version/version.rb +1 -1
- data/lib/active_version.rb +37 -21
- data/lib/generators/active_version/revisions/templates/migration.rb.erb +6 -7
- data/sig/active_version/configuration.rbs +3 -1
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c3b099b1522fb5b1d087683cf7003c5f1156f7f0da8703691f89075b347183d5
|
|
4
|
+
data.tar.gz: 0d49734313021a4ef263a807361fbf1b5b1f5d564b693162ddfe0d17b2439768
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27d139f5b16f533cdf00e11ab4ce7f9b12830574eb4e1cd49c900ed7885447a22c6ea2507cdacdcbcdd1d32577bed4e369a950c8c3a588bb35584157290bfbde
|
|
7
|
+
data.tar.gz: 87808c1b4380abaf00d72f223a27d0010fc178c6433fa52acfe356c00a9461b7e870c31320b102faaddf20d0f37cbccd8368f7c3516117bea48c89cdb45906d8
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 2.1.1 (2026-11-07)
|
|
4
|
+
|
|
5
|
+
- Use a single shallow `dup` of the merged global context when audit instance context is empty, instead of `dup.merge({})`.
|
|
6
|
+
- Encode audit/revision SQL and serializer JSON through `ActiveVersion.json_encode` (`JSON.generate`); use the same encoder in `with_transactional_context`.
|
|
7
|
+
- Cache `Query` `column_mapper` lookups per `(model, kind, field)`; share identity-map helpers; return the model class `none` when the version class is missing.
|
|
8
|
+
- Memoize `ActiveSupport::Notifications.listening?` per instrumentation event name (reset in specs).
|
|
9
|
+
- Merge audit batch context with `merge!` when the existing context is already a Hash; skip `stringify_keys` when keys are already strings.
|
|
10
|
+
- Remove `AV_PRESSURE_BUILD` / `pressure_build_phase_enabled?` from lib; drop the pressure-cooker harness and its episode docs.
|
|
11
|
+
- Maintainer load harness: `benchmarks/load.rb` and `benchmarks/pressure_probe.rb` with `make bench-load` / `make bench-fiber`.
|
|
12
|
+
|
|
13
|
+
## 2.1.0 (2026-10-29)
|
|
14
|
+
|
|
15
|
+
- Record PostgreSQL audit updates as changed-field pairs and support quoted table and column names.
|
|
16
|
+
- Copy source table columns into generated revision tables so custom attributes retain their values.
|
|
17
|
+
- Keep inherited model audits visible through ActiveRecord polymorphic associations.
|
|
18
|
+
|
|
19
|
+
- Fix `without_auditing` and `without_revisions` to use per-fiber/thread depth so other threads keep writing history.
|
|
20
|
+
- Restore only request-scoped context after `with_context` so persistent keys do not shadow a later `with_context!`; clear persistent context via the Railtie executor `to_complete` hook on request end.
|
|
21
|
+
- Honor `with_context(..., transactional: false)` by skipping the PostgreSQL `SET LOCAL` path.
|
|
22
|
+
- Fix `at(time:)` / `revision_at` to return the live state between updates (earliest revision after the time), matching pre-change snapshot semantics.
|
|
23
|
+
- Keep the earliest pre-change revision payload when debounce merges callback-driven updates inside the window.
|
|
24
|
+
- Skip pending-save columns when refreshing default-function attributes during revision snapshots.
|
|
25
|
+
- Map `action` and optional user/address/request fields in `Migrators::Audited`.
|
|
26
|
+
- Skip `redacted` and `encrypted_attributes` when copying shared columns for `:mirror_columns` storage.
|
|
27
|
+
- Validate `audit_error_behavior` and `revision_error_behavior` in `Configuration#validate!`.
|
|
28
|
+
- Require `Rails::Railtie` before loading the railtie; raise `required_ruby_version` to `>= 3.1` to match Active Support 7.2+.
|
|
29
|
+
- Document sharding as host-connection follow-through, not gem-owned routing.
|
|
30
|
+
|
|
3
31
|
## 2.0.0 (2026-08-21)
|
|
4
32
|
|
|
5
33
|
- BREAKING: Require Active Support and Active Record `>= 7.2.3.2` and `< 9.0`.
|
data/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Design changes go through `rfcs/` (RFC 0001).
|
|
|
12
12
|
- Revisions: Schema-aligned snapshots for workflow management
|
|
13
13
|
- Audits: `:json_column`, `:yaml_column`, or `:mirror_columns` change tracking
|
|
14
14
|
- Database Triggers: Optional PostgreSQL triggers for zero-overhead versioning
|
|
15
|
-
- Sharding
|
|
15
|
+
- Sharding: Follows the host app connection; this gem does not route shards
|
|
16
16
|
- SQL Generation: Batch operations via SQL generation
|
|
17
17
|
- Configurable: Flexible column naming and per-model configuration
|
|
18
18
|
|
|
@@ -307,7 +307,7 @@ rails g active_version:triggers Post --type=audit
|
|
|
307
307
|
rails g active_version:triggers Post --type=revision
|
|
308
308
|
```
|
|
309
309
|
|
|
310
|
-
PostgreSQL audit triggers capture the
|
|
310
|
+
PostgreSQL audit triggers capture complete rows on create and destroy, and changed-field old/new pairs on update. Disable the matching Ruby callbacks with `has_audits auto: false` or `has_revisions auto: false` before installing triggers so each change has one history writer. They do not apply Ruby-side `only`, `except`, or `redacted` filters from `has_audits`. Use triggers only when you want complete row snapshots at the database layer, or generate custom trigger SQL that matches your column policy.
|
|
311
311
|
|
|
312
312
|
### Infrastructure Ownership
|
|
313
313
|
|
|
@@ -503,7 +503,7 @@ Environment knobs:
|
|
|
503
503
|
|
|
504
504
|
## Requirements
|
|
505
505
|
|
|
506
|
-
- Ruby >= 3.
|
|
506
|
+
- Ruby >= 3.1.0
|
|
507
507
|
- ActiveRecord >= 7.2.3.2 and < 9.0 (default runtime)
|
|
508
508
|
- SQLite or MySQL or PostgreSQL (optional, for triggers and JSONB support)
|
|
509
509
|
|
data/SECURITY.md
CHANGED
|
@@ -2,26 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
## Reporting a Vulnerability
|
|
4
4
|
|
|
5
|
-
Do not open a public issue for security vulnerabilities.
|
|
6
|
-
|
|
7
5
|
Email security details to: security@kiskolabs.com
|
|
8
6
|
|
|
9
|
-
Include
|
|
7
|
+
Send the report only to that address. Include description, steps to reproduce, potential impact, and a suggested fix when you have one.
|
|
10
8
|
|
|
11
9
|
### Response Timeline
|
|
12
10
|
|
|
13
|
-
- We will acknowledge receipt of your report
|
|
14
|
-
- We will provide an initial assessment
|
|
15
|
-
- We will keep you informed of our progress and resolution timeline
|
|
11
|
+
- We will acknowledge receipt of your report.
|
|
12
|
+
- We will provide an initial assessment.
|
|
13
|
+
- We will keep you informed of our progress and resolution timeline.
|
|
16
14
|
|
|
17
15
|
### Disclosure Policy
|
|
18
16
|
|
|
19
|
-
- We will work with you to understand and resolve the issue
|
|
20
|
-
- We will credit you for the discovery
|
|
21
|
-
- We will publish a security advisory after the vulnerability is patched
|
|
22
|
-
- We will coordinate public disclosure with you
|
|
17
|
+
- We will work with you to understand and resolve the issue.
|
|
18
|
+
- We will credit you for the discovery unless you prefer to remain anonymous.
|
|
19
|
+
- We will publish a security advisory after the vulnerability is patched.
|
|
20
|
+
- We will coordinate public disclosure with you.
|
|
23
21
|
|
|
24
22
|
## Automation Security
|
|
25
23
|
|
|
26
|
-
-
|
|
27
|
-
-
|
|
24
|
+
- Keep production credentials, API keys, and personal data out of prompts sent to third-party models or automation services.
|
|
25
|
+
- Verify automated dependencies.
|
|
@@ -30,7 +30,7 @@ module ActiveVersion
|
|
|
30
30
|
association(:audits).reset
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
auditable_type = audited_options[:class_name] || self.class.
|
|
33
|
+
auditable_type = audited_options[:class_name] || self.class.polymorphic_name
|
|
34
34
|
auditable_column = ActiveVersion.column_mapper.column_for(self.class, :audits, :auditable)
|
|
35
35
|
version_column = ActiveVersion.column_mapper.column_for(self.class, :audits, :version)
|
|
36
36
|
audit_klass = combiner_audit_klass
|
|
@@ -175,9 +175,11 @@ module ActiveVersion
|
|
|
175
175
|
conn = audit_class.connection
|
|
176
176
|
table_name = audit_class.table_name
|
|
177
177
|
updates = []
|
|
178
|
-
|
|
178
|
+
enc_changes = ActiveVersion.json_encode(combined_changes)
|
|
179
|
+
updates << "#{conn.quote_column_name(changes_column)} = #{conn.quote(enc_changes)}"
|
|
179
180
|
if combined_context.any?
|
|
180
|
-
|
|
181
|
+
enc_ctx = ActiveVersion.json_encode(combined_context)
|
|
182
|
+
updates << "#{conn.quote_column_name(context_column)} = #{conn.quote(enc_ctx)}"
|
|
181
183
|
end
|
|
182
184
|
target_id = conn.quote(combine_target.read_attribute(:id))
|
|
183
185
|
sql = "UPDATE #{conn.quote_table_name(table_name)} SET #{updates.join(", ")} WHERE id = #{target_id}"
|
|
@@ -195,7 +197,7 @@ module ActiveVersion
|
|
|
195
197
|
# Use raw SQL with proper escaping to bypass ActiveRecord's readonly checks
|
|
196
198
|
conn = audit_class.connection
|
|
197
199
|
table_name = audit_class.table_name
|
|
198
|
-
empty_json_str =
|
|
200
|
+
empty_json_str = ActiveVersion.json_encode({})
|
|
199
201
|
empty_json = conn.quote(empty_json_str)
|
|
200
202
|
quoted_comment = conn.quote(combined_comment)
|
|
201
203
|
|
|
@@ -41,8 +41,13 @@ module ActiveVersion
|
|
|
41
41
|
else
|
|
42
42
|
{}
|
|
43
43
|
end
|
|
44
|
-
# Merge: global context first, then instance context (instance takes precedence)
|
|
45
|
-
|
|
44
|
+
# Merge: global context first, then instance context (instance takes precedence).
|
|
45
|
+
# `dup.merge({})` allocates twice; shallow `dup` alone matches semantics when instance is empty.
|
|
46
|
+
captured_context = if instance_context.empty?
|
|
47
|
+
global_context.dup
|
|
48
|
+
else
|
|
49
|
+
global_context.dup.merge!(instance_context)
|
|
50
|
+
end
|
|
46
51
|
|
|
47
52
|
self.audit_comment = nil
|
|
48
53
|
self.audit_context = nil
|
|
@@ -102,8 +107,16 @@ module ActiveVersion
|
|
|
102
107
|
batch_context = batch_state.dig(:options, :context)
|
|
103
108
|
if batch_context.is_a?(Hash)
|
|
104
109
|
merged_context = insert_attrs[context_column]
|
|
105
|
-
|
|
106
|
-
|
|
110
|
+
batch_keys = if batch_context.keys.all? { |k| k.is_a?(String) }
|
|
111
|
+
batch_context
|
|
112
|
+
else
|
|
113
|
+
batch_context.stringify_keys
|
|
114
|
+
end
|
|
115
|
+
if merged_context.is_a?(Hash)
|
|
116
|
+
merged_context.merge!(batch_keys)
|
|
117
|
+
else
|
|
118
|
+
insert_attrs[context_column] = batch_keys
|
|
119
|
+
end
|
|
107
120
|
end
|
|
108
121
|
end
|
|
109
122
|
|
|
@@ -116,7 +129,7 @@ module ActiveVersion
|
|
|
116
129
|
insert_attrs.merge!(identity_map)
|
|
117
130
|
# Use class_name from options if provided (for dynamically created classes)
|
|
118
131
|
# Otherwise use the actual class name
|
|
119
|
-
auditable_type = audited_options[:class_name] || self.class.
|
|
132
|
+
auditable_type = audited_options[:class_name] || self.class.polymorphic_name
|
|
120
133
|
if auditable_type.nil?
|
|
121
134
|
raise ConfigurationError, "Cannot determine class name for dynamically created class. Please specify class_name option in has_audits (e.g., has_audits as: PostAudit, class_name: 'Post')"
|
|
122
135
|
end
|
|
@@ -216,7 +229,7 @@ module ActiveVersion
|
|
|
216
229
|
return nil
|
|
217
230
|
end
|
|
218
231
|
|
|
219
|
-
auditable_type_for_query = audited_options[:class_name] || self.class.
|
|
232
|
+
auditable_type_for_query = audited_options[:class_name] || self.class.polymorphic_name
|
|
220
233
|
if auditable_type_for_query.nil?
|
|
221
234
|
raise ConfigurationError, "Cannot determine class name for dynamically created class. Please specify class_name option in has_audits"
|
|
222
235
|
end
|
|
@@ -273,7 +286,9 @@ module ActiveVersion
|
|
|
273
286
|
ActiveVersion.column_mapper.column_for(self.class, :audits, :version).to_s,
|
|
274
287
|
ActiveVersion.column_mapper.column_for(self.class, :audits, :action).to_s
|
|
275
288
|
]
|
|
276
|
-
|
|
289
|
+
redacted = Array(audited_options[:redacted]).map(&:to_s)
|
|
290
|
+
encrypted = respond_to?(:encrypted_attributes) ? Array(encrypted_attributes).map(&:to_s) : []
|
|
291
|
+
shared_columns -= (excluded + redacted + encrypted)
|
|
277
292
|
|
|
278
293
|
shared_columns.each do |column_name|
|
|
279
294
|
next if insert_attrs.key?(column_name)
|
|
@@ -107,7 +107,9 @@ module ActiveVersion
|
|
|
107
107
|
h = changes.each_with_object({}) do |(k, v), acc|
|
|
108
108
|
acc[k] = v.last if v.is_a?(Array)
|
|
109
109
|
acc[k] = v unless v.is_a?(Array)
|
|
110
|
-
|
|
110
|
+
if acc[k].is_a?(Hash) || acc[k].is_a?(Array)
|
|
111
|
+
acc[k] = ActiveVersion.json_encode(acc[k])
|
|
112
|
+
end
|
|
111
113
|
end
|
|
112
114
|
# Arel INSERT uses keys as column names; stringify so :action / mirror :attrs match
|
|
113
115
|
# string keys from column_mapper and so symbol + string for the same column cannot
|
|
@@ -100,18 +100,21 @@ module ActiveVersion
|
|
|
100
100
|
audited_options.present?
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
-
# Disable auditing for
|
|
103
|
+
# Disable auditing for this fiber/thread only (does not silence other threads)
|
|
104
104
|
def without_auditing
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
key = auditing_disabled_depth_store_key
|
|
106
|
+
depth = ActiveVersion.store_get(key).to_i + 1
|
|
107
|
+
ActiveVersion.store_set(key, depth)
|
|
107
108
|
yield
|
|
108
109
|
ensure
|
|
109
|
-
|
|
110
|
+
depth = ActiveVersion.store_get(key).to_i - 1
|
|
111
|
+
depth = 0 if depth.negative?
|
|
112
|
+
ActiveVersion.store_set(key, depth)
|
|
110
113
|
end
|
|
111
114
|
|
|
112
|
-
# Enable auditing for a block
|
|
115
|
+
# Enable auditing for a block even when class auditing was disabled permanently
|
|
113
116
|
def with_auditing
|
|
114
|
-
auditing_was_enabled =
|
|
117
|
+
auditing_was_enabled = class_auditing_enabled_without_skip?
|
|
115
118
|
enable_auditing
|
|
116
119
|
yield
|
|
117
120
|
ensure
|
|
@@ -506,10 +509,22 @@ module ActiveVersion
|
|
|
506
509
|
end
|
|
507
510
|
|
|
508
511
|
def class_auditing_enabled?
|
|
509
|
-
|
|
512
|
+
return false if ActiveVersion.store_get(auditing_disabled_depth_store_key).to_i.positive?
|
|
513
|
+
|
|
514
|
+
class_auditing_enabled_without_skip?
|
|
510
515
|
end
|
|
511
516
|
public :class_auditing_enabled?
|
|
512
517
|
|
|
518
|
+
def class_auditing_enabled_without_skip?
|
|
519
|
+
@class_auditing_enabled != false
|
|
520
|
+
end
|
|
521
|
+
private :class_auditing_enabled_without_skip?
|
|
522
|
+
|
|
523
|
+
def auditing_disabled_depth_store_key
|
|
524
|
+
:"active_version_#{name}_auditing_disabled_depth"
|
|
525
|
+
end
|
|
526
|
+
private :auditing_disabled_depth_store_key
|
|
527
|
+
|
|
513
528
|
def disable_auditing
|
|
514
529
|
@class_auditing_enabled = false
|
|
515
530
|
end
|
|
@@ -808,7 +823,7 @@ module ActiveVersion
|
|
|
808
823
|
# Use active_audits for filtered results
|
|
809
824
|
def audits
|
|
810
825
|
# Use class_name from options if provided (for dynamically created classes)
|
|
811
|
-
auditable_type = audited_options[:class_name] || self.class.
|
|
826
|
+
auditable_type = audited_options[:class_name] || self.class.polymorphic_name
|
|
812
827
|
if auditable_type.nil?
|
|
813
828
|
raise ConfigurationError, "Cannot determine class name for dynamically created class. Please specify class_name option in has_audits (e.g., has_audits as: PostAudit, class_name: 'Post')"
|
|
814
829
|
end
|
|
@@ -169,7 +169,7 @@ module ActiveVersion
|
|
|
169
169
|
end
|
|
170
170
|
return nil if identity_map.values.any?(&:nil?)
|
|
171
171
|
attrs.merge!(identity_map)
|
|
172
|
-
attrs["#{auditable_column}_type"] = record.class.
|
|
172
|
+
attrs["#{auditable_column}_type"] = record.class.polymorphic_name
|
|
173
173
|
|
|
174
174
|
# Set version
|
|
175
175
|
version_column = ActiveVersion.column_mapper.column_for(record.class, :audits, :version)
|
|
@@ -250,7 +250,7 @@ module ActiveVersion
|
|
|
250
250
|
def prepare_sql_value(value)
|
|
251
251
|
case value
|
|
252
252
|
when Hash, Array
|
|
253
|
-
|
|
253
|
+
ActiveVersion.json_encode(value)
|
|
254
254
|
when Time, DateTime
|
|
255
255
|
value.utc
|
|
256
256
|
when Date
|
|
@@ -78,10 +78,13 @@ module ActiveVersion
|
|
|
78
78
|
@debounce_time = nil # Time in seconds to merge revisions within window
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
+
ERROR_BEHAVIORS = %i[exception log silent].freeze
|
|
82
|
+
|
|
81
83
|
# Validate configuration
|
|
82
84
|
def validate!
|
|
83
85
|
validate_storage_type!
|
|
84
86
|
validate_execution_scope!
|
|
87
|
+
validate_error_behaviors!
|
|
85
88
|
validate_column_names!
|
|
86
89
|
end
|
|
87
90
|
|
|
@@ -101,6 +104,18 @@ module ActiveVersion
|
|
|
101
104
|
"execution_scope must be :fiber or :thread, got: #{@execution_scope.inspect}"
|
|
102
105
|
end
|
|
103
106
|
|
|
107
|
+
def validate_error_behaviors!
|
|
108
|
+
unless ERROR_BEHAVIORS.include?(@audit_error_behavior)
|
|
109
|
+
raise ConfigurationError,
|
|
110
|
+
"audit_error_behavior must be one of #{ERROR_BEHAVIORS.inspect}, got: #{@audit_error_behavior.inspect}"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
unless ERROR_BEHAVIORS.include?(@revision_error_behavior)
|
|
114
|
+
raise ConfigurationError,
|
|
115
|
+
"revision_error_behavior must be one of #{ERROR_BEHAVIORS.inspect}, got: #{@revision_error_behavior.inspect}"
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
104
119
|
def validate_column_names!
|
|
105
120
|
# Ensure column names are symbols or strings
|
|
106
121
|
column_attrs = [
|
|
@@ -10,12 +10,14 @@ module ActiveVersion
|
|
|
10
10
|
# @param options [Hash] Trigger options
|
|
11
11
|
# @return [String] SQL for trigger function
|
|
12
12
|
def generate_audit_trigger_function(table_name, audit_table_name, options = {})
|
|
13
|
-
function_name = "active_version_audit_#{table_name}"
|
|
14
|
-
auditable_type = options[:auditable_type] || table_name.classify
|
|
15
|
-
version_column = options[:version_column] || "version"
|
|
16
|
-
changes_column = options[:changes_column] || "audited_changes"
|
|
17
|
-
context_column = options[:context_column] || "audited_context"
|
|
18
|
-
action_column = options[:action_column] || "action"
|
|
13
|
+
function_name = quote_identifier("active_version_audit_#{table_name.tr(".", "_")}")
|
|
14
|
+
auditable_type = (options[:auditable_type] || table_name.classify).gsub("'", "''")
|
|
15
|
+
version_column = quote_identifier(options[:version_column] || "version")
|
|
16
|
+
changes_column = quote_identifier(options[:changes_column] || "audited_changes")
|
|
17
|
+
context_column = quote_identifier(options[:context_column] || "audited_context")
|
|
18
|
+
action_column = quote_identifier(options[:action_column] || "action")
|
|
19
|
+
|
|
20
|
+
audit_table_name = quote_table(audit_table_name)
|
|
19
21
|
|
|
20
22
|
<<~SQL
|
|
21
23
|
CREATE OR REPLACE FUNCTION #{function_name}()
|
|
@@ -32,10 +34,13 @@ module ActiveVersion
|
|
|
32
34
|
changes := to_jsonb(NEW.*);
|
|
33
35
|
ELSIF TG_OP = 'UPDATE' THEN
|
|
34
36
|
action_type := 'update';
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
37
|
+
SELECT COALESCE(jsonb_object_agg(item.key, jsonb_build_array(to_jsonb(OLD.*) -> item.key, item.value)), '{}'::jsonb)
|
|
38
|
+
INTO changes
|
|
39
|
+
FROM jsonb_each(to_jsonb(NEW.*)) AS item
|
|
40
|
+
WHERE item.value IS DISTINCT FROM to_jsonb(OLD.*) -> item.key;
|
|
41
|
+
IF changes = '{}'::jsonb THEN
|
|
42
|
+
RETURN NEW;
|
|
43
|
+
END IF;
|
|
39
44
|
ELSIF TG_OP = 'DELETE' THEN
|
|
40
45
|
action_type := 'destroy';
|
|
41
46
|
changes := to_jsonb(OLD.*);
|
|
@@ -54,7 +59,7 @@ module ActiveVersion
|
|
|
54
59
|
|
|
55
60
|
-- Get context from session variables (if set)
|
|
56
61
|
context_data := COALESCE(
|
|
57
|
-
current_setting('active_version.context', true)::jsonb,
|
|
62
|
+
NULLIF(current_setting('active_version.context', true), '')::jsonb,
|
|
58
63
|
'{}'::jsonb
|
|
59
64
|
);
|
|
60
65
|
|
|
@@ -112,15 +117,19 @@ module ActiveVersion
|
|
|
112
117
|
# @param options [Hash] Trigger options
|
|
113
118
|
# @return [String] SQL for CREATE TRIGGER statement
|
|
114
119
|
def generate_audit_trigger(table_name, options = {})
|
|
115
|
-
function_name = "active_version_audit_#{table_name}"
|
|
116
|
-
trigger_name = options[:trigger_name] || "active_version_audit_on_#{table_name}"
|
|
120
|
+
function_name = quote_identifier("active_version_audit_#{table_name.tr(".", "_")}")
|
|
121
|
+
trigger_name = quote_identifier(options[:trigger_name] || "active_version_audit_on_#{table_name.tr(".", "_")}")
|
|
117
122
|
events = options[:events] || [:insert, :update, :delete]
|
|
118
123
|
|
|
119
|
-
|
|
124
|
+
event_names = events.map { |event| event.to_s.upcase }
|
|
125
|
+
unless event_names.any? && (event_names - %w[INSERT UPDATE DELETE]).empty?
|
|
126
|
+
raise ArgumentError, "audit trigger events must be insert, update, or delete"
|
|
127
|
+
end
|
|
128
|
+
event_clause = event_names.join(" OR ")
|
|
120
129
|
|
|
121
130
|
<<~SQL
|
|
122
131
|
CREATE TRIGGER #{trigger_name}
|
|
123
|
-
AFTER #{event_clause} ON #{table_name}
|
|
132
|
+
AFTER #{event_clause} ON #{quote_table(table_name)}
|
|
124
133
|
FOR EACH ROW
|
|
125
134
|
WHEN (coalesce(current_setting('active_version.disabled', true), '') <> 'on')
|
|
126
135
|
EXECUTE FUNCTION #{function_name}();
|
|
@@ -133,9 +142,10 @@ module ActiveVersion
|
|
|
133
142
|
# @param options [Hash] Trigger options
|
|
134
143
|
# @return [String] SQL for trigger function
|
|
135
144
|
def generate_revision_trigger_function(table_name, revision_table_name, options = {})
|
|
136
|
-
function_name = "active_version_revision_#{table_name}"
|
|
137
|
-
foreign_key = options[:foreign_key] || "#{table_name.singularize}_id"
|
|
138
|
-
|
|
145
|
+
function_name = quote_identifier("active_version_revision_#{table_name.tr(".", "_")}")
|
|
146
|
+
foreign_key = quote_identifier(options[:foreign_key] || "#{table_name.singularize}_id")
|
|
147
|
+
revision_table_name = quote_table(revision_table_name)
|
|
148
|
+
version_column = quote_identifier(options[:version_column] || "version")
|
|
139
149
|
|
|
140
150
|
<<~SQL
|
|
141
151
|
CREATE OR REPLACE FUNCTION #{function_name}()
|
|
@@ -186,12 +196,12 @@ module ActiveVersion
|
|
|
186
196
|
# @param options [Hash] Trigger options
|
|
187
197
|
# @return [String] SQL for CREATE TRIGGER statement
|
|
188
198
|
def generate_revision_trigger(table_name, options = {})
|
|
189
|
-
function_name = "active_version_revision_#{table_name}"
|
|
190
|
-
trigger_name = options[:trigger_name] || "active_version_revision_on_#{table_name}"
|
|
199
|
+
function_name = quote_identifier("active_version_revision_#{table_name.tr(".", "_")}")
|
|
200
|
+
trigger_name = quote_identifier(options[:trigger_name] || "active_version_revision_on_#{table_name.tr(".", "_")}")
|
|
191
201
|
|
|
192
202
|
<<~SQL
|
|
193
203
|
CREATE TRIGGER #{trigger_name}
|
|
194
|
-
BEFORE UPDATE ON #{table_name}
|
|
204
|
+
BEFORE UPDATE ON #{quote_table(table_name)}
|
|
195
205
|
FOR EACH ROW
|
|
196
206
|
WHEN (coalesce(current_setting('active_version.disabled', true), '') <> 'on')
|
|
197
207
|
EXECUTE FUNCTION #{function_name}();
|
|
@@ -202,7 +212,7 @@ module ActiveVersion
|
|
|
202
212
|
# @param function_name [String] Name of the function
|
|
203
213
|
# @return [String] SQL for DROP FUNCTION statement
|
|
204
214
|
def drop_trigger_function(function_name)
|
|
205
|
-
"DROP FUNCTION IF EXISTS #{function_name}() CASCADE;"
|
|
215
|
+
"DROP FUNCTION IF EXISTS #{quote_identifier(function_name)}() CASCADE;"
|
|
206
216
|
end
|
|
207
217
|
|
|
208
218
|
# Drop trigger
|
|
@@ -210,19 +220,27 @@ module ActiveVersion
|
|
|
210
220
|
# @param table_name [String] Name of the table
|
|
211
221
|
# @return [String] SQL for DROP TRIGGER statement
|
|
212
222
|
def drop_trigger(trigger_name, table_name)
|
|
213
|
-
"DROP TRIGGER IF EXISTS #{trigger_name} ON #{table_name} CASCADE;"
|
|
223
|
+
"DROP TRIGGER IF EXISTS #{quote_identifier(trigger_name)} ON #{quote_table(table_name)} CASCADE;"
|
|
214
224
|
end
|
|
215
225
|
|
|
216
226
|
private
|
|
217
227
|
|
|
228
|
+
def quote_identifier(name)
|
|
229
|
+
%("#{name.to_s.gsub('"', '""')}")
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def quote_table(name)
|
|
233
|
+
name.to_s.split(".").map { |part| quote_identifier(part) }.join(".")
|
|
234
|
+
end
|
|
235
|
+
|
|
218
236
|
def build_revision_columns(table_name, options)
|
|
219
237
|
columns = normalize_revision_columns(options)
|
|
220
|
-
columns.join(", ")
|
|
238
|
+
columns.map { |column| quote_identifier(column) }.join(", ")
|
|
221
239
|
end
|
|
222
240
|
|
|
223
241
|
def build_revision_values(record_prefix, table_name, options)
|
|
224
242
|
columns = normalize_revision_columns(options)
|
|
225
|
-
columns.map { |column| "#{record_prefix}.#{column}" }.join(", ")
|
|
243
|
+
columns.map { |column| "#{record_prefix}.#{quote_identifier(column)}" }.join(", ")
|
|
226
244
|
end
|
|
227
245
|
|
|
228
246
|
def normalize_revision_columns(options)
|
|
@@ -16,6 +16,11 @@ module ActiveVersion
|
|
|
16
16
|
}.freeze
|
|
17
17
|
|
|
18
18
|
class << self
|
|
19
|
+
# Clear memoized `listening?` probes (tests stub the notifier between examples).
|
|
20
|
+
def reset_listening_cache!
|
|
21
|
+
@listening_cache = nil
|
|
22
|
+
end
|
|
23
|
+
|
|
19
24
|
# Instrument a translation creation
|
|
20
25
|
def instrument_translation_created(translation, source)
|
|
21
26
|
instrument_event(:translation_created) do
|
|
@@ -201,7 +206,10 @@ module ActiveVersion
|
|
|
201
206
|
end
|
|
202
207
|
|
|
203
208
|
def listening?(event_name)
|
|
204
|
-
|
|
209
|
+
@listening_cache ||= {}
|
|
210
|
+
return @listening_cache[event_name] if @listening_cache.key?(event_name)
|
|
211
|
+
|
|
212
|
+
@listening_cache[event_name] = ActiveSupport::Notifications.notifier.listening?(event_name)
|
|
205
213
|
rescue => e
|
|
206
214
|
log_debug("listener probe failed for #{event_name}: #{e.class}: #{e.message}; assuming listeners may exist")
|
|
207
215
|
true
|
|
@@ -66,8 +66,9 @@ module ActiveVersion
|
|
|
66
66
|
changes_column = ActiveVersion.column_mapper.column_for(model_class, :audits, :changes).to_s
|
|
67
67
|
context_column = ActiveVersion.column_mapper.column_for(model_class, :audits, :context).to_s
|
|
68
68
|
comment_column = ActiveVersion.column_mapper.column_for(model_class, :audits, :comment).to_s
|
|
69
|
+
action_column = ActiveVersion.column_mapper.column_for(model_class, :audits, :action).to_s
|
|
69
70
|
|
|
70
|
-
{
|
|
71
|
+
data = {
|
|
71
72
|
"#{auditable_column}_id" => old_audit.auditable_id,
|
|
72
73
|
"#{auditable_column}_type" => old_audit.auditable_type,
|
|
73
74
|
version_column => old_audit.version,
|
|
@@ -77,6 +78,22 @@ module ActiveVersion
|
|
|
77
78
|
:created_at => old_audit.created_at,
|
|
78
79
|
:updated_at => old_audit.updated_at || old_audit.created_at
|
|
79
80
|
}
|
|
81
|
+
|
|
82
|
+
copy_optional_audit_field(data, old_audit, :action, action_column)
|
|
83
|
+
copy_optional_audit_field(data, old_audit, :user_id, ActiveVersion.config.audit_user_column)
|
|
84
|
+
copy_optional_audit_field(data, old_audit, :user_type, :user_type)
|
|
85
|
+
copy_optional_audit_field(data, old_audit, :remote_address, ActiveVersion.config.audit_remote_address_column)
|
|
86
|
+
copy_optional_audit_field(data, old_audit, :request_uuid, ActiveVersion.config.audit_request_uuid_column)
|
|
87
|
+
copy_optional_audit_field(data, old_audit, :associated_id, :"#{ActiveVersion.config.audit_associated_column}_id")
|
|
88
|
+
copy_optional_audit_field(data, old_audit, :associated_type, :"#{ActiveVersion.config.audit_associated_column}_type")
|
|
89
|
+
|
|
90
|
+
data
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def copy_optional_audit_field(data, old_audit, source_method, destination_column)
|
|
94
|
+
return unless old_audit.respond_to?(source_method)
|
|
95
|
+
|
|
96
|
+
data[destination_column.to_s] = old_audit.public_send(source_method)
|
|
80
97
|
end
|
|
81
98
|
end
|
|
82
99
|
end
|
data/lib/active_version/query.rb
CHANGED
|
@@ -2,6 +2,12 @@ module ActiveVersion
|
|
|
2
2
|
# Unified query builder for version records
|
|
3
3
|
module Query
|
|
4
4
|
class << self
|
|
5
|
+
def column_for_cached(record_class, kind, field)
|
|
6
|
+
@column_for_query ||= {}
|
|
7
|
+
key = [record_class.name, kind, field]
|
|
8
|
+
@column_for_query[key] ||= ActiveVersion.column_mapper.column_for(record_class, kind, field)
|
|
9
|
+
end
|
|
10
|
+
|
|
5
11
|
# Query audits for a record
|
|
6
12
|
# @param record [ActiveRecord::Base] The record to query audits for
|
|
7
13
|
# @param opts [Hash] Query options
|
|
@@ -10,18 +16,13 @@ module ActiveVersion
|
|
|
10
16
|
# @return [ActiveRecord::Relation] Audit relation
|
|
11
17
|
def audits(record, opts = {})
|
|
12
18
|
audit_class = record.class.audit_class
|
|
13
|
-
return
|
|
19
|
+
return record.class.none unless audit_class
|
|
14
20
|
|
|
15
|
-
auditable_column =
|
|
21
|
+
auditable_column = column_for_cached(record.class, :audits, :auditable)
|
|
16
22
|
identity_map = if record.respond_to?(:active_version_audit_identity_map)
|
|
17
23
|
record.active_version_audit_identity_map
|
|
18
24
|
else
|
|
19
|
-
|
|
20
|
-
if primary_keys.one?
|
|
21
|
-
{"#{auditable_column}_id" => record.id}
|
|
22
|
-
else
|
|
23
|
-
primary_keys.zip(primary_keys.map { |column| record[column] }).to_h
|
|
24
|
-
end
|
|
25
|
+
identity_map_for_auditable_column(record, auditable_column)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
query = audit_class.where({"#{auditable_column}_type" => record.class.name}.merge(identity_map))
|
|
@@ -39,23 +40,18 @@ module ActiveVersion
|
|
|
39
40
|
# @return [ActiveRecord::Relation] Translation relation
|
|
40
41
|
def translations(record, opts = {})
|
|
41
42
|
translation_class = record.class.translation_class
|
|
42
|
-
return
|
|
43
|
+
return record.class.none unless translation_class
|
|
43
44
|
|
|
44
45
|
identity_map = if record.respond_to?(:active_version_translation_identity_map)
|
|
45
46
|
record.active_version_translation_identity_map
|
|
46
47
|
else
|
|
47
48
|
keys = Array(translation_class.source_foreign_key).map(&:to_s)
|
|
48
|
-
|
|
49
|
-
{keys.first => record.id}
|
|
50
|
-
else
|
|
51
|
-
values = Array(record.class.primary_key).map { |column| record[column] }
|
|
52
|
-
keys.zip(values).to_h
|
|
53
|
-
end
|
|
49
|
+
identity_map_for_foreign_keys(record, keys)
|
|
54
50
|
end
|
|
55
51
|
query = translation_class.where(identity_map)
|
|
56
52
|
|
|
57
53
|
if opts[:locale]
|
|
58
|
-
locale_column =
|
|
54
|
+
locale_column = column_for_cached(record.class, :translations, :locale)
|
|
59
55
|
query = query.where(locale_column => opts[:locale])
|
|
60
56
|
end
|
|
61
57
|
|
|
@@ -69,29 +65,44 @@ module ActiveVersion
|
|
|
69
65
|
# @return [ActiveRecord::Relation] Revision relation
|
|
70
66
|
def revisions(record, opts = {})
|
|
71
67
|
revision_class = record.class.revision_class
|
|
72
|
-
return
|
|
68
|
+
return record.class.none unless revision_class
|
|
73
69
|
|
|
74
70
|
identity_map = if record.respond_to?(:active_version_revision_identity_map)
|
|
75
71
|
record.active_version_revision_identity_map
|
|
76
72
|
else
|
|
77
73
|
keys = Array(revision_class.source_foreign_key).map(&:to_s)
|
|
78
|
-
|
|
79
|
-
{keys.first => record.id}
|
|
80
|
-
else
|
|
81
|
-
values = Array(record.class.primary_key).map { |column| record[column] }
|
|
82
|
-
keys.zip(values).to_h
|
|
83
|
-
end
|
|
74
|
+
identity_map_for_foreign_keys(record, keys)
|
|
84
75
|
end
|
|
85
76
|
query = revision_class.where(identity_map)
|
|
86
77
|
|
|
87
78
|
if opts[:version]
|
|
88
|
-
version_column =
|
|
79
|
+
version_column = column_for_cached(record.class, :revisions, :version)
|
|
89
80
|
query = query.where(version_column => opts[:version])
|
|
90
81
|
end
|
|
91
82
|
|
|
92
|
-
version_column =
|
|
83
|
+
version_column = column_for_cached(record.class, :revisions, :version)
|
|
93
84
|
query.order(version_column => :asc)
|
|
94
85
|
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def identity_map_for_foreign_keys(record, foreign_keys)
|
|
90
|
+
if foreign_keys.one?
|
|
91
|
+
{foreign_keys.first => record.id}
|
|
92
|
+
else
|
|
93
|
+
values = Array(record.class.primary_key).map { |column| record[column] }
|
|
94
|
+
foreign_keys.zip(values).to_h
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def identity_map_for_auditable_column(record, auditable_column)
|
|
99
|
+
primary_keys = Array(record.class.primary_key).map(&:to_s)
|
|
100
|
+
if primary_keys.one?
|
|
101
|
+
{"#{auditable_column}_id" => record.id}
|
|
102
|
+
else
|
|
103
|
+
primary_keys.zip(primary_keys.map { |column| record[column] }).to_h
|
|
104
|
+
end
|
|
105
|
+
end
|
|
95
106
|
end
|
|
96
107
|
end
|
|
97
108
|
end
|
|
@@ -9,6 +9,14 @@ module ActiveVersion
|
|
|
9
9
|
# Expose configuration in Rails config
|
|
10
10
|
config.active_version = ActiveVersion.config
|
|
11
11
|
|
|
12
|
+
# Drop fiber/thread persistent context when the request or job unit ends so a
|
|
13
|
+
# reused Puma/Sidekiq thread does not stamp the previous actor onto later audits.
|
|
14
|
+
initializer "active_version.clear_context_on_complete" do |app|
|
|
15
|
+
app.executor.to_complete do
|
|
16
|
+
ActiveVersion.clear_context!
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
12
20
|
# Add rake tasks
|
|
13
21
|
rake_tasks do
|
|
14
22
|
load "active_version/tasks/active_version.rake"
|
|
@@ -37,17 +37,28 @@ module ActiveVersion
|
|
|
37
37
|
current_version + 1
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
# Refresh only columns with default functions (query optimization)
|
|
40
|
+
# Refresh only columns with default functions (query optimization).
|
|
41
|
+
# Never overwrite attributes the caller is about to save.
|
|
41
42
|
refreshable_columns = refreshable_column_names
|
|
42
43
|
if refreshable_columns.any?
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
pending_changes = if respond_to?(:changes_to_save) && changes_to_save.present?
|
|
45
|
+
changes_to_save.keys.map(&:to_s)
|
|
46
|
+
elsif respond_to?(:changed)
|
|
47
|
+
Array(changed).map(&:to_s)
|
|
48
|
+
else
|
|
49
|
+
[]
|
|
50
|
+
end
|
|
51
|
+
columns_to_refresh = refreshable_columns - pending_changes
|
|
52
|
+
if columns_to_refresh.any?
|
|
53
|
+
refreshed = self.class.select(columns_to_refresh).find(id)
|
|
54
|
+
columns_to_refresh.each do |col|
|
|
55
|
+
refreshed_value = if refreshed.respond_to?(col)
|
|
56
|
+
refreshed.public_send(col)
|
|
57
|
+
elsif refreshed.respond_to?(:[])
|
|
58
|
+
refreshed[col]
|
|
59
|
+
end
|
|
60
|
+
self[col] = refreshed_value
|
|
49
61
|
end
|
|
50
|
-
self[col] = refreshed_value
|
|
51
62
|
end
|
|
52
63
|
end
|
|
53
64
|
|
|
@@ -370,7 +381,16 @@ module ActiveVersion
|
|
|
370
381
|
last_revision ||= revisions_scope.order(version_column => :desc).first
|
|
371
382
|
return unless last_revision
|
|
372
383
|
|
|
373
|
-
#
|
|
384
|
+
# Callback path stores pre-change state. Keep the earliest pre-state and
|
|
385
|
+
# only extend the debounce window; overwriting would drop history (A -> B
|
|
386
|
+
# then B -> C within the window must leave A, not replace it with B).
|
|
387
|
+
if use_old_values
|
|
388
|
+
last_revision.class.where(id: last_revision.id).update_all(updated_at: timestamp)
|
|
389
|
+
revisions.reset
|
|
390
|
+
return
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# Update last revision with current attributes (filtered by only/except)
|
|
374
394
|
base_attrs = snapshot_base_attributes(use_old_values)
|
|
375
395
|
|
|
376
396
|
snapshot_attrs = if only_attrs
|
|
@@ -13,7 +13,9 @@ module ActiveVersion
|
|
|
13
13
|
at_version(version)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
# Get revision at specific time
|
|
16
|
+
# Get revision row that encodes state at specific time.
|
|
17
|
+
# Revision N stores pre-update state and is stamped at that update, so the
|
|
18
|
+
# state after an update is the next revision's payload (or current self).
|
|
17
19
|
def revision_at(time: nil)
|
|
18
20
|
return nil unless time
|
|
19
21
|
|
|
@@ -21,10 +23,7 @@ module ActiveVersion
|
|
|
21
23
|
raise ActiveVersion::FutureTimeError, "Future state cannot be known" if time_obj.future?
|
|
22
24
|
|
|
23
25
|
version_column = revision_version_column
|
|
24
|
-
|
|
25
|
-
# If requested time is before the first revision, return the earliest revision if present.
|
|
26
|
-
revision_entry ||= revisions_scope.order(version_column => :asc).first
|
|
27
|
-
revision_entry
|
|
26
|
+
revisions_scope.where("created_at > ?", time_obj).order(version_column => :asc).first
|
|
28
27
|
end
|
|
29
28
|
|
|
30
29
|
# Return a copy of record at specified time (read-only)
|
|
@@ -133,7 +132,9 @@ module ActiveVersion
|
|
|
133
132
|
|
|
134
133
|
def find_revision_by_time(time)
|
|
135
134
|
version_column = revision_version_column
|
|
136
|
-
|
|
135
|
+
# Earliest revision after +time+ holds the live state as of +time+
|
|
136
|
+
# (that row's payload is the pre-state of the next change).
|
|
137
|
+
revisions_scope.where("created_at > ?", time).order(version_column => :asc).first
|
|
137
138
|
end
|
|
138
139
|
|
|
139
140
|
def revisions_scope
|
|
@@ -190,27 +190,21 @@ module ActiveVersion
|
|
|
190
190
|
end
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
-
# Disable revisions for
|
|
193
|
+
# Disable revisions for this fiber/thread only (does not mutate callback chains)
|
|
194
194
|
def without_revisions
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
[]
|
|
199
|
-
end
|
|
200
|
-
callback_installed = callbacks.any?
|
|
201
|
-
|
|
202
|
-
skip_callback(:update, :before, :create_revision_before_update) if callback_installed
|
|
195
|
+
key = revision_disabled_depth_store_key
|
|
196
|
+
depth = ActiveVersion.store_get(key).to_i + 1
|
|
197
|
+
ActiveVersion.store_set(key, depth)
|
|
203
198
|
yield
|
|
204
199
|
ensure
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
end
|
|
200
|
+
depth = ActiveVersion.store_get(key).to_i - 1
|
|
201
|
+
depth = 0 if depth.negative?
|
|
202
|
+
ActiveVersion.store_set(key, depth)
|
|
209
203
|
end
|
|
210
204
|
|
|
211
|
-
# Enable revisions for a block
|
|
205
|
+
# Enable revisions for a block even when class revisions were disabled permanently
|
|
212
206
|
def with_revisions
|
|
213
|
-
revision_was_enabled =
|
|
207
|
+
revision_was_enabled = class_revision_enabled_without_skip?
|
|
214
208
|
enable_revisions
|
|
215
209
|
yield
|
|
216
210
|
ensure
|
|
@@ -218,10 +212,22 @@ module ActiveVersion
|
|
|
218
212
|
end
|
|
219
213
|
|
|
220
214
|
def class_revision_enabled?
|
|
221
|
-
|
|
215
|
+
return false if ActiveVersion.store_get(revision_disabled_depth_store_key).to_i.positive?
|
|
216
|
+
|
|
217
|
+
class_revision_enabled_without_skip?
|
|
222
218
|
end
|
|
223
219
|
public :class_revision_enabled?
|
|
224
220
|
|
|
221
|
+
def class_revision_enabled_without_skip?
|
|
222
|
+
@class_revision_enabled != false
|
|
223
|
+
end
|
|
224
|
+
private :class_revision_enabled_without_skip?
|
|
225
|
+
|
|
226
|
+
def revision_disabled_depth_store_key
|
|
227
|
+
:"active_version_#{name}_revision_disabled_depth"
|
|
228
|
+
end
|
|
229
|
+
private :revision_disabled_depth_store_key
|
|
230
|
+
|
|
225
231
|
private
|
|
226
232
|
|
|
227
233
|
def disable_revisions
|
|
@@ -328,7 +334,7 @@ module ActiveVersion
|
|
|
328
334
|
def revision_sql_value(value)
|
|
329
335
|
case value
|
|
330
336
|
when Hash, Array
|
|
331
|
-
|
|
337
|
+
ActiveVersion.json_encode(value)
|
|
332
338
|
when Time, DateTime
|
|
333
339
|
value.utc
|
|
334
340
|
when Date
|
|
@@ -362,17 +368,10 @@ module ActiveVersion
|
|
|
362
368
|
return false
|
|
363
369
|
end
|
|
364
370
|
|
|
365
|
-
|
|
366
|
-
class_enabled = if self.class.instance_variable_defined?(:@class_revision_enabled)
|
|
367
|
-
self.class.instance_variable_get(:@class_revision_enabled)
|
|
368
|
-
else
|
|
369
|
-
true # Default to enabled
|
|
370
|
-
end
|
|
371
|
-
return false unless class_enabled != false
|
|
371
|
+
return false unless self.class.class_revision_enabled?
|
|
372
372
|
|
|
373
|
-
# Check global enabled state
|
|
374
|
-
|
|
375
|
-
return false unless ActiveVersion.config.auditing_enabled
|
|
373
|
+
# Check global enabled state (respects per-fiber without_auditing depth)
|
|
374
|
+
return false unless ActiveVersion.auditing_enabled
|
|
376
375
|
|
|
377
376
|
# Get revision options (with defaults if not set)
|
|
378
377
|
options = self.class.revision_options || {auto: true, on: [:update]}
|
data/lib/active_version.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "logger"
|
|
2
|
+
require "json"
|
|
2
3
|
require "active_support"
|
|
3
4
|
begin
|
|
4
5
|
require "active_record"
|
|
@@ -17,6 +18,12 @@ require "active_version/unique_version_collision"
|
|
|
17
18
|
# Main entry point for ActiveVersion
|
|
18
19
|
module ActiveVersion
|
|
19
20
|
class Error < StandardError; end
|
|
21
|
+
|
|
22
|
+
# Stdlib JSON encoding helper (hot paths share this entry point).
|
|
23
|
+
def self.json_encode(value)
|
|
24
|
+
JSON.generate(value)
|
|
25
|
+
end
|
|
26
|
+
|
|
20
27
|
class ConfigurationError < Error; end
|
|
21
28
|
class VersionNotFoundError < Error; end
|
|
22
29
|
class ReadonlyVersionError < Error; end
|
|
@@ -69,8 +76,11 @@ module ActiveVersion
|
|
|
69
76
|
config
|
|
70
77
|
end
|
|
71
78
|
|
|
72
|
-
# Convenience methods for accessing configuration
|
|
79
|
+
# Convenience methods for accessing configuration.
|
|
80
|
+
# +without_auditing+ uses a per-fiber/thread depth so concurrent callers are not silenced.
|
|
73
81
|
def self.auditing_enabled
|
|
82
|
+
return false if store_get(:active_version_auditing_disabled_depth).to_i.positive?
|
|
83
|
+
|
|
74
84
|
config.auditing_enabled
|
|
75
85
|
end
|
|
76
86
|
|
|
@@ -100,7 +110,10 @@ module ActiveVersion
|
|
|
100
110
|
end
|
|
101
111
|
|
|
102
112
|
def self.context
|
|
103
|
-
# Merge persistent context with request-scoped context
|
|
113
|
+
# Merge persistent context (fiber/thread store) with request-scoped context.
|
|
114
|
+
# with_context ensure must restore only RequestStore.version_context so
|
|
115
|
+
# persistent keys are not copied into the request store and cannot shadow
|
|
116
|
+
# a later with_context!.
|
|
104
117
|
persistent = store_get(:active_version_persistent_context) || {}
|
|
105
118
|
request_scoped = RequestStore.version_context || {}
|
|
106
119
|
persistent.merge(request_scoped)
|
|
@@ -112,7 +125,8 @@ module ActiveVersion
|
|
|
112
125
|
end
|
|
113
126
|
|
|
114
127
|
# Transaction-aware context (uses PostgreSQL session variables)
|
|
115
|
-
# Accepts either a hash as first argument or keyword arguments
|
|
128
|
+
# Accepts either a hash as first argument or keyword arguments.
|
|
129
|
+
# Pass transactional: false to skip SET LOCAL even when PostgreSQL supports it.
|
|
116
130
|
def self.with_context(context = nil, transactional: true, **kwargs, &block)
|
|
117
131
|
raise ArgumentError, "with_context requires a block" unless block_given?
|
|
118
132
|
|
|
@@ -129,16 +143,15 @@ module ActiveVersion
|
|
|
129
143
|
raise ArgumentError, "context must be a hash or keyword arguments"
|
|
130
144
|
end
|
|
131
145
|
|
|
132
|
-
if transactional_context_supported?
|
|
133
|
-
# Use PostgreSQL session variables for transaction-aware context
|
|
146
|
+
if transactional && transactional_context_supported?
|
|
134
147
|
with_transactional_context(context_hash, &block)
|
|
135
148
|
else
|
|
136
|
-
# Fallback to thread-local context
|
|
137
149
|
with_thread_local_context(context_hash, &block)
|
|
138
150
|
end
|
|
139
151
|
end
|
|
140
152
|
|
|
141
|
-
# Persistent context (
|
|
153
|
+
# Persistent context for the current fiber/thread (use clear_context! or the
|
|
154
|
+
# Railtie executor hook so a reused server thread does not leak actors).
|
|
142
155
|
def self.with_context!(context)
|
|
143
156
|
raise ArgumentError, "context must be a hash" unless context.is_a?(Hash)
|
|
144
157
|
|
|
@@ -217,25 +230,26 @@ module ActiveVersion
|
|
|
217
230
|
|
|
218
231
|
def self.with_transactional_context(context, &block)
|
|
219
232
|
connection = Runtime.adapter.base_connection
|
|
220
|
-
|
|
233
|
+
old_request_context = RequestStore.version_context
|
|
221
234
|
old_block_context = store_get(:active_version_block_context)
|
|
222
235
|
enter_context_block!
|
|
223
236
|
|
|
224
237
|
# Set PostgreSQL session variable
|
|
225
238
|
if connection.open_transactions.positive?
|
|
226
|
-
|
|
239
|
+
payload = json_encode(context)
|
|
240
|
+
encoded_context = connection.quote(payload)
|
|
227
241
|
connection.execute("SET LOCAL active_version.context = #{encoded_context}")
|
|
228
242
|
end
|
|
229
243
|
|
|
230
|
-
#
|
|
231
|
-
|
|
244
|
+
# Merge into request-scoped store only (not persistent), so ensure restores cleanly
|
|
245
|
+
RequestStore.version_context = (old_request_context || {}).merge(context)
|
|
232
246
|
store_set(:active_version_block_context, context)
|
|
233
247
|
|
|
234
248
|
yield
|
|
235
249
|
ensure
|
|
236
250
|
# Context is automatically cleared on transaction rollback
|
|
237
|
-
#
|
|
238
|
-
|
|
251
|
+
# Restore request-scoped context only (do not copy persistent keys into RequestStore)
|
|
252
|
+
RequestStore.version_context = old_request_context
|
|
239
253
|
store_set(:active_version_block_context, old_block_context)
|
|
240
254
|
leave_context_block!
|
|
241
255
|
end
|
|
@@ -255,25 +269,27 @@ module ActiveVersion
|
|
|
255
269
|
end
|
|
256
270
|
|
|
257
271
|
def self.with_thread_local_context(context, &block)
|
|
258
|
-
|
|
272
|
+
old_request_context = RequestStore.version_context
|
|
259
273
|
old_block_context = store_get(:active_version_block_context)
|
|
260
274
|
enter_context_block!
|
|
261
|
-
|
|
275
|
+
RequestStore.version_context = (old_request_context || {}).merge(context)
|
|
262
276
|
store_set(:active_version_block_context, context)
|
|
263
277
|
yield
|
|
264
278
|
ensure
|
|
265
|
-
|
|
279
|
+
RequestStore.version_context = old_request_context
|
|
266
280
|
store_set(:active_version_block_context, old_block_context)
|
|
267
281
|
leave_context_block!
|
|
268
282
|
end
|
|
269
283
|
|
|
270
|
-
# Disable
|
|
284
|
+
# Disable auditing for this fiber/thread only (nested-safe depth counter)
|
|
271
285
|
def self.without_auditing
|
|
272
|
-
|
|
273
|
-
|
|
286
|
+
depth = store_get(:active_version_auditing_disabled_depth).to_i + 1
|
|
287
|
+
store_set(:active_version_auditing_disabled_depth, depth)
|
|
274
288
|
yield
|
|
275
289
|
ensure
|
|
276
|
-
|
|
290
|
+
depth = store_get(:active_version_auditing_disabled_depth).to_i - 1
|
|
291
|
+
depth = 0 if depth.negative?
|
|
292
|
+
store_set(:active_version_auditing_disabled_depth, depth)
|
|
277
293
|
end
|
|
278
294
|
|
|
279
295
|
def self.disable_auditing
|
|
@@ -357,6 +373,6 @@ module ActiveVersion
|
|
|
357
373
|
end
|
|
358
374
|
|
|
359
375
|
# Load Rails integration if available
|
|
360
|
-
if defined?(Rails)
|
|
376
|
+
if defined?(Rails::Railtie)
|
|
361
377
|
require "active_version/railtie"
|
|
362
378
|
end
|
|
@@ -4,13 +4,12 @@ class Create<%= revision_class_name.pluralize %> < ActiveRecord::Migration<%= mi
|
|
|
4
4
|
t.references :<%= file_name %>, null: false, foreign_key: true, index: false
|
|
5
5
|
t.integer :<%= version_column %>, null: false
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
# Add other columns as needed
|
|
7
|
+
source_primary_key = connection.primary_key(:<%= source_table_name %>)
|
|
8
|
+
connection.columns(:<%= source_table_name %>).each do |column|
|
|
9
|
+
next if [source_primary_key, "created_at", "updated_at"].include?(column.name)
|
|
10
|
+
|
|
11
|
+
t.column column.name, column.sql_type, limit: column.limit, precision: column.precision, scale: column.scale
|
|
12
|
+
end
|
|
14
13
|
|
|
15
14
|
t.timestamps
|
|
16
15
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
module ActiveVersion
|
|
2
2
|
class Configuration
|
|
3
|
+
ERROR_BEHAVIORS: Array[Symbol]
|
|
4
|
+
|
|
3
5
|
attr_accessor auditing_enabled: bool
|
|
4
6
|
attr_accessor current_user_method: Symbol | String
|
|
5
7
|
attr_accessor ignored_attributes: Array[String]
|
|
@@ -13,6 +15,7 @@ module ActiveVersion
|
|
|
13
15
|
attr_accessor revision_version_column: Symbol | String
|
|
14
16
|
attr_accessor revision_foreign_key_suffix: String
|
|
15
17
|
|
|
18
|
+
attr_accessor default_audit_class: Class?
|
|
16
19
|
attr_accessor audit_storage: :json_column | :yaml_column | :mirror_columns
|
|
17
20
|
attr_accessor audit_action_column: Symbol | String
|
|
18
21
|
attr_accessor audit_changes_column: Symbol | String
|
|
@@ -32,7 +35,6 @@ module ActiveVersion
|
|
|
32
35
|
attr_accessor audit_error_behavior: Symbol
|
|
33
36
|
attr_accessor revision_error_behavior: Symbol
|
|
34
37
|
attr_accessor debounce_time: Numeric?
|
|
35
|
-
attr_accessor partition_schema_guards_enabled: bool
|
|
36
38
|
|
|
37
39
|
def validate!: () -> void
|
|
38
40
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_version
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrei Makarov
|
|
@@ -97,14 +97,14 @@ dependencies:
|
|
|
97
97
|
requirements:
|
|
98
98
|
- - ">="
|
|
99
99
|
- !ruby/object:Gem::Version
|
|
100
|
-
version: 2.9.
|
|
100
|
+
version: 2.9.6
|
|
101
101
|
type: :development
|
|
102
102
|
prerelease: false
|
|
103
103
|
version_requirements: !ruby/object:Gem::Requirement
|
|
104
104
|
requirements:
|
|
105
105
|
- - ">="
|
|
106
106
|
- !ruby/object:Gem::Version
|
|
107
|
-
version: 2.9.
|
|
107
|
+
version: 2.9.6
|
|
108
108
|
- !ruby/object:Gem::Dependency
|
|
109
109
|
name: pg
|
|
110
110
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -153,14 +153,14 @@ dependencies:
|
|
|
153
153
|
requirements:
|
|
154
154
|
- - ">="
|
|
155
155
|
- !ruby/object:Gem::Version
|
|
156
|
-
version: 2.2.
|
|
156
|
+
version: 2.2.9
|
|
157
157
|
type: :development
|
|
158
158
|
prerelease: false
|
|
159
159
|
version_requirements: !ruby/object:Gem::Requirement
|
|
160
160
|
requirements:
|
|
161
161
|
- - ">="
|
|
162
162
|
- !ruby/object:Gem::Version
|
|
163
|
-
version: 2.2.
|
|
163
|
+
version: 2.2.9
|
|
164
164
|
- !ruby/object:Gem::Dependency
|
|
165
165
|
name: prosopite
|
|
166
166
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -443,7 +443,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
443
443
|
requirements:
|
|
444
444
|
- - ">="
|
|
445
445
|
- !ruby/object:Gem::Version
|
|
446
|
-
version: '3.
|
|
446
|
+
version: '3.1'
|
|
447
447
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
448
448
|
requirements:
|
|
449
449
|
- - ">="
|