Make things more postgres compatible

This commit is contained in:
Raphael Sofaer 2011-04-05 09:49:30 -07:00
parent 3a1094dee8
commit fdb6675f28
14 changed files with 44 additions and 37 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -27,7 +27,11 @@ class CreateConversationsAndMessagesAndVisibilities < ActiveRecord::Migration
add_index :conversation_visibilities, :person_id
add_index :conversation_visibilities, :conversation_id
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

View file

@ -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

View file

@ -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

View file

@ -68,14 +68,14 @@ SQL
add_foreign_key :aspect_visibilities, :aspects, :dependent => :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

View file

@ -23,9 +23,11 @@ SQL
SQL
end
def self.up
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

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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