Make person_id on profile unique, delete duplicate profiles in migration

This commit is contained in:
Raphael 2011-01-25 12:10:24 -08:00
parent 5fe0613a97
commit def20a4d31
3 changed files with 31 additions and 17 deletions

View file

@ -0,0 +1,29 @@
class UniqueIndexOnProfile < ActiveRecord::Migration
def self.up
columns = Profile.column_names
["id", "created_at", "updated_at"].each{|n| columns.delete(n)}
sql = <<-SQL
SELECT `profiles`.person_id FROM `profiles`
GROUP BY #{columns.join(',')}
HAVING COUNT(*)>1 AND `profiles`.person_id IS NOT NULL;
SQL
result = Profile.connection.execute(sql)
duplicate_person_ids = result.to_a.flatten
undesired_profile_ids = []
duplicate_person_ids.each do |person_id|
ids = Profile.where(:person_id => person_id).map!{|p| p.id}
ids.pop
undesired_profile_id.concat(ids)
end
Profile.where(:id => undesired_profile_ids).delete_all
remove_index :profiles, :person_id
add_index :profiles, :person_id, :unique => true
end
def self.down
remove_index :profiles, :person_id
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110120182100) do
ActiveRecord::Schema.define(:version => 20110125190034) do
create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id"
@ -77,19 +77,6 @@ ActiveRecord::Schema.define(:version => 20110120182100) do
add_index "data_points", ["statistic_id"], :name => "index_data_points_on_statistic_id"
create_table "histories", :force => true do |t|
t.string "message"
t.string "username"
t.integer "item"
t.string "table"
t.integer "month", :limit => 2
t.integer "year", :limit => 8
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "histories", ["item", "table", "month", "year"], :name => "index_histories_on_item_and_table_and_month_and_year"
create_table "invitations", :force => true do |t|
t.text "message"
t.integer "sender_id"
@ -397,7 +384,7 @@ ActiveRecord::Schema.define(:version => 20110120182100) do
add_index "profiles", ["first_name", "searchable"], :name => "index_profiles_on_first_name_and_searchable"
add_index "profiles", ["last_name", "searchable"], :name => "index_profiles_on_last_name_and_searchable"
add_index "profiles", ["mongo_id"], :name => "index_profiles_on_mongo_id"
add_index "profiles", ["person_id"], :name => "index_profiles_on_person_id"
add_index "profiles", ["person_id"], :name => "index_profiles_on_person_id", :unique => true
create_table "requests", :force => true do |t|
t.integer "sender_id"

View file

@ -229,8 +229,6 @@ describe Person do
people = Person.search("ing", @user)
people.map{|p| p.name}.should == [@casey_grippi, @yevgeniy_dodis, @robert_grimm, @eugene_weinstein].map{|p|p.name}
end
it 'has no duplicate contacts'
end
context 'people finders for webfinger' do