20221101190723_backfill_admin_action_logs.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, foreign_key: :user_id
  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(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(human_identifier: log.user.account.acct, route_param: log.user.account_id)
  71. end
  72. Admin::ActionLog.where(target_type: 'Report', human_identifier: nil).in_batches.update_all('human_identifier = target_id::text')
  73. AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log|
  74. next if log.domain_block.nil?
  75. log.update(human_identifier: log.domain_block.domain)
  76. end
  77. AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log|
  78. next if log.domain_allow.nil?
  79. log.update(human_identifier: log.domain_allow.domain)
  80. end
  81. AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log|
  82. next if log.email_domain_block.nil?
  83. log.update(human_identifier: log.email_domain_block.domain)
  84. end
  85. AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log|
  86. next if log.unavailable_domain.nil?
  87. log.update(human_identifier: log.unavailable_domain.domain)
  88. end
  89. AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log|
  90. next if log.status.nil?
  91. log.update(human_identifier: log.status.account.acct, permalink: log.status.uri)
  92. end
  93. AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log|
  94. next if log.account_warning.nil?
  95. log.update(human_identifier: log.account_warning.account.acct)
  96. end
  97. AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log|
  98. next if log.announcement.nil?
  99. log.update(human_identifier: log.announcement.text)
  100. end
  101. AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log|
  102. next if log.ip_block.nil?
  103. log.update(human_identifier: "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}")
  104. end
  105. AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log|
  106. next if log.custom_emoji.nil?
  107. log.update(human_identifier: log.custom_emoji.shortcode)
  108. end
  109. AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log|
  110. next if log.canonical_email_block.nil?
  111. log.update(human_identifier: log.canonical_email_block.canonical_email_hash)
  112. end
  113. AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal', human_identifier: nil).find_each do |log|
  114. next if log.appeal.nil?
  115. log.update(human_identifier: log.appeal.account.acct, route_param: log.appeal.account_warning_id)
  116. end
  117. end
  118. end
  119. def down; end
  120. end