remont 0.1.0 → 0.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 +6 -0
- data/README.md +26 -10
- data/lib/remont/config.rb +1 -7
- data/lib/remont/record_processor.rb +8 -7
- data/lib/remont/schema.rb +27 -7
- data/lib/remont/tasks/remont.rake +10 -2
- data/lib/remont/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc06eb6d1dddd55ec43542c7ce574c3e4b2f775230d4005aa4e72a2352ce24df
|
4
|
+
data.tar.gz: 403813fb176d0c028075298808a897f149de6116bacb89e7bf92dce5633d743f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71af67ff58ba821f6c12ca67ceb038024c88536afbeb1173c7cce0b34cca9fca63da47e9543932a2f744f508fb8c1aca7c4656ef9706c188956f658689ed1244
|
7
|
+
data.tar.gz: bf33a5f1ccbf9b88c0ce06852ec12fda9898c0186dfe1f5cb5a09408f680cb4e04eade3519791f1ac6dae5fd850105fb92ba645ec9b18bca2a4838126d5c7cdb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -21,14 +21,12 @@ Add `config/initializers/remont.rb`
|
|
21
21
|
```ruby
|
22
22
|
Remont.setup do |config|
|
23
23
|
config.process_timestamp_attribute = :processed_at
|
24
|
-
config.script_path = 'db/remont.rb'
|
25
24
|
end
|
26
25
|
```
|
27
26
|
|
28
|
-
| Option | Default value | Description
|
29
|
-
| --- | :---: | ---
|
30
|
-
| `process_timestamp_attribute` |
|
31
|
-
| `script_path` | `'db/remont.rb'` | entry point for the schema processing task |
|
27
|
+
| Option | Default value | Description |
|
28
|
+
| --- | :---: | --- |
|
29
|
+
| `process_timestamp_attribute` | `nil` | processing status attribute identifier |
|
32
30
|
|
33
31
|
On successful record processing, `process_timestamp_attribute` on the record will be set to the current processing time (`Time.now.getlocal`).
|
34
32
|
|
@@ -38,11 +36,10 @@ In the following example, the intention is to simulate anonymization of the `use
|
|
38
36
|
```ruby
|
39
37
|
# config/initializers/remont.rb
|
40
38
|
Remont.setup do |config|
|
41
|
-
config.process_timestamp_attribute = :
|
42
|
-
config.script_path = 'db/anonymize.rb'
|
39
|
+
config.process_timestamp_attribute = :anonymized_at
|
43
40
|
end
|
44
41
|
```
|
45
|
-
- define processing script
|
42
|
+
- define the processing script
|
46
43
|
```ruby
|
47
44
|
# db/anonymize.rb
|
48
45
|
schema model: User do
|
@@ -53,8 +50,12 @@ schema model: Order do
|
|
53
50
|
attribute(:billing_address) { '23 Wall Street, NY' }
|
54
51
|
end
|
55
52
|
```
|
56
|
-
- and run `
|
53
|
+
- and run `remont` rake task with the path to the processing script
|
54
|
+
```bash
|
55
|
+
bundle exec rake "remont[db/anonymize.rb]"
|
56
|
+
```
|
57
57
|
|
58
|
+
Passing the script path to the rake task is mandatory.
|
58
59
|
Running the rake task would result in updating `email` (and `anonymized_at`) column for each row in the `users` table, and `billing_address` column for each row in the `orders` table.
|
59
60
|
|
60
61
|
### Schema
|
@@ -77,14 +78,29 @@ schema model: Order do
|
|
77
78
|
scope { |scope| scope.where(status: :active) }
|
78
79
|
end
|
79
80
|
```
|
81
|
+
### Recording the processing end time
|
82
|
+
Library supports an option to store the processing end time of a record. Enable the behavior by configuring a processing status attribute identifier for the schema. Configure the attribute identifier
|
83
|
+
- globally (`Remont::Config#process_timestamp_attribute`, for all schemas)
|
84
|
+
- or individually per schema (in the schema DSL, overrides the global setting)
|
85
|
+
```ruby
|
86
|
+
schema model: User, process_timestamp_attribute: :anonymized_at do
|
87
|
+
end
|
88
|
+
|
89
|
+
schema model: Order do
|
90
|
+
with_process_timestamp_attribute :anonymized_at
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Set process status attribute to `nil` to disable the behavior.
|
80
95
|
### Skipping the processed records
|
81
|
-
In some cases, it's desirable to skip already processed records. You can
|
96
|
+
In some cases, it's desirable to skip already processed records. You can enable this behavior by declaring `without_processed` within the `schema` block. When declared, the processing dataset query will be extended with a condition that excludes already processed records.
|
82
97
|
```ruby
|
83
98
|
schema model: User do
|
84
99
|
without_processed
|
85
100
|
# ...
|
86
101
|
end
|
87
102
|
```
|
103
|
+
Configure processing status attribute before declaring the `without_processed`. An error will be raised otherwise.
|
88
104
|
### Callbacks
|
89
105
|
Custom pre-processing or post-processing behavior can be declared using `before` and `after` callbacks.
|
90
106
|
```ruby
|
data/lib/remont/config.rb
CHANGED
@@ -1,16 +1,10 @@
|
|
1
1
|
module Remont
|
2
2
|
class Config
|
3
|
-
DEFAULT_SCRIPT_PATH = 'db/remont.rb'.freeze
|
4
|
-
|
5
3
|
# @return [Symbol]
|
6
4
|
attr_accessor :process_timestamp_attribute
|
7
5
|
|
8
|
-
# @return [String]
|
9
|
-
attr_accessor :script_path
|
10
|
-
|
11
6
|
def initialize
|
12
|
-
@process_timestamp_attribute =
|
13
|
-
@script_path = DEFAULT_SCRIPT_PATH
|
7
|
+
@process_timestamp_attribute = nil
|
14
8
|
end
|
15
9
|
end
|
16
10
|
end
|
@@ -2,11 +2,11 @@ module Remont
|
|
2
2
|
class RecordProcessor
|
3
3
|
NOOP = proc {}
|
4
4
|
|
5
|
-
# @param [
|
5
|
+
# @param [Remont::Schema] schema
|
6
6
|
# @param [Proc] before
|
7
7
|
# @param [Proc] after
|
8
|
-
def initialize(
|
9
|
-
@
|
8
|
+
def initialize(schema, before, after)
|
9
|
+
@schema = schema
|
10
10
|
@before = before || NOOP
|
11
11
|
@after = after || NOOP
|
12
12
|
end
|
@@ -25,12 +25,13 @@ module Remont
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
attr_reader :
|
28
|
+
attr_reader :schema
|
29
29
|
attr_reader :before
|
30
30
|
attr_reader :after
|
31
31
|
|
32
32
|
def processed_attributes(record)
|
33
|
-
|
33
|
+
schema
|
34
|
+
.attributes
|
34
35
|
.select { |attribute| record.send(attribute.name).present? }
|
35
36
|
.reduce(default_processed_attributes) do |memo, attribute|
|
36
37
|
memo.merge(attribute.name => attribute.process(record.send(attribute.name), record))
|
@@ -38,10 +39,10 @@ module Remont
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def default_processed_attributes
|
41
|
-
return {} unless
|
42
|
+
return {} unless schema.process_timestamp_attribute
|
42
43
|
|
43
44
|
{
|
44
|
-
|
45
|
+
schema.process_timestamp_attribute => Time.now.getlocal
|
45
46
|
}
|
46
47
|
end
|
47
48
|
end
|
data/lib/remont/schema.rb
CHANGED
@@ -1,16 +1,23 @@
|
|
1
1
|
module Remont
|
2
2
|
class Schema
|
3
|
-
|
4
|
-
|
3
|
+
MISSING_PROCESSING_STATUS_ATTR = "Processing status attribute isn't configured".freeze
|
4
|
+
|
5
|
+
# @return [Array<Remont::Attribute>]
|
6
|
+
attr_reader :attributes
|
7
|
+
|
8
|
+
# @return [Nil, String, Symbol]
|
9
|
+
attr_reader :process_timestamp_attribute
|
5
10
|
|
6
11
|
# @param [Hash] opts
|
7
12
|
# @option [Class] :model
|
8
13
|
# @option [Proc] :scope
|
14
|
+
# @option [String, Symbol] :process_timestamp_attribute
|
9
15
|
# @param [Proc] block
|
10
16
|
def initialize(opts = {}, &block)
|
11
17
|
@model = opts.fetch(:model)
|
12
|
-
@model_scope = opts.fetch(:scope,
|
13
|
-
@without_processed_scope =
|
18
|
+
@model_scope = opts.fetch(:scope, default_scope)
|
19
|
+
@without_processed_scope = default_scope
|
20
|
+
@process_timestamp_attribute = opts.fetch(:process_timestamp_attribute, Remont.config.process_timestamp_attribute)
|
14
21
|
@before_cb = nil
|
15
22
|
@after_cb = nil
|
16
23
|
@attributes = []
|
@@ -20,7 +27,17 @@ module Remont
|
|
20
27
|
|
21
28
|
# @return [Remont::Schema]
|
22
29
|
def without_processed
|
23
|
-
|
30
|
+
raise MISSING_PROCESSING_STATUS_ATTR if @process_timestamp_attribute.nil?
|
31
|
+
|
32
|
+
@without_processed_scope = proc { |scope| scope.where(process_timestamp_attribute => nil) }
|
33
|
+
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param [String, Symbol] attr_name
|
38
|
+
# @return [Remont::Schema]
|
39
|
+
def with_process_timestamp_attribute(attr_name)
|
40
|
+
@process_timestamp_attribute = attr_name
|
24
41
|
|
25
42
|
self
|
26
43
|
end
|
@@ -70,12 +87,11 @@ module Remont
|
|
70
87
|
attr_reader :model
|
71
88
|
attr_reader :without_processed_scope
|
72
89
|
attr_reader :model_scope
|
73
|
-
attr_reader :attributes
|
74
90
|
attr_reader :before_cb
|
75
91
|
attr_reader :after_cb
|
76
92
|
|
77
93
|
def processor
|
78
|
-
@processor ||= RecordProcessor.new(
|
94
|
+
@processor ||= RecordProcessor.new(self, before_cb, after_cb)
|
79
95
|
end
|
80
96
|
|
81
97
|
def records_for_processing
|
@@ -83,5 +99,9 @@ module Remont
|
|
83
99
|
without_processed_scope.call(model.all)
|
84
100
|
)
|
85
101
|
end
|
102
|
+
|
103
|
+
def default_scope
|
104
|
+
proc { |scope| scope }
|
105
|
+
end
|
86
106
|
end
|
87
107
|
end
|
@@ -1,5 +1,13 @@
|
|
1
1
|
desc 'Run schema processing'
|
2
|
-
task remont: :
|
3
|
-
|
2
|
+
task :remont, [:script_path] => :remont_env do |_, args|
|
3
|
+
script_path = args[:script_path]
|
4
|
+
abort 'Missing script path' unless script_path
|
5
|
+
|
6
|
+
path = Rails.root.join(script_path)
|
4
7
|
Remont::Script.new(path).run!
|
5
8
|
end
|
9
|
+
|
10
|
+
task :remont_env do # rubocop:disable Rails/RakeEnvironment
|
11
|
+
Rake::Task['environment'].invoke
|
12
|
+
rescue StandardError # rubocop:disable Lint/SuppressedException
|
13
|
+
end
|
data/lib/remont/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remont
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rails Team
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|