Make migrations compatible with postgres
This commit is contained in:
parent
ba6afa50e8
commit
9d92161430
6 changed files with 94 additions and 85 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -10,51 +10,54 @@ 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
|
||||||
|
|
||||||
#make the notification actors table
|
note_ids = execute('select id from notifications').to_a
|
||||||
execute "INSERT INTO notification_actors (notification_id, person_id) " +
|
unless note_ids.empty?
|
||||||
" SELECT id , actor_id " +
|
#make the notification actors table
|
||||||
" FROM notifications"
|
execute "INSERT INTO notification_actors (notification_id, person_id) " +
|
||||||
|
" SELECT id , actor_id " +
|
||||||
|
" FROM notifications"
|
||||||
|
|
||||||
#update the notifications to reference the post
|
#update the notifications to reference the post
|
||||||
execute "UPDATE notifications, comments " +
|
execute "UPDATE notifications, comments " +
|
||||||
"SET notifications.target_id = comments.post_id, " +
|
"SET notifications.target_id = comments.post_id, " +
|
||||||
"target_type = 'Post' " +
|
"target_type = 'Post' " +
|
||||||
"WHERE (notifications.target_id = comments.id " +
|
"WHERE (notifications.target_id = comments.id " +
|
||||||
"AND (notifications.action = 'comment_on_post' " +
|
"AND (notifications.action = 'comment_on_post' " +
|
||||||
"OR notifications.action = 'also_commented'))"
|
"OR notifications.action = 'also_commented'))"
|
||||||
|
|
||||||
#select all the notifications to keep
|
#select all the notifications to keep
|
||||||
execute "CREATE TEMPORARY TABLE keep_table " +
|
execute "CREATE TEMPORARY TABLE keep_table " +
|
||||||
"(SELECT id as keep_id, actor_id , target_type , target_id , recipient_id , action " +
|
"(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' " +
|
"FROM notifications WHERE action = 'comment_on_post' OR action = 'also_commented' " +
|
||||||
"GROUP BY target_type , target_id , recipient_id , action) "
|
"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
|
#get a table of with ids of the notifications that need to be deleted and with the ones that need
|
||||||
#to replace them
|
#to replace them
|
||||||
execute "CREATE TEMPORARY TABLE keep_delete " +
|
execute "CREATE TEMPORARY TABLE keep_delete " +
|
||||||
"( SELECT n1.keep_id, n2.id as delete_id, " +
|
"( SELECT n1.keep_id, n2.id as delete_id, " +
|
||||||
"n2.actor_id, n1.target_type, n1.target_id, n1.recipient_id, n1.action " +
|
"n2.actor_id, n1.target_type, n1.target_id, n1.recipient_id, n1.action " +
|
||||||
"FROM keep_table n1, notifications n2 " +
|
"FROM keep_table n1, notifications n2 " +
|
||||||
"WHERE n1.keep_id != n2.id " +
|
"WHERE n1.keep_id != n2.id " +
|
||||||
"AND n1.actor_id != n2.actor_id "+
|
"AND n1.actor_id != n2.actor_id "+
|
||||||
"AND n1.target_type = n2.target_type AND n1.target_id = n2.target_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.recipient_id = n2.recipient_id AND n1.action = n2.action " +
|
||||||
"AND (n1.action = 'comment_on_post' OR n1.action = 'also_commented') "+
|
"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)"
|
"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
|
#have the notifications actors reference the notifications that need to be kept
|
||||||
execute "UPDATE notification_actors, keep_delete "+
|
execute "UPDATE notification_actors, keep_delete "+
|
||||||
"SET notification_actors.notification_id = keep_delete.keep_id "+
|
"SET notification_actors.notification_id = keep_delete.keep_id "+
|
||||||
"WHERE notification_actors.notification_id = keep_delete.delete_id"
|
"WHERE notification_actors.notification_id = keep_delete.delete_id"
|
||||||
|
|
||||||
#delete all the notifications that need to be deleted
|
#delete all the notifications that need to be deleted
|
||||||
execute "DELETE notifications.* " +
|
execute "DELETE notifications.* " +
|
||||||
"FROM notifications, keep_delete " +
|
"FROM notifications, keep_delete " +
|
||||||
"WHERE notifications.id != keep_delete.keep_id AND "+
|
"WHERE notifications.id != keep_delete.keep_id AND "+
|
||||||
"notifications.target_type = keep_delete.target_type AND "+
|
"notifications.target_type = keep_delete.target_type AND "+
|
||||||
"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
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,27 @@
|
||||||
class FixTargetOnNotification < ActiveRecord::Migration
|
class FixTargetOnNotification < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
execute("UPDATE notifications " +
|
note_ids = execute('select id from notifications').to_a
|
||||||
"SET target_type='Post' " +
|
unless note_ids.empty?
|
||||||
"WHERE action = 'comment_on_post' OR action = 'also_commented'")
|
execute("UPDATE notifications " +
|
||||||
|
"SET target_type='Post' " +
|
||||||
|
"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 " +
|
||||||
"from notifications LEFT JOIN mentions "+
|
"from notifications LEFT JOIN mentions "+
|
||||||
"ON notifications.target_id = mentions.id "+
|
"ON notifications.target_id = mentions.id "+
|
||||||
"WHERE notifications.action = 'mentioned' AND mentions.id IS NULL)")
|
"WHERE notifications.action = 'mentioned' AND mentions.id IS NULL)")
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,32 @@
|
||||||
class UniqueIndexPostVisibilities < ActiveRecord::Migration
|
class UniqueIndexPostVisibilities < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
sql = <<-SQL
|
visibility_ids = execute('select id from post_visibilities').to_a
|
||||||
SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities`
|
unless visibility_ids.empty?
|
||||||
GROUP BY post_id, aspect_id
|
sql = <<-SQL
|
||||||
HAVING COUNT(*)>1;
|
SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities`
|
||||||
SQL
|
GROUP BY post_id, aspect_id
|
||||||
|
HAVING COUNT(*)>1;
|
||||||
|
SQL
|
||||||
|
|
||||||
result = execute(sql)
|
result = execute(sql)
|
||||||
dup_pvs = result.to_a
|
dup_pvs = result.to_a
|
||||||
undesired_ids = []
|
undesired_ids = []
|
||||||
|
|
||||||
dup_pvs.each do |arr|
|
dup_pvs.each do |arr|
|
||||||
post_id, aspect_id = arr
|
post_id, aspect_id = arr
|
||||||
pv_ids = execute("
|
pv_ids = execute("
|
||||||
SELECT `post_visibilities`.id FROM `post_visibilities`
|
SELECT `post_visibilities`.id FROM `post_visibilities`
|
||||||
WHERE `post_visibilities`.post_id = #{post_id}
|
WHERE `post_visibilities`.post_id = #{post_id}
|
||||||
AND `post_visibilities`.aspect_id = #{aspect_id};"
|
AND `post_visibilities`.aspect_id = #{aspect_id};"
|
||||||
).to_a.flatten!
|
).to_a.flatten!
|
||||||
pv_ids.pop
|
pv_ids.pop
|
||||||
undesired_ids.concat(pv_ids)
|
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?
|
||||||
end
|
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?
|
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue