DEV: Remove use of ActiveRecord in migration.

It makes the migration brittle to changes. If I could I would go back in
time and tell the 2016 version of me that.
This commit is contained in:
Guo Xiang Tan 2019-01-10 11:10:21 +08:00
parent c2bca9cabe
commit 7896c74c2b

View File

@ -3,16 +3,20 @@ class AddUploadsToCategories < ActiveRecord::Migration[4.2]
add_column :categories, :uploaded_logo_id, :integer, index: true
add_column :categories, :uploaded_background_id, :integer, index: true
transaction do
Category.find_each do |category|
logo_upload = Upload.find_by(url: category.logo_url)
background_upload = Upload.find_by(url: category.background_url)
execute <<~SQL
UPDATE categories
SET uploaded_logo_id = u.id
FROM categories c
LEFT JOIN uploads u ON u.url = c.logo_url
WHERE u.url IS NOT NULL
SQL
category.update_columns(
uploaded_logo_id: logo_upload&.id,
uploaded_background_id: background_upload&.id
)
end
end
execute <<~SQL
UPDATE categories
SET uploaded_background_id = u.id
FROM categories c
LEFT JOIN uploads u ON u.url = c.background_url
WHERE u.url IS NOT NULL
SQL
end
end