Method: DTWorker#do_batch_copy

Defined in:
lib/datatransit/rule_dsl.rb

#do_batch_copy(sourceCls, targetCls, task, pk = nil, in_batch = false) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/datatransit/rule_dsl.rb', line 163

def do_batch_copy (sourceCls, targetCls, task, pk = nil, in_batch = false)
  count = sourceCls.where(task.search_cond).size.to_f
  return if count <= 0
  
  how_many_batch = (count / @batch_size).ceil
  #the progress bar
  bar = ProgressBar.new(count)
  
  if in_batch 
    0.upto (how_many_batch-1) do |i|  
      sourceCls.where(task.search_cond).find_each(
        start: i * @batch_size, batch_size: @batch_size) do |source_row|
        
        #update progress
        bar.increment!
        
        if task.filter
          next if do_filter_out task.filter, source_row
        end
        target_row = targetCls.new source_row.attributes

        #activerecord would ignore pk field, and the above initialization will result nill primary key.
        #here the original pk is used in the target_row, it is what we need exactly.
        if pk 
          target_row.send( "#{pk}=", source_row.send("#{pk}") )
        end

        target_row.save
      end
    end
  else
    sourceCls.where(task.search_cond).each do |source_row|
      #update progress
      bar.increment!
      
      if task.filter
        next if do_filter_out task.filter, source_row
      end
      target_row = targetCls.new source_row.attributes

      #activerecord would ignore pk field, and the above initialization will result nill primary key.
      #here the original pk is used in the target_row, it is what we need exactly.
      if pk 
        target_row.send( "#{pk}=", source_row.send("#{pk}") )
      end

      target_row.save
    end
  end
  
  
end