diff --git a/db/migrate/20110125190034_unique_index_on_profile.rb b/db/migrate/20110125190034_unique_index_on_profile.rb new file mode 100644 index 000000000..55461c258 --- /dev/null +++ b/db/migrate/20110125190034_unique_index_on_profile.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7f075f34f..1db5153d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index d8add61d4..08cfd787e 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -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