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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2c3c2f5e186a3e9f766ac33a72903f835ae2f5e
4
- data.tar.gz: 196848f11699fc575a26708163e6271babb9a8bd
3
+ metadata.gz: 42473644a78bcc8e3175774fb6b6783a93ee1ac1
4
+ data.tar.gz: ca62a4ba9252989df7090cd4a31aa8692b23262d
5
5
  SHA512:
6
- metadata.gz: 8592a34342273e61e5f23999a5233f4e3ee3d22bc78bd60e3cb640a8ddecf048238b63f823e3ebb874454ad44c00266039e51a14f8c9ee2de6d1b9aab39453f6
7
- data.tar.gz: c31e9a4c791c1a18ccdb5245fed0b9d4ffa099c5731614e006b5a1c903d3b2acffa8371c16ef9af64b81e2e92715d85e8c9b4d8cb1c6ada7db0d7c92c6c6e2a4
6
+ metadata.gz: 76076d599c2f543d563a35c1770056a400fad021cfd8894f87588aec0bdefb268f8cf5d0609cba6dc4308ca6594a4e4aaf55314840ea4c603fad13682fd45ad1
7
+ data.tar.gz: 7d0c093432672e53539b50f6c7613a37fcb08a20f8a9df168d90fcfe095b74e964b68ff58a8b136d1a0ab66c3e6048d8192e0fc5128504f01e08edfd5819048f
data/CHANGELOG.md CHANGED
@@ -1,2 +1,6 @@
1
+ # 1.3.0
2
+ - Add support for changing batch size
3
+ - Add support for `after_batch` and `before_batch` callbacks
4
+
1
5
  # 1.2.0
2
- Add support for ActiveRecord versions 3.0 through 4.2
6
+ - Add support for ActiveRecord versions 3.0 through 4.2
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
@@ -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
- if @id_list.count > 0
106
- run_for_id_list
107
- elsif @end_id
108
- @id_list = (@start_id..@end_id)
109
- run_for_id_list
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.find_each(start: @start_id) { |record| transform_one_record(record) }
115
+ relation = model_finder.where(id: @id_list)
112
116
  end
113
117
 
114
- expire_redis_sets
115
- end
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
- def run_for_id_list
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)
@@ -1,3 +1,3 @@
1
1
  module Scripterator
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
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
 
@@ -10,5 +10,6 @@ end
10
10
  class Widget < ActiveRecord::Base
11
11
  # some dummy methods for setting message expectations in specs
12
12
  def self.before_stuff; end
13
+ def self.after_batch_stuff(batch); end
13
14
  def self.transform_a_widget(widget); true; end
14
15
  end
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.2.0
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: 2016-02-25 00:00:00.000000000 Z
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.3
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: