From bf0eb3dac3c0d1aae94c025777ba10bcc3c5acb7 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 17 Aug 2011 14:18:51 -0700 Subject: [PATCH] 4 failures left in postgres --- app/controllers/comments_controller.rb | 2 +- app/models/job/receive_local_batch.rb | 17 ++++++++++++++--- app/models/person.rb | 16 +++++----------- app/models/services/facebook.rb | 11 ++++++++++- db/migrate/20110707234802_likes_on_comments.rb | 10 ++++++---- .../20110729045734_add_full_name_to_profile.rb | 9 ++++++++- spec/helpers/getting_started_helper_spec.rb | 4 ++-- spec/integration/receiving_spec.rb | 1 - 8 files changed, 46 insertions(+), 24 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index b111c5736..0272ccd73 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -57,7 +57,7 @@ class CommentsController < ApplicationController def index @post = current_user.find_visible_post_by_id(params[:post_id]) if @post - @comments = @post.comments.includes(:author => :profile) + @comments = @post.comments.includes(:author => :profile).order('created_at ASC') render :layout => false else raise ActiveRecord::RecordNotFound.new diff --git a/app/models/job/receive_local_batch.rb b/app/models/job/receive_local_batch.rb index ceca8f807..cc42d1cfa 100644 --- a/app/models/job/receive_local_batch.rb +++ b/app/models/job/receive_local_batch.rb @@ -14,12 +14,23 @@ module Job socket_to_users(post, recipient_user_ids) if post.respond_to?(:socket_to_user) notify_mentioned_users(post) end + def self.create_visibilities(post, recipient_user_ids) contacts = Contact.where(:user_id => recipient_user_ids, :person_id => post.author_id) - new_post_visibilities = contacts.map do |contact| - PostVisibility.new(:contact_id => contact.id, :post_id => post.id) + + if postgres? + # Take the naive approach to inserting our new visibilities for now. + contacts.each do |contact| + PostVisibility.find_or_create_by_contact_id_and_post_id(contact.id, post.id) + end + else + # Use a batch insert on mySQL. + new_post_visibilities = contacts.map do |contact| + PostVisibility.new(:contact_id => contact.id, :post_id => post.id) + end + PostVisibility.import(new_post_visibilities) end - PostVisibility.import new_post_visibilities + end def self.socket_to_users(post, recipient_user_ids) recipient_user_ids.each do |id| diff --git a/app/models/person.rb b/app/models/person.rb index 84f9b41e0..dbd17c6e1 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -67,18 +67,12 @@ class Person < ActiveRecord::Base def self.search_query_string(query) query = query.downcase + like_operator = postgres? ? "ILIKE" : "LIKE" - if postgres? - where_clause = <<-SQL - profiles.full_name ILIKE ? OR - profiles.diaspora_handle ILIKE ? - SQL - else - where_clause = <<-SQL - profiles.full_name LIKE ? OR - people.diaspora_handle LIKE ? - SQL - end + where_clause = <<-SQL + profiles.full_name #{like_operator} ? OR + people.diaspora_handle #{like_operator} ? + SQL q_tokens = query.to_s.strip.gsub(/(\s|$|^)/) { "%#{$1}" } [where_clause, [q_tokens, q_tokens]] diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 3a208039a..2252dd01f 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -42,7 +42,16 @@ class Services::Facebook < Service su.attach_local_models su } - ServiceUser.import(data, :on_duplicate_key_update => [:updated_at, :contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name, :username]) + + + if postgres? + # Take the naive approach to inserting our new visibilities for now. + data.each do |su| + su.save + end + else + ServiceUser.import(data, :on_duplicate_key_update => [:updated_at, :contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name, :username]) + end end private diff --git a/db/migrate/20110707234802_likes_on_comments.rb b/db/migrate/20110707234802_likes_on_comments.rb index 78a2fa272..67b039332 100644 --- a/db/migrate/20110707234802_likes_on_comments.rb +++ b/db/migrate/20110707234802_likes_on_comments.rb @@ -18,10 +18,12 @@ SQL SQL #There are some duplicate likes. - keeper_likes = Like.group(:target_id, :author_id, :target_type).having('COUNT(*) > 1') - keeper_likes.each do |like| - l = Like.arel_table - Like.where(:target_id => like.target_id, :author_id => like.author_id, :target_type => like.target_type).where(l[:id].not_eq(like.id)).delete_all + if Like.count > 0 + keeper_likes = Like.group(:target_id, :author_id, :target_type).having('COUNT(*) > 1') + keeper_likes.each do |like| + l = Like.arel_table + Like.where(:target_id => like.target_id, :author_id => like.author_id, :target_type => like.target_type).where(l[:id].not_eq(like.id)).delete_all + end end add_index :likes, [:target_id, :author_id, :target_type], :unique => true end diff --git a/db/migrate/20110729045734_add_full_name_to_profile.rb b/db/migrate/20110729045734_add_full_name_to_profile.rb index 047eed869..6096b3447 100644 --- a/db/migrate/20110729045734_add_full_name_to_profile.rb +++ b/db/migrate/20110729045734_add_full_name_to_profile.rb @@ -1,4 +1,5 @@ class AddFullNameToProfile < ActiveRecord::Migration + class Profile < ActiveRecord::Base; end def self.up add_column :profiles, :full_name, :string, :limit => 70 @@ -8,7 +9,13 @@ class AddFullNameToProfile < ActiveRecord::Migration remove_index :profiles, [:first_name, :searchable] remove_index :profiles, [:last_name, :searchable] - execute("UPDATE profiles SET full_name=LOWER(CONCAT(first_name, ' ', last_name))") + if Profile.count > 0 + if postgres? + execute("UPDATE profiles SET full_name=LOWER(first_name || ' ' || last_name)") + else + execute("UPDATE profiles SET full_name=LOWER(CONCAT(first_name, ' ', last_name))") + end + end end def self.down diff --git a/spec/helpers/getting_started_helper_spec.rb b/spec/helpers/getting_started_helper_spec.rb index 0223b9230..1176300b4 100644 --- a/spec/helpers/getting_started_helper_spec.rb +++ b/spec/helpers/getting_started_helper_spec.rb @@ -16,7 +16,7 @@ describe GettingStartedHelper do it 'returns true if the current user has filled out all 7 suggested fields (from getting started)' do profile = @current_user.person.profile profile.update_attributes!( - {:first_name => "alice", :last_name => "smith", :image_url => "abcd.jpg", :birthday => Date.new, + {:first_name => "alice", :last_name => "smith", :image_url => "abcd.jpg", :birthday => Time.now, :gender => "cow", :location => "san fran", :tag_string => "#sup", :bio => "holler" }) has_completed_profile?.should be_true end @@ -59,7 +59,7 @@ describe GettingStartedHelper do end it 'returns false if the current_user has less than 2 contacts (inclusive)' do - @current_user.contacts.delete_all + @current_user.contacts.destroy_all has_few_contacts?.should be_false end end diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index 4e6d9315d..a4ea66f3e 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -272,7 +272,6 @@ describe 'a user receives a post' do post_in_db.comments.should == [] receive_with_zord(bob, alice.person, @xml) - post_in_db.comments(true).first.author.should == remote_person end end