-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
feat: Script to migrate images between providers #9117
Open
FranciscoJBrito
wants to merge
6
commits into
chatwoot:develop
Choose a base branch
from
FranciscoJBrito:feature/#7907-script-to-change-local-storage-to-aws
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c35034
feature/#7907-script-to-change-local-storage-to-aws
FranciscoJBrito cf29780
feature/#7907-script-to-change-local-storage-to-aws
FranciscoJBrito f5b2e7b
fixing path for RuboCop
FranciscoJBrito f701a4d
Merge branch 'chatwoot:develop' into feature/#7907-script-to-change-l…
FranciscoJBrito 43d9500
fixing the path in the rake task test
FranciscoJBrito c8a9e36
fixing error in the test
FranciscoJBrito File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'yaml' | ||
require 'erb' | ||
|
||
module ActiveStorage | ||
class Migrator | ||
def self.migrate(from_service_name, to_service_name) | ||
yaml_with_env = ERB.new(File.read('config/storage.yml')).result | ||
configs = YAML.load(yaml_with_env) | ||
|
||
from_service = ActiveStorage::Service.configure(from_service_name, { from_service_name.to_sym => configs[from_service_name.to_s] }) | ||
to_service = ActiveStorage::Service.configure(to_service_name, { to_service_name.to_sym => configs[to_service_name.to_s] }) | ||
|
||
# Check if services are configured correctly | ||
if from_service.nil? || to_service.nil? | ||
puts "Error: The services '#{from_service_name}' or '#{to_service_name}' are not configured correctly." | ||
return | ||
end | ||
|
||
# Configure the blob service for the source service | ||
ActiveStorage::Blob.service = from_service | ||
|
||
puts "#{ActiveStorage::Blob.count} Blobs to migrate from #{from_service_name} to #{to_service_name}" | ||
ActiveStorage::Blob.find_each do |blob| | ||
next unless blob.image? | ||
|
||
print '.' | ||
|
||
blob.open do |io| | ||
checksum = blob.checksum | ||
to_service.upload(blob.key, io, checksum: checksum) | ||
end | ||
end | ||
end | ||
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,13 @@ | ||
namespace :storage do | ||
desc 'Migrate blobs from one storage service to another' | ||
task migrate: :environment do | ||
from_service = ENV['FROM'] | ||
to_service = ENV['TO'] | ||
|
||
if from_service.nil? || to_service.nil? | ||
raise 'Missing FROM or TO argument. Usage: FROM=service_name TO=service_name rake storage:migrate' | ||
end | ||
|
||
ActiveStorage::Migrator.migrate(from_service.to_sym, to_service.to_sym) | ||
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,34 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ActiveStorage::Migrator do | ||
describe '.migrate' do | ||
let(:from_service_stub) { double('from_service') } | ||
let(:to_service_stub) { double('to_service') } | ||
let(:blob_spy) { spy('ActiveStorage::Blob') } | ||
|
||
before do | ||
allow(ActiveStorage::Service).to receive(:configure).with(:from_service, any_args).and_return(from_service_stub) | ||
allow(ActiveStorage::Service).to receive(:configure).with(:to_service, any_args).and_return(to_service_stub) | ||
allow(ActiveStorage::Blob).to receive(:find_each).and_yield(blob_spy) | ||
end | ||
|
||
context 'when services are configured correctly' do | ||
it 'migrates blobs from one service to another' do | ||
allow(blob_spy).to receive(:image?).and_return(true) | ||
|
||
expect(ActiveStorage::Service).to receive(:configure).with(:from_service, any_args) | ||
expect(ActiveStorage::Service).to receive(:configure).with(:to_service, any_args) | ||
|
||
described_class.migrate(:from_service, :to_service) | ||
end | ||
end | ||
|
||
context 'when services are not configured correctly' do | ||
it 'prints an error message' do | ||
allow(from_service_stub).to receive(:nil?).and_return(true) | ||
|
||
expect { described_class.migrate(:from_service, :to_service) }.to output("Error: The services 'from_service' or 'to_service' are not configured correctly.\n").to_stdout | ||
end | ||
end | ||
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,28 @@ | ||
require 'rails_helper' | ||
require 'rake' | ||
|
||
RSpec.describe 'storage_migrations' do | ||
describe 'rake task' do | ||
|
||
context 'when FROM argument is missing' do | ||
before do | ||
ENV['FROM'] = nil | ||
end | ||
|
||
it 'raises an error' do | ||
expect { Rake::Task['storage:migrate'].invoke }.to raise_error(RuntimeError, 'Missing FROM or TO argument. Usage: FROM=service_name TO=service_name rake storage:migrate') | ||
end | ||
end | ||
|
||
context 'when TO argument is missing' do | ||
before do | ||
ENV['FROM'] = 'service_name' | ||
ENV['TO'] = nil | ||
end | ||
|
||
it 'raises an error' do | ||
expect { Rake::Task['storage:migrate'].invoke }.to raise_error(RuntimeError, 'Missing FROM or TO argument. Usage: FROM=service_name TO=service_name rake storage:migrate') | ||
end | ||
end | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line prevents uploading of audio files received via WhatsApp. It will possibly impact PDF's and other files in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @hiagodotme, of course, I assumed they wanted to export only images because the issue specifically mentioned images, but we can add other types of files without a problem. 👍🏼