From b4b42c7cc9354f67378c88fbd9c19c9e44b7fe26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 27 Jul 2021 00:22:08 +0200 Subject: [PATCH] Fix duplicate entries in Active Storage migation Using `LASTVAL()` wasn't working 100% of the time for some reason after inserting and deleting some rows. So we're using `RETURNING` instead. --- lib/tasks/active_storage.rake | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/tasks/active_storage.rake b/lib/tasks/active_storage.rake index 7eb4ab919..067957368 100644 --- a/lib/tasks/active_storage.rake +++ b/lib/tasks/active_storage.rake @@ -5,18 +5,16 @@ namespace :active_storage do desc "Copy paperclip's attachment database columns to active storage" task migrate_from_paperclip: :environment do logger = ApplicationLogger.new - get_blob_id = "LASTVAL()" - ActiveRecord::Base.connection.raw_connection.prepare("active_storage_blob_statement", <<-SQL) - INSERT INTO active_storage_blobs ( - key, filename, content_type, metadata, byte_size, checksum, created_at - ) VALUES ($1, $2, $3, '{}', $4, $5, $6) - SQL - - ActiveRecord::Base.connection.raw_connection.prepare("active_storage_attachment_statement", <<-SQL) - INSERT INTO active_storage_attachments ( - name, record_type, record_id, blob_id, created_at - ) VALUES ($1, $2, $3, #{get_blob_id}, $4) + ActiveRecord::Base.connection.raw_connection.prepare("active_storage_statement", <<-SQL) + with rows as( + INSERT INTO active_storage_blobs ( + key, filename, content_type, metadata, byte_size, checksum, created_at + ) VALUES ($1, $2, $3, '{}', $4, $5, $6) RETURNING id + ) + INSERT INTO active_storage_attachments ( + name, record_type, record_id, blob_id, created_at + ) VALUES ($7, $8, $9, (SELECT id FROM rows), $10) SQL Rails.application.eager_load! @@ -41,17 +39,13 @@ namespace :active_storage do next if instance.send(attachment).path.blank? ActiveRecord::Base.connection.raw_connection.exec_prepared( - "active_storage_blob_statement", [ + "active_storage_statement", [ SecureRandom.uuid, # Alternatively instance.send("#{attachment}_file_name"), instance.send("#{attachment}_file_name"), instance.send("#{attachment}_content_type"), instance.send("#{attachment}_file_size"), Digest::MD5.base64digest(File.read(instance.send(attachment).path)), - instance.updated_at.iso8601 - ]) - - ActiveRecord::Base.connection.raw_connection.exec_prepared( - "active_storage_attachment_statement", [ + instance.updated_at.iso8601, attachment, model.name, instance.id,