From fdb6675f2822015973a5c0b4d962d5161b79faac Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Tue, 5 Apr 2011 09:49:30 -0700 Subject: [PATCH] Make things more postgres compatible --- app/controllers/tags_controller.rb | 2 +- app/models/contact.rb | 6 +++--- app/models/person.rb | 6 +++--- app/models/post.rb | 4 ++-- ...te_conversations_and_messages_and_visibilities.rb | 6 +++++- db/migrate/20110311220249_downcase_tags.rb | 2 +- db/migrate/20110313015438_rename_text_fields.rb | 2 +- .../20110328202414_post_visibilities_on_contacts.rb | 10 +++++----- db/migrate/20110330230206_pm_foreign_keys.rb | 8 +++++--- db/schema.rb | 3 ++- lib/diaspora/user/querying.rb | 8 ++++---- spec/controllers/apis_controller_spec.rb | 10 +++++----- spec/integration/receiving_spec.rb | 12 ++++++------ spec/models/relayable_retraction_spec.rb | 2 +- 14 files changed, 44 insertions(+), 37 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 22dbbe286..fb9c55b6a 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -42,7 +42,7 @@ class TagsController < ApplicationController if current_user @posts = StatusMessage.joins(:aspects).where(:pending => false ).where(Aspect.arel_table[:user_id].eq(current_user.id).or(StatusMessage.arel_table[:public].eq(true)) - ).select('DISTINCT `posts`.*') + ).select('DISTINCT posts.*') else @posts = StatusMessage.where(:public => true, :pending => false) end diff --git a/app/models/contact.rb b/app/models/contact.rb index bfbdccc7b..943867e66 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -16,7 +16,7 @@ class Contact < ActiveRecord::Base has_many :post_visibilities has_many :posts, :through => :post_visibilities - + validate :not_contact_for_self validates_uniqueness_of :person_id, :scope => :user_id @@ -42,10 +42,10 @@ class Contact < ActiveRecord::Base incoming_aspects = Aspect.joins(:contacts).where( :user_id => self.person.owner_id, :contacts_visible => true, - :contacts => {:person_id => self.user.person.id}).select('`aspects`.id') + :contacts => {:person_id => self.user.person.id}).select('aspects.id') incoming_aspect_ids = incoming_aspects.map{|a| a.id} similar_contacts = Person.joins(:contacts => :aspect_memberships).where( - :aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT `people`.*') + :aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT people.*') end private def not_contact_for_self diff --git a/app/models/person.rb b/app/models/person.rb index 6c4f6eb1a..a20562d69 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -71,8 +71,8 @@ class Person < ActiveRecord::Base sql, tokens = self.search_query_string(query) Person.searchable.where(sql, *tokens).joins( - "LEFT OUTER JOIN `contacts` ON `contacts`.user_id = #{user.id} AND `contacts`.person_id = `people`.id" - ).joins("LEFT OUTER JOIN `requests` ON `requests`.recipient_id = #{user.person.id} AND `requests`.sender_id = `people`.id" + "LEFT OUTER JOIN contacts ON contacts.user_id = #{user.id} AND contacts.person_id = people.id" + ).joins("LEFT OUTER JOIN requests ON requests.recipient_id = #{user.person.id} AND requests.sender_id = people.id" ).includes(:profile ).order("contacts.user_id DESC", "requests.recipient_id DESC", "profiles.last_name ASC", "profiles.first_name ASC") end @@ -86,7 +86,7 @@ class Person < ActiveRecord::Base def name(opts = {}) @name ||= Person.name_from_attrs(self.profile.first_name, self.profile.last_name, self.diaspora_handle) - + end def self.name_from_attrs(first_name, last_name, diaspora_handle) diff --git a/app/models/post.rb b/app/models/post.rb index 359e04aff..333dc1313 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -14,8 +14,8 @@ class Post < ActiveRecord::Base xml_attr :created_at has_many :comments, :order => 'created_at ASC' - has_many :likes, :conditions => '`likes`.`positive` = 1', :dependent => :delete_all - has_many :dislikes, :conditions => '`likes`.`positive` = 0', :class_name => 'Like', :dependent => :delete_all + has_many :likes, :conditions => {:positive => true}, :dependent => :delete_all + has_many :dislikes, :conditions => {:positive => false}, :class_name => 'Like', :dependent => :delete_all has_many :aspect_visibilities has_many :aspects, :through => :aspect_visibilities diff --git a/db/migrate/20110225190919_create_conversations_and_messages_and_visibilities.rb b/db/migrate/20110225190919_create_conversations_and_messages_and_visibilities.rb index 7c3f880d7..1a56ce8b8 100644 --- a/db/migrate/20110225190919_create_conversations_and_messages_and_visibilities.rb +++ b/db/migrate/20110225190919_create_conversations_and_messages_and_visibilities.rb @@ -27,7 +27,11 @@ class CreateConversationsAndMessagesAndVisibilities < ActiveRecord::Migration add_index :conversation_visibilities, :person_id add_index :conversation_visibilities, :conversation_id - add_index :conversation_visibilities, [:conversation_id, :person_id], :unique => true + if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + add_index :conversation_visibilities, [:conversation_id, :person_id], :unique => true, :name => 'index_conversation_visibilities_on_everything' + else + add_index :conversation_visibilities, [:conversation_id, :person_id], :unique => true + end add_index :messages, :author_id end diff --git a/db/migrate/20110311220249_downcase_tags.rb b/db/migrate/20110311220249_downcase_tags.rb index ba3bef737..0a85f528d 100644 --- a/db/migrate/20110311220249_downcase_tags.rb +++ b/db/migrate/20110311220249_downcase_tags.rb @@ -11,7 +11,7 @@ class DowncaseTags < ActiveRecord::Migration end def self.up execute('UPDATE tags - SET tags.name = LOWER(tags.name);') + SET name = LOWER(tags.name);') names_with_duplicates = execute('SELECT name FROM tags GROUP BY name diff --git a/db/migrate/20110313015438_rename_text_fields.rb b/db/migrate/20110313015438_rename_text_fields.rb index d667ca5ac..07019a5da 100644 --- a/db/migrate/20110313015438_rename_text_fields.rb +++ b/db/migrate/20110313015438_rename_text_fields.rb @@ -2,7 +2,7 @@ class RenameTextFields < ActiveRecord::Migration def self.up rename_column :posts, :message, :text execute("UPDATE posts - SET posts.text = posts.caption + SET text = posts.caption WHERE posts.caption IS NOT NULL;") remove_column :posts, :caption end diff --git a/db/migrate/20110328202414_post_visibilities_on_contacts.rb b/db/migrate/20110328202414_post_visibilities_on_contacts.rb index 063741b04..85cffd7aa 100644 --- a/db/migrate/20110328202414_post_visibilities_on_contacts.rb +++ b/db/migrate/20110328202414_post_visibilities_on_contacts.rb @@ -1,7 +1,7 @@ class PostVisibilitiesOnContacts < ActiveRecord::Migration def self.move_author_pvs_to_aspect_pvs where_clause = < :delete add_foreign_key :aspect_visibilities, :posts, :dependent => :delete - delete_disconnected_pvs + delete_disconnected_pvs if PostVisibility.count > 0 add_column :post_visibilities, :contact_id, :integer, :null => false - move_author_pvs_to_aspect_pvs - set_pv_contact_ids + move_author_pvs_to_aspect_pvs if PostVisibility.count > 0 + set_pv_contact_ids if PostVisibility.count > 0 - delete_duplicate_pvs + delete_duplicate_pvs if PostVisibility.count > 0 remove_index :post_visibilities, [:aspect_id, :post_id] remove_column :post_visibilities, :aspect_id diff --git a/db/migrate/20110330230206_pm_foreign_keys.rb b/db/migrate/20110330230206_pm_foreign_keys.rb index 98fcd3a1d..652133004 100644 --- a/db/migrate/20110330230206_pm_foreign_keys.rb +++ b/db/migrate/20110330230206_pm_foreign_keys.rb @@ -23,9 +23,11 @@ SQL SQL end def self.up - delete_disconnected_conversations - delete_disconnected_messages - delete_disconnected_cvs + if Message.count > 0 + delete_disconnected_conversations + delete_disconnected_messages + delete_disconnected_cvs + end add_foreign_key :conversation_visibilities, :conversations, :dependent => :delete add_foreign_key :conversation_visibilities, :people, :dependent => :delete diff --git a/db/schema.rb b/db/schema.rb index 8d57025da..765daed3e 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 => 20110331222629) do +ActiveRecord::Schema.define(:version => 20110331004720) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -250,6 +250,7 @@ ActiveRecord::Schema.define(:version => 20110331222629) do add_index "posts", ["status_message_id", "pending"], :name => "index_posts_on_status_message_id_and_pending" add_index "posts", ["status_message_id"], :name => "index_posts_on_status_message_id" add_index "posts", ["type", "pending", "id"], :name => "index_posts_on_type_and_pending_and_id" + add_index "posts", ["type"], :name => "index_posts_on_type" create_table "profiles", :force => true do |t| t.string "diaspora_handle" diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 62628e9c1..250331b82 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -18,7 +18,7 @@ module Diaspora opts[:limit] ||= 20 opts[:order] ||= 'updated_at DESC' opts[:hidden] ||= false - order_with_table = '`posts`.' + opts[:order] + order_with_table = 'posts.' + opts[:order] opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page] posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id}) @@ -33,7 +33,7 @@ module Diaspora post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first} post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first} - Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit]).order(order_with_table) + Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table) end def visible_photos @@ -60,7 +60,7 @@ module Diaspora if opts[:type] == 'remote' people = people.where(:owner_id => nil) elsif opts[:type] == 'local' - people = people.where('`people`.`owner_id` IS NOT NULL') + people = people.where('people.owner_id IS NOT NULL') end people end @@ -94,7 +94,7 @@ module Diaspora end post_ids += Post.connection.execute(person.posts.where(:public => true).select('posts.id').to_sql).map{|r| r.first} - Post.where(:id => post_ids, :pending => false).select('DISTINCT `posts`.*').order("posts.created_at DESC") + Post.where(:id => post_ids, :pending => false).select('DISTINCT posts.*').order("posts.created_at DESC") end end end diff --git a/spec/controllers/apis_controller_spec.rb b/spec/controllers/apis_controller_spec.rb index 3b101b34e..14844608e 100644 --- a/spec/controllers/apis_controller_spec.rb +++ b/spec/controllers/apis_controller_spec.rb @@ -5,7 +5,7 @@ describe ApisController do @status_message1 = Factory(:status_message, :text => '#bobby #flay #sux', :public => true, :updated_at => Time.now + 20) @status_message2 = Factory(:status_message, :text => '#aobby', :public => true, :created_at => Time.now + 10) - @status_message3 = Factory(:status_message, :created_at => Time.now + 15) + @status_message3 = Factory(:status_message, :created_at => Time.now + 15) @person = Factory(:person, :profile => Factory.build(:profile,:first_name => 'bobby', :searchable => true, :tag_string => '#zord')) @person.profile.save end @@ -43,7 +43,7 @@ describe ApisController do context 'protected timelines' do let(:authenticate){ - sign_in(:user, @user); + sign_in(:user, @user); @controller.stub(:current_user).and_return(@user) } @@ -51,7 +51,7 @@ describe ApisController do @message1 = alice.post(:status_message, :text=> "hello", :to => alice.aspects.first) @message2 = eve.post(:status_message, :text=> "hello", :to => eve.aspects.first) end - + describe '#home_timeline' do it 'authenticates' do get :home_timeline, :format => :json @@ -158,7 +158,7 @@ describe ApisController do end end end - + describe '#users_profile_image' do it 'redirects on success' do get :users_profile_image, :screen_name => bob.diaspora_handle, :format => :json @@ -179,7 +179,7 @@ describe ApisController do end it 'returns a 404 if does not exsist' do - get :statuses, :guid => 999 + get :statuses, :guid => '999' response.code.should == '404' end end diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index 849512032..90cb10d42 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -142,11 +142,11 @@ describe 'a user receives a post' do raph.profile.diaspora_handle = "raph@remote.net" raph.profile.save! p = raph.profile - + p.tag_string = "#big #rafi #style" p.receive(luke, raph) p.tags(true).count.should == 3 - end + end end describe 'post refs' do @@ -305,11 +305,11 @@ describe 'a user receives a post' do describe 'receiving mulitple versions of the same post from a remote pod' do before do @local_luke, @local_leia, @remote_raphael = set_up_friends - @post = Factory.build(:status_message, :text => 'hey', :guid => 12313123, :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago) + @post = Factory.build(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago) end it 'does not update created_at or updated_at when two people save the same post' do - @post = Factory.build(:status_message, :text => 'hey', :guid => 12313123, :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago) + @post = Factory.build(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago) xml = @post.to_diaspora_xml receive_with_zord(@local_luke, @remote_raphael, xml) sleep(2) @@ -320,11 +320,11 @@ describe 'a user receives a post' do end it 'does not update the post if a new one is sent with a new created_at' do - @post = Factory.build(:status_message, :text => 'hey', :guid => 12313123, :author => @remote_raphael, :created_at => 5.days.ago) + @post = Factory.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 5.days.ago) old_time = @post.created_at xml = @post.to_diaspora_xml receive_with_zord(@local_luke, @remote_raphael, xml) - @post = Factory.build(:status_message, :text => 'hey', :guid => 12313123, :author => @remote_raphael, :created_at => 2.days.ago) + @post = Factory.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 2.days.ago) receive_with_zord(@local_luke, @remote_raphael, xml) (Post.find_by_guid @post.guid).created_at.day.should == old_time.day end diff --git a/spec/models/relayable_retraction_spec.rb b/spec/models/relayable_retraction_spec.rb index 9056a35b0..45f25c201 100644 --- a/spec/models/relayable_retraction_spec.rb +++ b/spec/models/relayable_retraction_spec.rb @@ -30,7 +30,7 @@ describe RelayableRetraction do @retraction= @local_luke.retract(@comment) @retraction.instance_variable_set(:@target, nil) - @retraction.target_guid = 135245 + @retraction.target_guid = '135245' @retraction.should_not_receive(:perform) @retraction.receive(@local_luke, @remote_raphael) end