Skip to content
Extraits de code Groupes Projets
Valider 9d921614 rédigé par Raphael Sofaer's avatar Raphael Sofaer
Parcourir les fichiers

Make migrations compatible with postgres

parent ba6afa50
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -5,9 +5,9 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
["id", "created_at", "updated_at"].each{|n| columns.delete(n)}
sql = <<-SQL
SELECT `profiles`.person_id FROM `profiles`
SELECT profiles.person_id FROM profiles
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
result = conn.execute(sql)
duplicate_person_ids = result.to_a.flatten
......@@ -15,13 +15,13 @@ class UniqueIndexOnProfile < ActiveRecord::Migration
undesired_profile_ids = []
duplicate_person_ids.each do |person_id|
profile_ids = conn.execute("
SELECT `profiles`.id FROM `profiles`
WHERE `profiles`.person_id = #{person_id};").to_a.flatten
SELECT profiles.id FROM profiles
WHERE profiles.person_id = #{person_id};").to_a.flatten
profile_ids.pop
undesired_profile_ids.concat(profile_ids)
end
conn.execute("DELETE FROM `profiles`
WHERE `profiles`.id IN (#{undesired_profile_ids.join(",")});") unless undesired_profile_ids.empty?
conn.execute("DELETE FROM profiles
WHERE profiles.id IN (#{undesired_profile_ids.join(",")});") unless undesired_profile_ids.empty?
remove_index :profiles, :person_id
add_index :profiles, :person_id, :unique => true
......
......@@ -3,7 +3,7 @@ class AddInvitationServiceAndInvitationIdentifierToUser < ActiveRecord::Migratio
add_column(:users, :invitation_service, :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
def self.down
......
......@@ -4,7 +4,7 @@ class AddContactsVisible < ActiveRecord::Migration
add_index :aspects, [:user_id, :contacts_visible]
ActiveRecord::Base.connection.execute <<-SQL
UPDATE `aspects`
UPDATE aspects
SET contacts_visible = false
WHERE contacts_visible IS NULL
SQL
......
......@@ -10,51 +10,54 @@ class NotificationMultiplePeople < ActiveRecord::Migration
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
#make the notification actors table
execute "INSERT INTO notification_actors (notification_id, person_id) " +
" SELECT id , actor_id " +
" FROM notifications"
note_ids = execute('select id from notifications').to_a
unless note_ids.empty?
#make the notification actors table
execute "INSERT INTO notification_actors (notification_id, person_id) " +
" SELECT id , actor_id " +
" FROM notifications"
#update the notifications to reference the post
execute "UPDATE notifications, comments " +
"SET notifications.target_id = comments.post_id, " +
"target_type = 'Post' " +
"WHERE (notifications.target_id = comments.id " +
"AND (notifications.action = 'comment_on_post' " +
"OR notifications.action = 'also_commented'))"
#update the notifications to reference the post
execute "UPDATE notifications, comments " +
"SET notifications.target_id = comments.post_id, " +
"target_type = 'Post' " +
"WHERE (notifications.target_id = comments.id " +
"AND (notifications.action = 'comment_on_post' " +
"OR notifications.action = 'also_commented'))"
#select all the notifications to keep
execute "CREATE TEMPORARY TABLE keep_table " +
"(SELECT id as keep_id, actor_id , target_type , target_id , recipient_id , action " +
"FROM notifications WHERE action = 'comment_on_post' OR action = 'also_commented' " +
"GROUP BY target_type , target_id , recipient_id , action) "
#select all the notifications to keep
execute "CREATE TEMPORARY TABLE keep_table " +
"(SELECT id as keep_id, actor_id , target_type , target_id , recipient_id , action " +
"FROM notifications WHERE action = 'comment_on_post' OR action = 'also_commented' " +
"GROUP BY target_type , target_id , recipient_id , action) "
#get a table of with ids of the notifications that need to be deleted and with the ones that need
#to replace them
execute "CREATE TEMPORARY TABLE keep_delete " +
"( SELECT n1.keep_id, n2.id as delete_id, " +
"n2.actor_id, n1.target_type, n1.target_id, n1.recipient_id, n1.action " +
"FROM keep_table n1, notifications n2 " +
"WHERE n1.keep_id != n2.id " +
"AND n1.actor_id != n2.actor_id "+
"AND n1.target_type = n2.target_type AND n1.target_id = n2.target_id " +
"AND n1.recipient_id = n2.recipient_id AND n1.action = n2.action " +
"AND (n1.action = 'comment_on_post' OR n1.action = 'also_commented') "+
"GROUP BY n2.actor_id , n2.target_type , n2.target_id , n2.recipient_id , n2.action)"
#get a table of with ids of the notifications that need to be deleted and with the ones that need
#to replace them
execute "CREATE TEMPORARY TABLE keep_delete " +
"( SELECT n1.keep_id, n2.id as delete_id, " +
"n2.actor_id, n1.target_type, n1.target_id, n1.recipient_id, n1.action " +
"FROM keep_table n1, notifications n2 " +
"WHERE n1.keep_id != n2.id " +
"AND n1.actor_id != n2.actor_id "+
"AND n1.target_type = n2.target_type AND n1.target_id = n2.target_id " +
"AND n1.recipient_id = n2.recipient_id AND n1.action = n2.action " +
"AND (n1.action = 'comment_on_post' OR n1.action = 'also_commented') "+
"GROUP BY n2.actor_id , n2.target_type , n2.target_id , n2.recipient_id , n2.action)"
#have the notifications actors reference the notifications that need to be kept
execute "UPDATE notification_actors, keep_delete "+
"SET notification_actors.notification_id = keep_delete.keep_id "+
"WHERE notification_actors.notification_id = keep_delete.delete_id"
#have the notifications actors reference the notifications that need to be kept
execute "UPDATE notification_actors, keep_delete "+
"SET notification_actors.notification_id = keep_delete.keep_id "+
"WHERE notification_actors.notification_id = keep_delete.delete_id"
#delete all the notifications that need to be deleted
execute "DELETE notifications.* " +
"FROM notifications, keep_delete " +
"WHERE notifications.id != keep_delete.keep_id AND "+
"notifications.target_type = keep_delete.target_type AND "+
"notifications.target_id = keep_delete.target_id AND "+
"notifications.recipient_id = keep_delete.recipient_id AND "+
"notifications.action = keep_delete.action"
#delete all the notifications that need to be deleted
execute "DELETE notifications.* " +
"FROM notifications, keep_delete " +
"WHERE notifications.id != keep_delete.keep_id AND "+
"notifications.target_type = keep_delete.target_type AND "+
"notifications.target_id = keep_delete.target_id AND "+
"notifications.recipient_id = keep_delete.recipient_id AND "+
"notifications.action = keep_delete.action"
end
remove_column :notifications, :actor_id
......
class FixTargetOnNotification < ActiveRecord::Migration
def self.up
execute("UPDATE notifications " +
"SET target_type='Post' " +
"WHERE action = 'comment_on_post' OR action = 'also_commented'")
note_ids = execute('select id from notifications').to_a
unless note_ids.empty?
execute("UPDATE notifications " +
"SET target_type='Post' " +
"WHERE action = 'comment_on_post' OR action = 'also_commented'")
execute("UPDATE notifications " +
"SET target_type='Request' " +
"WHERE action = 'new_request' OR action = 'request_accepted'")
execute("UPDATE notifications " +
"SET target_type='Request' " +
"WHERE action = 'new_request' OR action = 'request_accepted'")
execute("UPDATE notifications " +
"SET target_type='Mention' " +
"WHERE action = 'mentioned'")
execute("UPDATE notifications " +
"SET target_type='Mention' " +
"WHERE action = 'mentioned'")
execute("create temporary table t1 "+
"(select notifications.id as n_id " +
"from notifications LEFT JOIN mentions "+
"ON notifications.target_id = mentions.id "+
"WHERE notifications.action = 'mentioned' AND mentions.id IS NULL)")
execute("DELETE notifications.* FROM notifications, t1 WHERE notifications.id = t1.n_id")
execute("create temporary table t1 "+
"(select notifications.id as n_id " +
"from notifications LEFT JOIN mentions "+
"ON notifications.target_id = mentions.id "+
"WHERE notifications.action = 'mentioned' AND mentions.id IS NULL)")
execute("DELETE notifications.* FROM notifications, t1 WHERE notifications.id = t1.n_id")
end
end
def self.down
......
class UniqueIndexPostVisibilities < ActiveRecord::Migration
def self.up
sql = <<-SQL
SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities`
GROUP BY post_id, aspect_id
HAVING COUNT(*)>1;
SQL
visibility_ids = execute('select id from post_visibilities').to_a
unless visibility_ids.empty?
sql = <<-SQL
SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities`
GROUP BY post_id, aspect_id
HAVING COUNT(*)>1;
SQL
result = execute(sql)
dup_pvs = result.to_a
undesired_ids = []
result = execute(sql)
dup_pvs = result.to_a
undesired_ids = []
dup_pvs.each do |arr|
post_id, aspect_id = arr
pv_ids = execute("
SELECT `post_visibilities`.id FROM `post_visibilities`
WHERE `post_visibilities`.post_id = #{post_id}
AND `post_visibilities`.aspect_id = #{aspect_id};"
).to_a.flatten!
pv_ids.pop
undesired_ids.concat(pv_ids)
end
execute("DELETE FROM `post_visibilities` WHERE `post_visibilities`.id IN (#{undesired_ids.join(",")});") unless undesired_ids.empty?
dup_pvs.each do |arr|
post_id, aspect_id = arr
pv_ids = execute("
SELECT `post_visibilities`.id FROM `post_visibilities`
WHERE `post_visibilities`.post_id = #{post_id}
AND `post_visibilities`.aspect_id = #{aspect_id};"
).to_a.flatten!
pv_ids.pop
undesired_ids.concat(pv_ids)
end
execute("DELETE FROM `post_visibilities` WHERE `post_visibilities`.id IN (#{undesired_ids.join(",")});") unless undesired_ids.empty?
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?
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?
end
remove_index :post_visibilities, [:aspect_id, :post_id]
add_index :post_visibilities, [:aspect_id, :post_id], :unique => true
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter