Commit 36b0eace

mo k <mo@mokhan.ca>
2012-10-10 19:04:36
use delayed_job to run the migration of each creation one job at a time. Doing it all in one migration is timing out on staging.
1 parent 92c45d4
app/models/creation.rb
@@ -33,4 +33,14 @@ class Creation < ActiveRecord::Base
   def main_image_thumb_url
     primary_image.image.thumb.url
   end
+
+  def migrate_primary_image
+      puts "#{index}. processing #{name}"
+      photo = photos.build({:is_primary => true})
+      photo.created_at = created_at
+      photo.updated_at = updated_at
+      photo.image = image.file
+      photo.save!
+      puts "#{index}. migrated #{image.url} to #{photo.attributes}"
+  end
 end
db/migrate/20121007153155_add_is_primary_to_photos.rb
@@ -0,0 +1,11 @@
+class AddIsPrimaryToPhotos < ActiveRecord::Migration
+  def up
+    add_column :photos, :is_primary, :boolean, :default => false
+    add_index  :photos, :is_primary
+  end
+
+  def down
+    remove_index  :photos, :is_primary
+    remove_column :photos, :is_primary
+  end
+end
db/migrate/20121007153155_migrate_images_to_photos.rb
@@ -1,26 +0,0 @@
-class MigrateImagesToPhotos < ActiveRecord::Migration
-  def up
-    add_column :photos, :is_primary, :boolean, :default => false
-
-    Photo.reset_column_information
-    Creation.all.each_with_index do |creation, index|
-      puts "#{index}. processing #{creation.name}"
-      photo = creation.photos.build({:is_primary => true})
-      photo.created_at = creation.created_at
-      photo.updated_at = creation.updated_at
-      photo.image = creation.image.file
-      photo.save!
-      puts "#{index}. migrated #{creation.image.url} to #{photo.attributes}"
-    end
-    #add_index  :photos, :is_primary
-    #remove_column :creations, :image
-  end
-
-  def down
-    Photo.where(:is_primary => true).each do |photo|
-      photo.destroy
-    end
-    #remove_index  :photos, :is_primary
-    remove_column :photos, :is_primary
-  end
-end
db/migrate/20121010190225_migrate_images_for_each_creation.rb
@@ -0,0 +1,14 @@
+class MigrateImagesForEachCreation < ActiveRecord::Migration
+  def up
+    Creation.all.each_with_index do |creation, index|
+      creation.delay.migrate_primary_image
+    end
+    #remove_column :creations, :image
+  end
+
+  def down
+    Photo.where(:is_primary => true).each do |photo|
+      photo.destroy
+    end
+  end
+end