about summary refs log tree commit diff
path: root/db/migrate/20181026034033_remove_faux_remote_account_duplicates.rb
blob: 40537e9c9e027bfaec38caa2551886666188b6d4 (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
class RemoveFauxRemoteAccountDuplicates < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  class StreamEntry < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account, inverse_of: :stream_entries
  end

  class Status < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account, inverse_of: :statuses
    has_many :favourites, inverse_of: :status, dependent: :destroy
    has_many :mentions, dependent: :destroy, inverse_of: :status
  end

  class Favourite < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account, inverse_of: :favourites
    belongs_to :status,  inverse_of: :favourites
  end

  class Mention < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account, inverse_of: :mentions
    belongs_to :status
  end

  class Notification < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    belongs_to :account, optional: true
    belongs_to :from_account, class_name: 'Account', optional: true
    belongs_to :activity, polymorphic: true, optional: true
  end

  class Account < ApplicationRecord
    # Dummy class, to make migration possible across version changes
    has_many :stream_entries, inverse_of: :account, dependent: :destroy
    has_many :statuses, inverse_of: :account, dependent: :destroy
    has_many :favourites, inverse_of: :account, dependent: :destroy
    has_many :mentions, inverse_of: :account, dependent: :destroy
    has_many :notifications, inverse_of: :account, dependent: :destroy
  end

  def up
    local_domain = Rails.configuration.x.local_domain

    # Just a safety measure to ensure that under no circumstance
    # we will query `domain IS NULL` because that would return
    # actually local accounts, the originals
    return if local_domain.nil?

    Account.where(domain: local_domain).in_batches.destroy_all
  end

  def down; end
end