-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate from Paperclip (EOL) to Active Storage #1114
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3d8f19e
make migration of paperclip to active storage issue #841
johnbumgardner 1db9472
update helper to remove paperclip
johnbumgardner 7a414b2
modify migration to work depending on sql version
johnbumgardner 9ca3081
use different fixture for file tests
johnbumgardner f6c0419
try without rails join
johnbumgardner 7afd001
try using with rack attached
johnbumgardner 49a4504
rework to remove old paperclip fields
johnbumgardner 5772b34
fix rubocop issues
johnbumgardner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ Design | |
|
||
.passenger | ||
.vagrant | ||
storage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
db/migrate/20230526211831_create_active_storage_tables.active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < ActiveRecord::Migration[5.2] | ||
def change | ||
# Use Active Record's configured type for primary and foreign keys | ||
primary_key_type, foreign_key_type = primary_and_foreign_key_types | ||
|
||
create_table :active_storage_blobs, id: primary_key_type do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
t.string :service_name, null: false | ||
t.bigint :byte_size, null: false | ||
t.string :checksum, null: false | ||
t.datetime :created_at, null: false | ||
|
||
t.index [ :key ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments, id: primary_key_type do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type | ||
t.references :blob, null: false, type: foreign_key_type | ||
|
||
t.datetime :created_at, null: false | ||
|
||
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
|
||
create_table :active_storage_variant_records, id: primary_key_type do |t| | ||
t.belongs_to :blob, null: false, index: false, type: foreign_key_type | ||
t.string :variation_digest, null: false | ||
|
||
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
|
||
private | ||
def primary_and_foreign_key_types | ||
config = Rails.configuration.generators | ||
setting = config.options[config.orm][:primary_key_type] | ||
primary_key_type = setting || :primary_key | ||
foreign_key_type = setting || :bigint | ||
[primary_key_type, foreign_key_type] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
class ConvertToActiveStorage < ActiveRecord::Migration[5.2] | ||
require 'open-uri' | ||
|
||
def up | ||
# postgres | ||
get_blob_id = 'LASTVAL()' | ||
# mariadb | ||
# get_blob_id = 'LAST_INSERT_ID()' | ||
# sqlite | ||
# get_blob_id = 'LAST_INSERT_ROWID()' | ||
|
||
active_storage_blob_statement = 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 | ||
|
||
active_storage_attachment_statement = 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) | ||
SQL | ||
|
||
Rails.application.eager_load! | ||
models = ActiveRecord::Base.descendants.reject { |model| model.abstract_class? || model == ActionMailbox::InboundEmail || model == ActionText::RichText} | ||
|
||
transaction do | ||
models.each do |model| | ||
attachments = model.column_names.map do |c| | ||
if c =~ /(.+)_file_name$/ | ||
$1 | ||
end | ||
end.compact | ||
|
||
if attachments.blank? | ||
next | ||
end | ||
|
||
model.find_each.each do |instance| | ||
attachments.each do |attachment| | ||
if instance.send(attachment).path.blank? | ||
next | ||
end | ||
|
||
ActiveRecord::Base.connection.execute_prepared( | ||
'active_storage_blob_statement', [ | ||
key(instance, attachment), | ||
instance.send("#{attachment}_file_name"), | ||
instance.send("#{attachment}_content_type"), | ||
instance.send("#{attachment}_file_size"), | ||
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 | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
|
||
private | ||
|
||
def key(instance, attachment) | ||
SecureRandom.uuid | ||
# Alternatively: | ||
# instance.send("#{attachment}_file_name") | ||
end | ||
|
||
def checksum(attachment) | ||
# local files stored on disk: | ||
url = attachment.path | ||
Digest::MD5.base64digest(File.read(url)) | ||
|
||
# remote files stored on another person's computer: | ||
# url = attachment.url | ||
# Digest::MD5.base64digest(Net::HTTP.get(URI(url))) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Rubocop
Avoid methods longer than 10 lines of code.