mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Move hotlinked image information into a dedicated table (#16585)
This will make future changes to the 'pull hotlinked images' system easier. This commit should not introduce any functional change. For now, the old post_custom_field data is kept in the database. This will be dropped in a future commit.
This commit is contained in:
75
db/migrate/20220428094026_create_post_hotlinked_media.rb
Normal file
75
db/migrate/20220428094026_create_post_hotlinked_media.rb
Normal file
@@ -0,0 +1,75 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreatePostHotlinkedMedia < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<~SQL
|
||||
CREATE TYPE hotlinked_media_status AS ENUM('downloaded', 'too_large', 'download_failed', 'upload_create_failed')
|
||||
SQL
|
||||
end
|
||||
dir.down do
|
||||
execute <<~SQL
|
||||
DROP TYPE hotlinked_media_status
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
create_table :post_hotlinked_media do |t|
|
||||
t.bigint :post_id, null: false
|
||||
t.string :url, null: false
|
||||
t.column :status, :hotlinked_media_status, null: false
|
||||
t.bigint :upload_id
|
||||
t.timestamps
|
||||
|
||||
t.index [:post_id, :url], unique: true
|
||||
end
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<~SQL
|
||||
INSERT INTO post_hotlinked_media (post_id, url, status, upload_id, created_at, updated_at)
|
||||
SELECT
|
||||
post_id,
|
||||
obj.key AS url,
|
||||
'downloaded',
|
||||
obj.value::bigint AS upload_id,
|
||||
pcf.created_at,
|
||||
pcf.updated_at
|
||||
FROM post_custom_fields pcf
|
||||
JOIN json_each_text(pcf.value::json) obj ON true
|
||||
JOIN uploads ON obj.value::bigint = uploads.id
|
||||
WHERE name='downloaded_images'
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
INSERT INTO post_hotlinked_media (post_id, url, status, upload_id, created_at, updated_at)
|
||||
SELECT
|
||||
post_id,
|
||||
url.value,
|
||||
'download_failed',
|
||||
NULL,
|
||||
pcf.created_at,
|
||||
pcf.updated_at
|
||||
FROM post_custom_fields pcf
|
||||
JOIN json_array_elements_text(pcf.value::json) url ON true
|
||||
WHERE name='broken_images'
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
INSERT INTO post_hotlinked_media (post_id, url, status, upload_id, created_at, updated_at)
|
||||
SELECT
|
||||
post_id,
|
||||
url.value,
|
||||
'too_large',
|
||||
NULL,
|
||||
pcf.created_at,
|
||||
pcf.updated_at
|
||||
FROM post_custom_fields pcf
|
||||
JOIN json_array_elements_text(pcf.value::json) url ON true
|
||||
WHERE name='large_images'
|
||||
SQL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user