exwiw 1.4.0 → 2.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 +10 -0
- data/README.md +3 -1
- data/lib/exwiw/maskable/active_record_model.rb +21 -10
- data/lib/exwiw/maskable/mongoid_document.rb +11 -5
- data/lib/exwiw/maskable.rb +2 -2
- data/lib/exwiw/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c122380fdc3d5ee3b9bf361f9d88c9a7d374c3c1fdbe6129fa4f3f54266f69d4
|
|
4
|
+
data.tar.gz: f2840577552485a11a36a38166a68fea54cd8963348cded1e68002e024b06a1d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62462591b52cba72ca739e8bca75fb76fb984452b2a23775123c8ad5cac3d8d4f83de45cd194c21b8c9e217c6d078522301c732307705cbd3ecb75c880a1733f
|
|
7
|
+
data.tar.gz: 03fe68f3d5e80ca064bb8196d87a6416d02581421e0083d5b2f47a1255241ff43069eb609b774ff74a594051e726af5f325421ee3fef19b228ccdac422860c92
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [2.0.0] - 2026-11-22
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`mask_with_exwiw!` takes an optional block, told of each batch once it is done.** It yields the batch's summary and statements (`mask_with_exwiw!(dry_run: true) { |summary, statements| ... }`), after the batch's transaction and with or without `dry_run`. Given a block, it keeps no statements (`Result#statements` is nil) and returns the total summary, so the statements of a large relation can be handled batch by batch without holding them all. Mongoid masks in one batch, so the block is called once.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- **BREAKING — `mask_with_exwiw!` returns the statements only with `dry_run: true`; otherwise `Result#statements` is nil.** It used to keep every statement it executed and return them all, so masking a large relation in batches still held the SQL of every batch in memory until it returned. Each batch's statements are now dropped once executed. Call it with `dry_run: true` to see the statements.
|
|
14
|
+
|
|
5
15
|
## [1.4.0] - 2026-11-22
|
|
6
16
|
|
|
7
17
|
### Added
|
data/README.md
CHANGED
|
@@ -162,9 +162,11 @@ User.where(organization_id: 1).mask_with_exwiw!
|
|
|
162
162
|
User.where(organization_id: 1).mask_with_exwiw!(batch: false) # all rows at once, in no transaction of its own
|
|
163
163
|
user.mask_with_exwiw!
|
|
164
164
|
User.where(id: ids).mask_with_exwiw!(dry_run: true).statements # the UPDATEs, not executed
|
|
165
|
+
User.where(organization_id: 1).mask_with_exwiw! { |summary, statements| audit(statements) } # told of each batch once done
|
|
165
166
|
```
|
|
166
167
|
|
|
167
|
-
- It applies the masks `mask-update` would print, reading and updating through the model's connection, and returns `Exwiw::Maskable::Result` (`summary`, `statements`). `touch:` (default `true`) also sets `updated_at` to the current time; `dry_run: true` only returns the statements: SQL UPDATEs, or for MongoDB `Exwiw::Adapter::MongodbAdapter::MaskUpdateBulkWrite`s (`collection_name`, `operations`, and `#to_extended_json` for the line `mask-update` prints), and `profile:` applies a [profile](#profile-overlays).
|
|
168
|
+
- It applies the masks `mask-update` would print, reading and updating through the model's connection, and returns `Exwiw::Maskable::Result` (`summary`, `statements`). `touch:` (default `true`) also sets `updated_at` to the current time; `dry_run: true` only returns the statements (`statements` is nil without it): SQL UPDATEs, or for MongoDB `Exwiw::Adapter::MongodbAdapter::MaskUpdateBulkWrite`s (`collection_name`, `operations`, and `#to_extended_json` for the line `mask-update` prints), and `profile:` applies a [profile](#profile-overlays).
|
|
169
|
+
- Given a block, it yields each batch's summary and statements once the batch is done (after its transaction; with or without `dry_run`), and keeps none of them: `statements` is nil and `summary` totals the batches. Use it to handle the statements of a large relation without holding them all in memory. Mongoid masks in one batch, so the block is called once.
|
|
168
170
|
- A relation with no condition of its own (`User`, `User.all`, `User.unscoped`, or only the default scope) is refused, as it would mask every row. A default scope that embeds a changing value such as the current time is not recognized as the default scope, so the check does not catch it. Rows a default scope hides (e.g. soft-deleted ones) are not masked unless the relation includes them (`with_deleted`).
|
|
169
171
|
- With `batch:` (default `true`), an ActiveRecord relation is masked in batches of 1,000 rows (`in_batches`, so the relation's order is ignored), each in its own transaction, which joins the caller's if one is open. A batch that fails changes none of its rows, but the batches before it stay masked; wrap the call in a transaction to make the whole call all-or-nothing. With `batch: false` it opens no transaction and builds every statement before running any, so a mask that cannot be applied changes nothing. Mongoid opens none either: it runs in the session Mongoid has open, if any. The updates bypass callbacks, so nothing that runs on save (search indexes, events, caches) sees them. Mongoid reads from the primary, and an embedded document class is refused (mask the parent).
|
|
170
172
|
- `Model.exwiw_maskable?` (`profile:` as above) tells, from the schema alone and without reading the table, whether `mask_with_exwiw!` would mask the model's rows: it is false when the table is not declared, is `ignore: true`, Rails-managed or keyed by a composite primary key, declares no mask, or masks or does not select its primary key (Mongoid: also for an embedded document class). A caller masking many models can skip the ones it would refuse, while `mask_with_exwiw!` still raises for them:
|
|
@@ -12,6 +12,7 @@ module Exwiw
|
|
|
12
12
|
#
|
|
13
13
|
# User.where(id: [1, 2, 3]).mask_with_exwiw!
|
|
14
14
|
# User.where(organization_id: 1).mask_with_exwiw!(dry_run: true).statements
|
|
15
|
+
# User.where(organization_id: 1).mask_with_exwiw! { |summary, statements| log(statements) }
|
|
15
16
|
# User.all.mask_with_exwiw! # refused: nothing narrows the rows
|
|
16
17
|
# user.mask_with_exwiw!
|
|
17
18
|
#
|
|
@@ -24,13 +25,14 @@ module Exwiw
|
|
|
24
25
|
# Masks this record's row in place and, unless `dry_run`, reloads the record.
|
|
25
26
|
#
|
|
26
27
|
# @param options [Hash] as for ClassMethods#mask_with_exwiw!
|
|
28
|
+
# @yield as for ClassMethods#mask_with_exwiw!
|
|
27
29
|
# @return [Exwiw::Maskable::Result]
|
|
28
30
|
# @raise [Exwiw::MaskUpdateRunner::Error] when the record is not persisted, or its row cannot be masked as declared
|
|
29
|
-
def mask_with_exwiw!(**options)
|
|
31
|
+
def mask_with_exwiw!(**options, &block)
|
|
30
32
|
raise MaskUpdateRunner::Error, "#{self.class.name} is not persisted, so it has no row to mask" unless persisted?
|
|
31
33
|
|
|
32
34
|
# unscoped: a default scope must not hide the record's own row.
|
|
33
|
-
result = self.class.unscoped.where(self.class.primary_key => id_in_database).mask_with_exwiw!(**options)
|
|
35
|
+
result = self.class.unscoped.where(self.class.primary_key => id_in_database).mask_with_exwiw!(**options, &block)
|
|
34
36
|
reload unless options[:dry_run]
|
|
35
37
|
result
|
|
36
38
|
end
|
|
@@ -47,9 +49,13 @@ module Exwiw
|
|
|
47
49
|
# relation's order) and each batch in its own transaction, which joins the caller's when one is open; a
|
|
48
50
|
# failure leaves the batches before it masked. With false, the rows are read and updated at once in no
|
|
49
51
|
# transaction of its own; wrap the call in one to make it all-or-nothing
|
|
50
|
-
# @
|
|
52
|
+
# @yield [summary, statements] once each batch is done (after its transaction, or after the single batch
|
|
53
|
+
# with `batch: false`); the statements are then not kept in the result
|
|
54
|
+
# @yieldparam summary [Exwiw::MaskUpdateRunner::Summary] the batch's summary
|
|
55
|
+
# @yieldparam statements [Array<String>] the batch's SQL UPDATEs, executed unless `dry_run`
|
|
56
|
+
# @return [Exwiw::Maskable::Result] statements are SQL UPDATEs with `dry_run` and no block, nil otherwise
|
|
51
57
|
# @raise [Exwiw::MaskUpdateRunner::Error] when nothing narrows the rows, or they cannot be masked as declared; nothing of the batch is updated then
|
|
52
|
-
def mask_with_exwiw!(touch: true, dry_run: false, profile: nil, batch: true)
|
|
58
|
+
def mask_with_exwiw!(touch: true, dry_run: false, profile: nil, batch: true, &block)
|
|
53
59
|
scope = current_scope
|
|
54
60
|
if scope.nil? || scope.where_clause.empty? || scope.where_clause == default_scoped.where_clause
|
|
55
61
|
raise MaskUpdateRunner::Error,
|
|
@@ -58,9 +64,9 @@ module Exwiw
|
|
|
58
64
|
|
|
59
65
|
options = { touch: touch, dry_run: dry_run, profile: profile, batch: batch }
|
|
60
66
|
# with_connection appeared in ActiveRecord 7.2.
|
|
61
|
-
return mask_with_connection(connection, **options) unless respond_to?(:with_connection)
|
|
67
|
+
return mask_with_connection(connection, **options, &block) unless respond_to?(:with_connection)
|
|
62
68
|
|
|
63
|
-
with_connection { |connection| mask_with_connection(connection, **options) }
|
|
69
|
+
with_connection { |connection| mask_with_connection(connection, **options, &block) }
|
|
64
70
|
end
|
|
65
71
|
|
|
66
72
|
# Whether the schema lets `mask_with_exwiw!` mask this model's rows: the table is declared, is not
|
|
@@ -76,7 +82,7 @@ module Exwiw
|
|
|
76
82
|
Maskable.maskable?(self, TableConfig, table_name, profile)
|
|
77
83
|
end
|
|
78
84
|
|
|
79
|
-
private def mask_with_connection(connection, touch:, dry_run:, profile:, batch
|
|
85
|
+
private def mask_with_connection(connection, touch:, dry_run:, profile:, batch:, &block)
|
|
80
86
|
now = connection.quoted_date(current_time_from_proper_timezone) if touch
|
|
81
87
|
source = DataSource.given(Maskable::ActiveRecordModel.exwiw_adapter_name(connection), connection.raw_connection)
|
|
82
88
|
schema = Maskable.schema_for(self, source, profile)
|
|
@@ -89,18 +95,23 @@ module Exwiw
|
|
|
89
95
|
statements.each { |sql| connection.execute(sql) } unless dry_run
|
|
90
96
|
[summary, statements]
|
|
91
97
|
end
|
|
98
|
+
# Only a dry run without a block keeps the statements, so batches do not pile them up in memory otherwise.
|
|
99
|
+
done = lambda do |(summary, statements)|
|
|
100
|
+
block&.call(summary, statements)
|
|
101
|
+
[summary, dry_run && !block ? statements : nil]
|
|
102
|
+
end
|
|
92
103
|
|
|
93
104
|
parts =
|
|
94
105
|
if batch
|
|
95
106
|
in_batches(of: MaskUpdateRunner::CHUNK_SIZE).map do |relation|
|
|
96
107
|
ids = relation.pluck(arel_table[primary_key])
|
|
97
|
-
dry_run ? mask.(ids) : connection.transaction { mask.(ids) }
|
|
108
|
+
done.(dry_run ? mask.(ids) : connection.transaction { mask.(ids) })
|
|
98
109
|
end
|
|
99
110
|
else
|
|
100
|
-
[mask.(all.pluck(arel_table[primary_key]))]
|
|
111
|
+
[done.(mask.(all.pluck(arel_table[primary_key])))]
|
|
101
112
|
end
|
|
102
113
|
summary = MaskUpdateRunner::Summary.merge(table_name, parts.map(&:first))
|
|
103
|
-
Result.new(summary: summary, statements: parts.flat_map(&:last))
|
|
114
|
+
Result.new(summary: summary, statements: dry_run && !block ? parts.flat_map(&:last) : nil)
|
|
104
115
|
end
|
|
105
116
|
end
|
|
106
117
|
|
|
@@ -25,9 +25,10 @@ module Exwiw
|
|
|
25
25
|
# Masks this document in place and, unless `dry_run`, reloads it.
|
|
26
26
|
#
|
|
27
27
|
# @param options [Hash] as for ClassMethods#mask_with_exwiw!
|
|
28
|
+
# @yield as for ClassMethods#mask_with_exwiw!
|
|
28
29
|
# @return [Exwiw::Maskable::Result]
|
|
29
30
|
# @raise [Exwiw::MaskUpdateRunner::Error] when the document is not persisted, or cannot be masked as declared
|
|
30
|
-
def mask_with_exwiw!(**options)
|
|
31
|
+
def mask_with_exwiw!(**options, &block)
|
|
31
32
|
raise MaskUpdateRunner::Error, "#{self.class.name} is not persisted, so it has no document to mask" unless persisted?
|
|
32
33
|
if embedded?
|
|
33
34
|
raise MaskUpdateRunner::Error,
|
|
@@ -35,7 +36,7 @@ module Exwiw
|
|
|
35
36
|
end
|
|
36
37
|
|
|
37
38
|
# unscoped: a default scope must not hide the document itself.
|
|
38
|
-
result = self.class.unscoped.where(_id: _id).mask_with_exwiw!(**options)
|
|
39
|
+
result = self.class.unscoped.where(_id: _id).mask_with_exwiw!(**options, &block)
|
|
39
40
|
reload unless options[:dry_run]
|
|
40
41
|
result
|
|
41
42
|
end
|
|
@@ -48,11 +49,15 @@ module Exwiw
|
|
|
48
49
|
# @param dry_run [Boolean] only build the bulk writes, without executing them
|
|
49
50
|
# @param profile [String, nil] the schema profile to apply. The schema is found through the class methods
|
|
50
51
|
# `exwiw_config_path` and `exwiw_schema_dir` when the class defines them (see Exwiw::Maskable.schema_for)
|
|
52
|
+
# @yield [summary, statements] once the documents are written (one batch: documents are not masked in batches);
|
|
53
|
+
# the statements are then not kept in the result
|
|
54
|
+
# @yieldparam summary [Exwiw::MaskUpdateRunner::Summary]
|
|
55
|
+
# @yieldparam statements [Array<Exwiw::Adapter::MongodbAdapter::MaskUpdateBulkWrite>] written unless `dry_run`
|
|
51
56
|
# @return [Exwiw::Maskable::Result] statements are Exwiw::Adapter::MongodbAdapter::MaskUpdateBulkWrite
|
|
52
|
-
# (`#to_extended_json` gives the line `mask-update` prints)
|
|
57
|
+
# (`#to_extended_json` gives the line `mask-update` prints) with `dry_run` and no block, nil otherwise
|
|
53
58
|
# @raise [Exwiw::MaskUpdateRunner::Error] when nothing narrows the documents, the class is embedded, or the documents
|
|
54
59
|
# cannot be masked as declared; nothing is written then
|
|
55
|
-
def mask_with_exwiw!(touch: true, dry_run: false, profile: nil)
|
|
60
|
+
def mask_with_exwiw!(touch: true, dry_run: false, profile: nil, &block)
|
|
56
61
|
if embedded?
|
|
57
62
|
raise MaskUpdateRunner::Error,
|
|
58
63
|
"#{name} is embedded; its masks apply through the parent's documents, so mask the parent instead"
|
|
@@ -82,7 +87,8 @@ module Exwiw
|
|
|
82
87
|
client[bulk_write.collection_name].bulk_write(bulk_write.driver_operations, session ? { session: session } : {})
|
|
83
88
|
end
|
|
84
89
|
end
|
|
85
|
-
|
|
90
|
+
block&.call(summary, bulk_writes)
|
|
91
|
+
Result.new(summary: summary, statements: dry_run && !block ? bulk_writes : nil)
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
# Whether the schema lets `mask_with_exwiw!` mask this model's documents: the class is not embedded, and its
|
data/lib/exwiw/maskable.rb
CHANGED
|
@@ -7,8 +7,8 @@ module Exwiw
|
|
|
7
7
|
# connection. Include Exwiw::Maskable::ActiveRecordModel or Exwiw::Maskable::MongoidDocument in the model (or
|
|
8
8
|
# ApplicationRecord); nothing is added to a model that does not include one.
|
|
9
9
|
module Maskable
|
|
10
|
-
# What `mask_with_exwiw!` returns: the runner's summary, and
|
|
11
|
-
# execute
|
|
10
|
+
# What `mask_with_exwiw!` returns: the runner's summary, and with `dry_run` and no block the statements it would
|
|
11
|
+
# execute (SQL UPDATEs, or MongodbAdapter::MaskUpdateBulkWrite for MongoDB); nil otherwise.
|
|
12
12
|
Result = Data.define(:summary, :statements)
|
|
13
13
|
|
|
14
14
|
# The schema for a model, from the process-wide cache. A model (or its base class or concern) may define the class
|
data/lib/exwiw/version.rb
CHANGED