mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 12:43:54 -06:00
e45bca7298
In extreme circumstances when the uploads table is huge, the old version of this migration could take a very long time. The rewrite extracts the sha1 directly from the badges table and does an index based match on the uploads table
26 lines
640 B
Ruby
26 lines
640 B
Ruby
# frozen_string_literal: true
|
|
|
|
class AddImageUploadIdToBadges < ActiveRecord::Migration[6.0]
|
|
def change
|
|
add_column :badges, :image_upload_id, :integer
|
|
reversible do |dir|
|
|
dir.up do
|
|
DB.exec <<~SQL
|
|
UPDATE badges b1
|
|
SET image_upload_id = u.id
|
|
FROM (
|
|
SELECT id, (regexp_matches(b.image, '[a-f0-9]{40}'))[1] as sha1
|
|
FROM badges b
|
|
WHERE
|
|
b.image IS NOT NULL AND
|
|
b.image ~ '[a-f0-9]{40}'
|
|
) b2
|
|
JOIN uploads u ON u.sha1 = b2.sha1
|
|
WHERE
|
|
b1.id = b2.id
|
|
SQL
|
|
end
|
|
end
|
|
end
|
|
end
|