Permalink
Browse files

Migraciones en batches (de 100 por default). - #8

  • Loading branch information...
1 parent e5a01d6 commit afcf72ec3057f9eb49dbeda75c359c3d05b09dd5 @eloyesp eloyesp committed Apr 26, 2012
Showing with 20 additions and 9 deletions.
  1. +19 −8 lib/legacy/migration.rb
  2. +1 −1 test/unit/legacy/migration_test.rb
View
@@ -47,18 +47,24 @@ def initialize(opts)
def run(opts = {})
count = opts[:count] || :all
+ batches = opts[:batches] || 100
if count.kind_of? Integer
+ records = []
count.times do |i|
record = @legacy_table.find(i)
- migrate_record(record) if record
+ records << prepare_record(record) if record
end
+ migrate_records records.compact
else
- @legacy_table.each do |record|
- if record
- migrate_record(record)
- else
- puts "V" if @output == :dots
+ @legacy_table.each_slice(batches) do |records|
+ records.map! do |record|
+ if record
+ prepare_record(record)
+ else
+ puts "V" if @output == :dots
+ end
end
+ migrate_records records.compact
end
end
end
@@ -73,11 +79,16 @@ def close
private
- def migrate_record(record)
+ def prepare_record(record)
attributes = downcase_and_symbolize_attributes record.attributes
attributes.merge! build_references(attributes) unless @references.empty?
- @model.seed(*@constraints, attributes)
print "·" if @output == :dots
+ attributes
+ end
+
+ def migrate_records(records)
+ @model.seed(*@constraints, records)
+ print "*" if @output == :dots
end
def build_references(seed_attr)
@@ -40,7 +40,7 @@ def test_mass_migration
def test_migration_with_dots_output
migration.output = :dots
- assert_output("··") do
+ assert_output("··*") do
migration.run :count => 2
end
end

0 comments on commit afcf72e

Please sign in to comment.