scripterator 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +8 -0
- data/lib/scripterator/runner.rb +17 -11
- data/lib/scripterator/version.rb +1 -1
- data/spec/runner_spec.rb +3 -0
- data/spec/support/widget.rb +1 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42473644a78bcc8e3175774fb6b6783a93ee1ac1
|
4
|
+
data.tar.gz: ca62a4ba9252989df7090cd4a31aa8692b23262d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76076d599c2f543d563a35c1770056a400fad021cfd8894f87588aec0bdefb268f8cf5d0609cba6dc4308ca6594a4e4aaf55314840ea4c603fad13682fd45ad1
|
7
|
+
data.tar.gz: 7d0c093432672e53539b50f6c7613a37fcb08a20f8a9df168d90fcfe095b74e964b68ff58a8b136d1a0ab66c3e6048d8192e0fc5128504f01e08edfd5819048f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -32,10 +32,18 @@ Scripterator.run "Convert users from legacy auth data" do
|
|
32
32
|
User.skip_some_callbacks_we_want_to_avoid_during_script_running
|
33
33
|
end
|
34
34
|
|
35
|
+
before_batch do |batch|
|
36
|
+
# do stuff
|
37
|
+
end
|
38
|
+
|
35
39
|
for_each_user do |user|
|
36
40
|
user.do_legacy_conversion
|
37
41
|
end
|
38
42
|
|
43
|
+
after_batch do |batch|
|
44
|
+
# do stuff
|
45
|
+
end
|
46
|
+
|
39
47
|
after do
|
40
48
|
# some code to run after everything's finished
|
41
49
|
end
|
data/lib/scripterator/runner.rb
CHANGED
@@ -23,6 +23,7 @@ module Scripterator
|
|
23
23
|
@id_list = options[:id_list] || []
|
24
24
|
@start_id = options[:start_id] || 1
|
25
25
|
@end_id = options[:end_id]
|
26
|
+
@batch_size = options[:batch_size] || 1000
|
26
27
|
@redis_expiration = options[:redis_expiration]
|
27
28
|
@output_stream = options[:output_stream] || $stdout
|
28
29
|
|
@@ -34,7 +35,7 @@ module Scripterator
|
|
34
35
|
output_stats
|
35
36
|
end
|
36
37
|
|
37
|
-
%w(model before per_record after).each do |callback|
|
38
|
+
%w(model before per_record after before_batch after_batch).each do |callback|
|
38
39
|
define_method callback do |&block|
|
39
40
|
instance_variable_set "@#{callback}", block
|
40
41
|
end
|
@@ -102,20 +103,25 @@ module Scripterator
|
|
102
103
|
end
|
103
104
|
|
104
105
|
def run_loop
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
106
|
+
@id_list = (@start_id..@end_id) if @end_id
|
107
|
+
relation = model_finder
|
108
|
+
opts = {
|
109
|
+
batch_size: @batch_size
|
110
|
+
}
|
111
|
+
|
112
|
+
if @id_list.empty?
|
113
|
+
opts[:start] = @start_id
|
110
114
|
else
|
111
|
-
model_finder.
|
115
|
+
relation = model_finder.where(id: @id_list)
|
112
116
|
end
|
113
117
|
|
114
|
-
|
115
|
-
|
118
|
+
relation.find_in_batches(opts) do |group|
|
119
|
+
self.instance_exec(group, &@before_batch) if @before_batch
|
120
|
+
group.each { |record| transform_one_record(record) }
|
121
|
+
self.instance_exec(group, &@after_batch) if @after_batch
|
122
|
+
end
|
116
123
|
|
117
|
-
|
118
|
-
model_finder.where(id: @id_list).find_each { |record| transform_one_record(record) }
|
124
|
+
expire_redis_sets
|
119
125
|
end
|
120
126
|
|
121
127
|
def transform_one_record(record)
|
data/lib/scripterator/version.rb
CHANGED
data/spec/runner_spec.rb
CHANGED
@@ -10,6 +10,7 @@ describe Scripterator::Runner do
|
|
10
10
|
Proc.new do
|
11
11
|
before { Widget.before_stuff }
|
12
12
|
for_each_widget { |widget| Widget.transform_a_widget(widget) }
|
13
|
+
after_batch { |batch| Widget.after_batch_stuff(batch) }
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -31,6 +32,7 @@ describe Scripterator::Runner do
|
|
31
32
|
model { Widget.where(name: 'bla') }
|
32
33
|
before { Widget.before_stuff }
|
33
34
|
for_each_widget { |widget| Widget.transform_a_widget(widget) }
|
35
|
+
after_batch { |batch| Widget.after_batch_stuff(batch) }
|
34
36
|
end
|
35
37
|
end
|
36
38
|
let!(:widget1) { Widget.create(name: 'foo') }
|
@@ -96,6 +98,7 @@ describe Scripterator::Runner do
|
|
96
98
|
it 'runs the given script blocks' do
|
97
99
|
Widget.should_receive :before_stuff
|
98
100
|
Widget.should_receive(:transform_a_widget).exactly(num_widgets).times
|
101
|
+
Widget.should_receive :after_batch_stuff
|
99
102
|
subject
|
100
103
|
end
|
101
104
|
|
data/spec/support/widget.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scripterator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ted Dumitrescu
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.2
|
127
|
+
rubygems_version: 2.5.2
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: DSL for running operations on each of a set of models
|
@@ -132,4 +132,3 @@ test_files:
|
|
132
132
|
- spec/runner_spec.rb
|
133
133
|
- spec/spec_helper.rb
|
134
134
|
- spec/support/widget.rb
|
135
|
-
has_rdoc:
|