Commit ae47aed2 by Mike Burns

Syntax fixes for DB migration

This is a combination of [a comment from @sankage] and [a gist by
@colinpetruno]. Many thanks to them for sharing their discoveries.

[a comment from @sankage]: https://github.com/thoughtbot/paperclip/pull/2568#issuecomment-381629629
[a gist by @colinpetruno]: https://gist.github.com/colinpetruno/037de4fafa4cff695b1d7905cd6fd7c2
parent 1a1f8a88
...@@ -132,18 +132,19 @@ class ConvertToActiveStorage < ActiveRecord::Migration[5.2] ...@@ -132,18 +132,19 @@ class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
# sqlite # sqlite
# get_blob_id = 'LAST_INSERT_ROWID()' # get_blob_id = 'LAST_INSERT_ROWID()'
active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL) active_storage_blob_statement = ActiveRecord::Base.connection.raw_connection.prepare('active_storage_blob_statement', <<-SQL)
INSERT INTO active_storage_blobs ( INSERT INTO active_storage_blobs (
`key`, filename, content_type, metadata, byte_size, checksum, created_at `key`, filename, content_type, metadata, byte_size, checksum, created_at
) VALUES (?, ?, ?, '{}', ?, ?, ?) ) VALUES ($1, $2, $3, '{}', $4, $5, $6)
SQL SQL
active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL) active_storage_attachment_statement = ActiveRecord::Base.connection.raw_connection.prepare('active_storage_attachment_statement', <<-SQL)
INSERT INTO active_storage_attachments ( INSERT INTO active_storage_attachments (
name, record_type, record_id, blob_id, created_at name, record_type, record_id, blob_id, created_at
) VALUES (?, ?, ?, #{get_blob_id}, ?) ) VALUES ($1, $2, $3, #{get_blob_id}, $4)
SQL SQL
Rails.application.eager_load!
models = ActiveRecord::Base.descendants.reject(&:abstract_class?) models = ActiveRecord::Base.descendants.reject(&:abstract_class?)
transaction do transaction do
...@@ -154,26 +155,37 @@ class ConvertToActiveStorage < ActiveRecord::Migration[5.2] ...@@ -154,26 +155,37 @@ class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
end end
end.compact end.compact
if attachments.blank?
next
end
model.find_each.each do |instance| model.find_each.each do |instance|
attachments.each do |attachment| attachments.each do |attachment|
active_storage_blob_statement.execute( if instance.send(attachment).path.blank?
key(instance, attachment), next
instance.send("#{attachment}_file_name"), end
instance.send("#{attachment}_content_type"),
instance.send("#{attachment}_file_size"), ActiveRecord::Base.connection.execute_prepared(
checksum(instance.send(attachment)), 'active_storage_blob_statement', [
instance.updated_at.iso8601 key(instance, attachment),
) instance.send("#{attachment}_file_name"),
instance.send("#{attachment}_content_type"),
active_storage_attachment_statement. instance.send("#{attachment}_file_size"),
execute(attachment, model.name, instance.id, instance.updated_at.iso8601) checksum(instance.send(attachment)),
instance.updated_at.iso8601
])
ActiveRecord::Base.connection.execute_prepared(
'active_storage_attachment_statement', [
attachment,
model.name,
instance.id,
instance.updated_at.iso8601,
])
end end
end end
end end
end end
active_storage_attachment_statement.close
active_storage_blob_statement.close
end end
def down def down
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment