PERF: Migrate search data after locale rename (#11831)

The default locale `en_US` has been renamed into `en`. This tries to migrate existing search data to avoid resource intensive reindexing.
This commit is contained in:
Gerhard Schlager 2021-01-25 14:30:17 +01:00 committed by GitHub
parent 77c48644eb
commit e65c5b0aad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
class MigrateSearchDataAfterDefaultLocaleRename < ActiveRecord::Migration[6.0]
def up
move_search_data("category_search_data")
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
def down
raise ActiveRecord::IrreversibleMigration
end
private
def move_search_data(table_name)
execute <<~SQL
UPDATE #{table_name} x
SET locale = 'en'
WHERE locale = 'en_US'
SQL
rescue
# Probably a unique key constraint violation. A background job might have inserted conflicting data during the UPDATE.
# We can safely ignore this error. The ReindexSearch job will eventually fix the data.
end
end