mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Avoid using a temporary table in image url database migration
When using pgbouncer without a transaction, it's possible for the connection to change between statements in a migration. Instead, create a real table, and drop it when the migration is finished
This commit is contained in:
parent
1019789d1a
commit
0be68f147a
@ -9,8 +9,11 @@ class MigrateImageUrlToImageUploadId < ActiveRecord::Migration[6.0]
|
|||||||
# Defining regex here to avoid needing to double-escape the \ characters
|
# Defining regex here to avoid needing to double-escape the \ characters
|
||||||
regex = '\/(original|optimized)\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*'
|
regex = '\/(original|optimized)\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*'
|
||||||
|
|
||||||
|
# Can't use a real temporary table because we're running outside a transaction
|
||||||
|
# and the connection could change between statements
|
||||||
|
drop_temporary_table! # First check it doesn't already exist
|
||||||
execute <<~SQL
|
execute <<~SQL
|
||||||
CREATE TEMPORARY TABLE tmp_post_image_uploads(
|
CREATE TABLE tmp_post_image_uploads(
|
||||||
post_id int primary key,
|
post_id int primary key,
|
||||||
upload_id int
|
upload_id int
|
||||||
)
|
)
|
||||||
@ -91,5 +94,15 @@ class MigrateImageUrlToImageUploadId < ActiveRecord::Migration[6.0]
|
|||||||
SQL
|
SQL
|
||||||
last_update_id = result.last&.id
|
last_update_id = result.last&.id
|
||||||
end while last_update_id
|
end while last_update_id
|
||||||
|
ensure
|
||||||
|
drop_temporary_table!
|
||||||
|
end
|
||||||
|
|
||||||
|
def drop_temporary_table!
|
||||||
|
Migration::SafeMigrate.disable!
|
||||||
|
execute <<~SQL
|
||||||
|
DROP TABLE IF EXISTS tmp_post_image_uploads
|
||||||
|
SQL
|
||||||
|
Migration::SafeMigrate.enable!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user