discourse/db/migrate/20190424065841_add_post_image_indexes.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

27 lines
611 B
Ruby

# frozen_string_literal: true
class AddPostImageIndexes < ActiveRecord::Migration[5.2]
def change
%w{
large_images
broken_images
downloaded_images
}.each do |field|
execute <<~SQL
DELETE FROM post_custom_fields f
WHERE name = '#{field}' AND id > (
SELECT MIN(f2.id) FROM post_custom_fields f2
WHERE f2.post_id = f.post_id AND f2.name = f.name
)
SQL
add_index :post_custom_fields, [:post_id],
name: "post_custom_field_#{field}_idx",
unique: true,
where: "name = '#{field}'"
end
end
end