mirror of
https://github.com/discourse/discourse.git
synced 2025-02-22 12:17:12 +00:00
PERF: batch expensive post-migration (#11845)
Run the 'MigrateSearchDataAfterDefaultLocaleRename' post migration in batches of 500k records. This will hopefully prevent any potential deadlocks on large tables.
This commit is contained in:
parent
568bad75c1
commit
c56ba6c9bd
@ -1,12 +1,10 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class MigrateSearchDataAfterDefaultLocaleRename < ActiveRecord::Migration[6.0]
|
class MigrateSearchDataAfterDefaultLocaleRename < ActiveRecord::Migration[6.0]
|
||||||
|
disable_ddl_transaction!
|
||||||
|
|
||||||
def up
|
def up
|
||||||
move_search_data("category_search_data")
|
%w{category tag topic post user}.each { |model| fix_search_data(model) }
|
||||||
move_search_data("post_search_data")
|
|
||||||
move_search_data("tag_search_data")
|
|
||||||
move_search_data("topic_search_data")
|
|
||||||
move_search_data("user_search_data")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
@ -15,14 +13,25 @@ class MigrateSearchDataAfterDefaultLocaleRename < ActiveRecord::Migration[6.0]
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def move_search_data(table_name)
|
def fix_search_data(model)
|
||||||
execute <<~SQL
|
key = "#{model}_id"
|
||||||
UPDATE #{table_name} x
|
table = "#{model}_search_data"
|
||||||
SET locale = 'en'
|
|
||||||
WHERE locale = 'en_US'
|
sql = <<~SQL
|
||||||
|
UPDATE #{table}
|
||||||
|
SET locale = 'en'
|
||||||
|
WHERE #{key} IN (
|
||||||
|
SELECT #{key}
|
||||||
|
FROM #{table}
|
||||||
|
WHERE locale = 'en_US'
|
||||||
|
LIMIT 500000
|
||||||
|
)
|
||||||
SQL
|
SQL
|
||||||
rescue
|
|
||||||
# Probably a unique key constraint violation. A background job might have inserted conflicting data during the UPDATE.
|
loop do
|
||||||
# We can safely ignore this error. The ReindexSearch job will eventually fix the data.
|
count = execute(sql).cmd_tuples
|
||||||
|
break if count == 0
|
||||||
|
puts "Migrated #{count} rows of #{table} to new locale."
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user