20161203164520_add_from_account_id_to_notifications.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. class AddFromAccountIdToNotifications < ActiveRecord::Migration[5.0]
  3. def up
  4. add_column :notifications, :from_account_id, :integer
  5. Notification.where(from_account_id: nil).where(activity_type: 'Status').update_all(<<~SQL.squish)
  6. from_account_id = (
  7. SELECT statuses.account_id
  8. FROM notifications AS notifications1
  9. INNER JOIN statuses ON notifications1.activity_id = statuses.id
  10. WHERE notifications1.activity_type = 'Status' AND notifications1.id = notifications.id
  11. )
  12. SQL
  13. Notification.where(from_account_id: nil).where(activity_type: 'Mention').update_all(<<~SQL.squish)
  14. from_account_id = (
  15. SELECT statuses.account_id
  16. FROM notifications AS notifications1
  17. INNER JOIN mentions ON notifications1.activity_id = mentions.id
  18. INNER JOIN statuses ON mentions.status_id = statuses.id
  19. WHERE notifications1.activity_type = 'Mention' AND notifications1.id = notifications.id
  20. )
  21. SQL
  22. Notification.where(from_account_id: nil).where(activity_type: 'Favourite').update_all(<<~SQL.squish)
  23. from_account_id = (
  24. SELECT favourites.account_id
  25. FROM notifications AS notifications1
  26. INNER JOIN favourites ON notifications1.activity_id = favourites.id
  27. WHERE notifications1.activity_type = 'Favourite' AND notifications1.id = notifications.id
  28. )
  29. SQL
  30. Notification.where(from_account_id: nil).where(activity_type: 'Follow').update_all(<<~SQL.squish)
  31. from_account_id = (
  32. SELECT follows.account_id
  33. FROM notifications AS notifications1
  34. INNER JOIN follows ON notifications1.activity_id = follows.id
  35. WHERE notifications1.activity_type = 'Follow' AND notifications1.id = notifications.id
  36. )
  37. SQL
  38. end
  39. def down
  40. remove_column :notifications, :from_account_id
  41. end
  42. end