Only run the data migration if there are profiles in the db

This commit is contained in:
Raphael Sofaer 2011-06-27 10:26:12 -07:00
parent c7b4e2b4f4
commit e22701a12d

View file

@ -1,5 +1,7 @@
class UniqueIndexOnProfile < ActiveRecord::Migration
class Profile < ActiveRecord::Base; end
def self.up
if Profile.count > 0
conn = ActiveRecord::Base.connection
columns = conn.columns("profiles").map{|c| c.name}
["id", "created_at", "updated_at"].each{|n| columns.delete(n)}
@ -22,6 +24,7 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
end
conn.execute("DELETE FROM profiles
WHERE profiles.id IN (#{undesired_profile_ids.join(",")});") unless undesired_profile_ids.empty?
end
remove_index :profiles, :person_id
add_index :profiles, :person_id, :unique => true
@ -29,5 +32,6 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
def self.down
remove_index :profiles, :person_id
add_index :profiles, :person_id
end
end