20221101190723_backfill_admin_action_logs.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # frozen_string_literal: true
  2. class BackfillAdminActionLogs < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. class Account < ApplicationRecord
  5. # Dummy class, to make migration possible across version changes
  6. has_one :user, inverse_of: :account
  7. def local?
  8. domain.nil?
  9. end
  10. def acct
  11. local? ? username : "#{username}@#{domain}"
  12. end
  13. end
  14. class User < ApplicationRecord
  15. # Dummy class, to make migration possible across version changes
  16. belongs_to :account
  17. end
  18. class Status < ApplicationRecord
  19. include RoutingHelper
  20. # Dummy class, to make migration possible across version changes
  21. belongs_to :account
  22. def local?
  23. attributes['local'] || attributes['uri'].nil?
  24. end
  25. def uri
  26. local? ? activity_account_status_url(account, self) : attributes['uri']
  27. end
  28. end
  29. class DomainBlock < ApplicationRecord; end
  30. class DomainAllow < ApplicationRecord; end
  31. class EmailDomainBlock < ApplicationRecord; end
  32. class UnavailableDomain < ApplicationRecord; end
  33. class AccountWarning < ApplicationRecord
  34. # Dummy class, to make migration possible across version changes
  35. belongs_to :account
  36. end
  37. class Announcement < ApplicationRecord; end
  38. class IpBlock < ApplicationRecord; end
  39. class CustomEmoji < ApplicationRecord; end
  40. class CanonicalEmailBlock < ApplicationRecord; end
  41. class Appeal < ApplicationRecord
  42. # Dummy class, to make migration possible across version changes
  43. belongs_to :account
  44. end
  45. class AdminActionLog < ApplicationRecord
  46. # Dummy class, to make migration possible across version changes
  47. # Cannot use usual polymorphic support because of namespacing issues
  48. belongs_to :status, foreign_key: :target_id
  49. belongs_to :account, foreign_key: :target_id
  50. belongs_to :user
  51. belongs_to :domain_block, foreign_key: :target_id
  52. belongs_to :domain_allow, foreign_key: :target_id
  53. belongs_to :email_domain_block, foreign_key: :target_id
  54. belongs_to :unavailable_domain, foreign_key: :target_id
  55. belongs_to :account_warning, foreign_key: :target_id
  56. belongs_to :announcement, foreign_key: :target_id
  57. belongs_to :ip_block, foreign_key: :target_id
  58. belongs_to :custom_emoji, foreign_key: :target_id
  59. belongs_to :canonical_email_block, foreign_key: :target_id
  60. belongs_to :appeal, foreign_key: :target_id
  61. end
  62. def up
  63. safety_assured do
  64. AdminActionLog.includes(:account).where(target_type: 'Account', human_identifier: nil).find_each do |log|
  65. next if log.account.nil?
  66. log.update_attribute('human_identifier', log.account.acct)
  67. end
  68. AdminActionLog.includes(user: :account).where(target_type: 'User', human_identifier: nil).find_each do |log|
  69. next if log.user.nil?
  70. log.update_attribute('human_identifier', log.user.account.acct)
  71. log.update_attribute('route_param', log.user.account_id)
  72. end
  73. AdminActionLog.where(target_type: 'Report', human_identifier: nil).in_batches.update_all('human_identifier = target_id::text')
  74. AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log|
  75. next if log.domain_block.nil?
  76. log.update_attribute('human_identifier', log.domain_block.domain)
  77. end
  78. AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log|
  79. next if log.domain_allow.nil?
  80. log.update_attribute('human_identifier', log.domain_allow.domain)
  81. end
  82. AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log|
  83. next if log.email_domain_block.nil?
  84. log.update_attribute('human_identifier', log.email_domain_block.domain)
  85. end
  86. AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log|
  87. next if log.unavailable_domain.nil?
  88. log.update_attribute('human_identifier', log.unavailable_domain.domain)
  89. end
  90. AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log|
  91. next if log.status.nil?
  92. log.update_attribute('human_identifier', log.status.account.acct)
  93. log.update_attribute('permalink', log.status.uri)
  94. end
  95. AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log|
  96. next if log.account_warning.nil?
  97. log.update_attribute('human_identifier', log.account_warning.account.acct)
  98. end
  99. AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log|
  100. next if log.announcement.nil?
  101. log.update_attribute('human_identifier', log.announcement.text)
  102. end
  103. AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log|
  104. next if log.ip_block.nil?
  105. log.update_attribute('human_identifier', "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}")
  106. end
  107. AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log|
  108. next if log.custom_emoji.nil?
  109. log.update_attribute('human_identifier', log.custom_emoji.shortcode)
  110. end
  111. AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log|
  112. next if log.canonical_email_block.nil?
  113. log.update_attribute('human_identifier', log.canonical_email_block.canonical_email_hash)
  114. end
  115. AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal', human_identifier: nil).find_each do |log|
  116. next if log.appeal.nil?
  117. log.update_attribute('human_identifier', log.appeal.account.acct)
  118. log.update_attribute('route_param', log.appeal.account_warning_id)
  119. end
  120. end
  121. end
  122. def down; end
  123. end