20221206114142_backfill_admin_action_logs_again.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # frozen_string_literal: true
  2. class BackfillAdminActionLogsAgain < 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. process_logs_for_account
  65. process_logs_for_user
  66. process_logs_for_report
  67. process_logs_for_domain_block
  68. process_logs_for_domain_allow
  69. process_logs_for_email_domain_block
  70. process_logs_for_unavailable_domain
  71. process_logs_for_status
  72. process_logs_for_account_warning
  73. process_logs_for_announcement
  74. process_logs_for_ip_block
  75. process_logs_for_custom_emoji
  76. process_logs_for_canonical_email_block
  77. process_logs_for_appeal
  78. end
  79. end
  80. def down; end
  81. private
  82. def process_logs_for_account
  83. AdminActionLog.includes(:account).where(target_type: 'Account', human_identifier: nil).find_each do |log|
  84. next if log.account.nil?
  85. log.update_attribute('human_identifier', log.account.acct)
  86. end
  87. end
  88. def process_logs_for_user
  89. AdminActionLog.includes(user: :account).where(target_type: 'User', human_identifier: nil).find_each do |log|
  90. next if log.user.nil?
  91. log.update_attribute('human_identifier', log.user.account.acct)
  92. log.update_attribute('route_param', log.user.account_id)
  93. end
  94. end
  95. def process_logs_for_report
  96. AdminActionLog.where(target_type: 'Report', human_identifier: nil).in_batches.update_all('human_identifier = target_id::text')
  97. end
  98. def process_logs_for_domain_block
  99. AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log|
  100. next if log.domain_block.nil?
  101. log.update_attribute('human_identifier', log.domain_block.domain)
  102. end
  103. end
  104. def process_logs_for_domain_allow
  105. AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log|
  106. next if log.domain_allow.nil?
  107. log.update_attribute('human_identifier', log.domain_allow.domain)
  108. end
  109. end
  110. def process_logs_for_email_domain_block
  111. AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log|
  112. next if log.email_domain_block.nil?
  113. log.update_attribute('human_identifier', log.email_domain_block.domain)
  114. end
  115. end
  116. def process_logs_for_unavailable_domain
  117. AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log|
  118. next if log.unavailable_domain.nil?
  119. log.update_attribute('human_identifier', log.unavailable_domain.domain)
  120. end
  121. end
  122. def process_logs_for_status
  123. AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log|
  124. next if log.status.nil?
  125. log.update_attribute('human_identifier', log.status.account.acct)
  126. log.update_attribute('permalink', log.status.uri)
  127. end
  128. end
  129. def process_logs_for_account_warning
  130. AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log|
  131. next if log.account_warning.nil?
  132. log.update_attribute('human_identifier', log.account_warning.account.acct)
  133. end
  134. end
  135. def process_logs_for_announcement
  136. AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log|
  137. next if log.announcement.nil?
  138. log.update_attribute('human_identifier', log.announcement.text)
  139. end
  140. end
  141. def process_logs_for_ip_block
  142. AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log|
  143. next if log.ip_block.nil?
  144. log.update_attribute('human_identifier', "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}")
  145. end
  146. end
  147. def process_logs_for_custom_emoji
  148. AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log|
  149. next if log.custom_emoji.nil?
  150. log.update_attribute('human_identifier', log.custom_emoji.shortcode)
  151. end
  152. end
  153. def process_logs_for_canonical_email_block
  154. AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log|
  155. next if log.canonical_email_block.nil?
  156. log.update_attribute('human_identifier', log.canonical_email_block.canonical_email_hash)
  157. end
  158. end
  159. def process_logs_for_appeal
  160. AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal', human_identifier: nil).find_each do |log|
  161. next if log.appeal.nil?
  162. log.update_attribute('human_identifier', log.appeal.account.acct)
  163. log.update_attribute('route_param', log.appeal.account_warning_id)
  164. end
  165. end
  166. end