Make migrations compatible with postgres

This commit is contained in:
Raphael Sofaer 2011-03-02 11:05:16 -08:00
parent ba6afa50e8
commit 9d92161430
6 changed files with 94 additions and 85 deletions

View file

@ -5,9 +5,9 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
["id", "created_at", "updated_at"].each{|n| columns.delete(n)} ["id", "created_at", "updated_at"].each{|n| columns.delete(n)}
sql = <<-SQL sql = <<-SQL
SELECT `profiles`.person_id FROM `profiles` SELECT profiles.person_id FROM profiles
GROUP BY #{columns.join(',')} GROUP BY #{columns.join(',')}
HAVING COUNT(*)>1 AND `profiles`.person_id IS NOT NULL; HAVING COUNT(*)>1 AND profiles.person_id IS NOT NULL;
SQL SQL
result = conn.execute(sql) result = conn.execute(sql)
duplicate_person_ids = result.to_a.flatten duplicate_person_ids = result.to_a.flatten
@ -15,13 +15,13 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
undesired_profile_ids = [] undesired_profile_ids = []
duplicate_person_ids.each do |person_id| duplicate_person_ids.each do |person_id|
profile_ids = conn.execute(" profile_ids = conn.execute("
SELECT `profiles`.id FROM `profiles` SELECT profiles.id FROM profiles
WHERE `profiles`.person_id = #{person_id};").to_a.flatten WHERE profiles.person_id = #{person_id};").to_a.flatten
profile_ids.pop profile_ids.pop
undesired_profile_ids.concat(profile_ids) undesired_profile_ids.concat(profile_ids)
end end
conn.execute("DELETE FROM `profiles` conn.execute("DELETE FROM profiles
WHERE `profiles`.id IN (#{undesired_profile_ids.join(",")});") unless undesired_profile_ids.empty? WHERE profiles.id IN (#{undesired_profile_ids.join(",")});") unless undesired_profile_ids.empty?
remove_index :profiles, :person_id remove_index :profiles, :person_id
add_index :profiles, :person_id, :unique => true add_index :profiles, :person_id, :unique => true

View file

@ -3,7 +3,7 @@ class AddInvitationServiceAndInvitationIdentifierToUser < ActiveRecord::Migratio
add_column(:users, :invitation_service, :string) add_column(:users, :invitation_service, :string)
add_column(:users, :invitation_identifier, :string) add_column(:users, :invitation_identifier, :string)
execute("UPDATE `users` SET invitation_service='email', invitation_identifier= email WHERE invitation_token IS NOT NULL;") execute("UPDATE users SET invitation_service='email', invitation_identifier= email WHERE invitation_token IS NOT NULL;")
end end
def self.down def self.down

View file

@ -4,7 +4,7 @@ class AddContactsVisible < ActiveRecord::Migration
add_index :aspects, [:user_id, :contacts_visible] add_index :aspects, [:user_id, :contacts_visible]
ActiveRecord::Base.connection.execute <<-SQL ActiveRecord::Base.connection.execute <<-SQL
UPDATE `aspects` UPDATE aspects
SET contacts_visible = false SET contacts_visible = false
WHERE contacts_visible IS NULL WHERE contacts_visible IS NULL
SQL SQL

View file

@ -10,6 +10,8 @@ class NotificationMultiplePeople < ActiveRecord::Migration
add_index :notification_actors, [:notification_id, :person_id] , :unique => true add_index :notification_actors, [:notification_id, :person_id] , :unique => true
add_index :notification_actors, :person_id ## if i am not mistaken we don't need this one because we won't query person.notifications add_index :notification_actors, :person_id ## if i am not mistaken we don't need this one because we won't query person.notifications
note_ids = execute('select id from notifications').to_a
unless note_ids.empty?
#make the notification actors table #make the notification actors table
execute "INSERT INTO notification_actors (notification_id, person_id) " + execute "INSERT INTO notification_actors (notification_id, person_id) " +
" SELECT id , actor_id " + " SELECT id , actor_id " +
@ -55,6 +57,7 @@ class NotificationMultiplePeople < ActiveRecord::Migration
"notifications.target_id = keep_delete.target_id AND "+ "notifications.target_id = keep_delete.target_id AND "+
"notifications.recipient_id = keep_delete.recipient_id AND "+ "notifications.recipient_id = keep_delete.recipient_id AND "+
"notifications.action = keep_delete.action" "notifications.action = keep_delete.action"
end
remove_column :notifications, :actor_id remove_column :notifications, :actor_id

View file

@ -1,16 +1,18 @@
class FixTargetOnNotification < ActiveRecord::Migration class FixTargetOnNotification < ActiveRecord::Migration
def self.up def self.up
note_ids = execute('select id from notifications').to_a
unless note_ids.empty?
execute("UPDATE notifications " + execute("UPDATE notifications " +
"SET target_type='Post' " + "SET target_type='Post' " +
"WHERE action = 'comment_on_post' OR action = 'also_commented'") "WHERE action = 'comment_on_post' OR action = 'also_commented'")
execute("UPDATE notifications " + execute("UPDATE notifications " +
"SET target_type='Request' " + "SET target_type='Request' " +
"WHERE action = 'new_request' OR action = 'request_accepted'") "WHERE action = 'new_request' OR action = 'request_accepted'")
execute("UPDATE notifications " + execute("UPDATE notifications " +
"SET target_type='Mention' " + "SET target_type='Mention' " +
"WHERE action = 'mentioned'") "WHERE action = 'mentioned'")
execute("create temporary table t1 "+ execute("create temporary table t1 "+
"(select notifications.id as n_id " + "(select notifications.id as n_id " +
@ -20,6 +22,7 @@ class FixTargetOnNotification < ActiveRecord::Migration
execute("DELETE notifications.* FROM notifications, t1 WHERE notifications.id = t1.n_id") execute("DELETE notifications.* FROM notifications, t1 WHERE notifications.id = t1.n_id")
end end
end
def self.down def self.down
end end

View file

@ -1,5 +1,7 @@
class UniqueIndexPostVisibilities < ActiveRecord::Migration class UniqueIndexPostVisibilities < ActiveRecord::Migration
def self.up def self.up
visibility_ids = execute('select id from post_visibilities').to_a
unless visibility_ids.empty?
sql = <<-SQL sql = <<-SQL
SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities` SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities`
GROUP BY post_id, aspect_id GROUP BY post_id, aspect_id
@ -24,6 +26,7 @@ class UniqueIndexPostVisibilities < ActiveRecord::Migration
new_result = execute(sql) new_result = execute(sql)
raise "Not all violating visibilities deleted, try migrating again if this is the first occurence" unless new_result.to_a.empty? raise "Not all violating visibilities deleted, try migrating again if this is the first occurence" unless new_result.to_a.empty?
end
remove_index :post_visibilities, [:aspect_id, :post_id] remove_index :post_visibilities, [:aspect_id, :post_id]
add_index :post_visibilities, [:aspect_id, :post_id], :unique => true add_index :post_visibilities, [:aspect_id, :post_id], :unique => true