~cytrogen/masto-fe

ref: a2dca50ef3464734aa89eb56a163ce06a1b63b66 masto-fe/db/migrate/20161203164520_add_from_account_id_to_notifications.rb -rw-r--r-- 1.8 KiB
a2dca50e — Matt Jankowski Use heredoc SQL blocks in `AddFromAccountIdToNotifications` migration (#26178) 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

class AddFromAccountIdToNotifications < ActiveRecord::Migration[5.0]
  def up
    add_column :notifications, :from_account_id, :integer

    Notification.where(from_account_id: nil).where(activity_type: 'Status').update_all(<<~SQL.squish)
      from_account_id = (
        SELECT statuses.account_id
        FROM notifications AS notifications1
        INNER JOIN statuses ON notifications1.activity_id = statuses.id
        WHERE notifications1.activity_type = 'Status' AND notifications1.id = notifications.id
      )
    SQL
    Notification.where(from_account_id: nil).where(activity_type: 'Mention').update_all(<<~SQL.squish)
      from_account_id = (
        SELECT statuses.account_id
        FROM notifications AS notifications1
        INNER JOIN mentions ON notifications1.activity_id = mentions.id
        INNER JOIN statuses ON mentions.status_id = statuses.id
        WHERE notifications1.activity_type = 'Mention' AND notifications1.id = notifications.id
      )
    SQL
    Notification.where(from_account_id: nil).where(activity_type: 'Favourite').update_all(<<~SQL.squish)
      from_account_id = (
        SELECT favourites.account_id
        FROM notifications AS notifications1
        INNER JOIN favourites ON notifications1.activity_id = favourites.id
        WHERE notifications1.activity_type = 'Favourite' AND notifications1.id = notifications.id
      )
    SQL
    Notification.where(from_account_id: nil).where(activity_type: 'Follow').update_all(<<~SQL.squish)
      from_account_id = (
        SELECT follows.account_id
        FROM notifications AS notifications1
        INNER JOIN follows ON notifications1.activity_id = follows.id
        WHERE notifications1.activity_type = 'Follow' AND notifications1.id = notifications.id
      )
    SQL
  end

  def down
    remove_column :notifications, :from_account_id
  end
end