Use `upsert_all` and `insert_all` to reduce back-and-forth in costly migrations (#29752)

pull/29764/head
Claire 10 months ago committed by GitHub
parent 9c24f2d6b1
commit de740dfb9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -4,46 +4,50 @@ class MigrateInteractionSettingsToPolicy < ActiveRecord::Migration[7.1]
disable_ddl_transaction! disable_ddl_transaction!
# Dummy classes, to make migration possible across version changes # Dummy classes, to make migration possible across version changes
class Account < ApplicationRecord
has_one :user, inverse_of: :account
has_one :notification_policy, inverse_of: :account
end
class User < ApplicationRecord class User < ApplicationRecord
belongs_to :account belongs_to :notification_policy, foreign_key: 'account_id', primary_key: 'account_id', optional: true, inverse_of: false
end end
class NotificationPolicy < ApplicationRecord class NotificationPolicy < ApplicationRecord; end
belongs_to :account
end
def up def up
User.includes(account: :notification_policy).find_each do |user| User.includes(:notification_policy).in_batches do |users|
deserialized_settings = Oj.load(user.attributes_before_type_cast['settings']) NotificationPolicy.upsert_all(users.filter_map { |user| policy_for_user(user) })
end
end
next if deserialized_settings.nil? def down; end
policy = user.account.notification_policy || user.account.build_notification_policy private
requires_new_policy = false
if deserialized_settings['interactions.must_be_follower'] def policy_for_user(user)
policy.filter_not_followers = true deserialized_settings = Oj.load(user.attributes_before_type_cast['settings'])
requires_new_policy = true return if deserialized_settings.nil?
end
if deserialized_settings['interactions.must_be_following'] requires_new_policy = false
policy.filter_not_following = true
requires_new_policy = true
end
unless deserialized_settings['interactions.must_be_following_dm'] policy = {
policy.filter_private_mentions = false account_id: user.account_id,
requires_new_policy = true filter_not_followers: false,
end filter_not_following: false,
filter_private_mentions: true,
}
policy.save if requires_new_policy && policy.changed? if deserialized_settings['interactions.must_be_follower']
policy[:filter_not_followers] = true
requires_new_policy = true
end end
end
def down; end if deserialized_settings['interactions.must_be_following']
policy[:filter_not_following] = true
requires_new_policy = true
end
unless deserialized_settings['interactions.must_be_following_dm']
policy[:filter_private_mentions] = false
requires_new_policy = true
end
policy if requires_new_policy
end
end end

@ -4,51 +4,51 @@ class MigrateInteractionSettingsToPolicyAgain < ActiveRecord::Migration[7.1]
disable_ddl_transaction! disable_ddl_transaction!
# Dummy classes, to make migration possible across version changes # Dummy classes, to make migration possible across version changes
class Account < ApplicationRecord
has_one :user, inverse_of: :account
has_one :notification_policy, inverse_of: :account
end
class User < ApplicationRecord class User < ApplicationRecord
belongs_to :account belongs_to :notification_policy, foreign_key: 'account_id', primary_key: 'account_id', optional: true, inverse_of: false
end end
class NotificationPolicy < ApplicationRecord class NotificationPolicy < ApplicationRecord; end
belongs_to :account
end
def up def up
User.includes(account: :notification_policy).find_each do |user| User.includes(:notification_policy).in_batches do |users|
deserialized_settings = Oj.load(user.attributes_before_type_cast['settings']) NotificationPolicy.insert_all(users.filter_map { |user| policy_for_user(user) })
end
end
def down; end
next if deserialized_settings.nil? private
# If the user has configured a notification policy, don't override it def policy_for_user(user)
next if user.account.notification_policy.present? deserialized_settings = Oj.load(user.attributes_before_type_cast['settings'])
return if deserialized_settings.nil?
return if user.notification_policy.present?
policy = user.account.build_notification_policy requires_new_policy = false
requires_new_policy = false
if deserialized_settings['interactions.must_be_follower'] policy = {
policy.filter_not_followers = true account_id: user.account_id,
requires_new_policy = true filter_not_followers: false,
end filter_not_following: false,
filter_private_mentions: true,
}
if deserialized_settings['interactions.must_be_following'] if deserialized_settings['interactions.must_be_follower']
policy.filter_not_following = true policy[:filter_not_followers] = true
requires_new_policy = true requires_new_policy = true
end end
unless deserialized_settings['interactions.must_be_following_dm'] if deserialized_settings['interactions.must_be_following']
policy.filter_private_mentions = false policy[:filter_not_following] = true
requires_new_policy = true requires_new_policy = true
end end
policy.save if requires_new_policy && policy.changed? unless deserialized_settings['interactions.must_be_following_dm']
rescue ActiveRecord::RecordNotUnique policy[:filter_private_mentions] = false
next requires_new_policy = true
end end
end
def down; end policy if requires_new_policy
end
end end

Loading…
Cancel
Save