2020-04-27 07:29:08 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-24 00:08:26 +10:00
|
|
|
require_relative '../../../config/boot'
|
|
|
|
require_relative '../../../config/environment'
|
|
|
|
require_relative 'helper'
|
2020-04-27 07:29:08 +10:00
|
|
|
|
2023-05-24 00:08:26 +10:00
|
|
|
module Mastodon::CLI
|
|
|
|
class Upgrade < Thor
|
|
|
|
include Helper
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
def self.exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
CURRENT_STORAGE_SCHEMA_VERSION = 1
|
|
|
|
|
|
|
|
option :dry_run, type: :boolean, default: false
|
|
|
|
option :verbose, type: :boolean, default: false, aliases: [:v]
|
|
|
|
desc 'storage-schema', 'Upgrade storage schema of various file attachments to the latest version'
|
|
|
|
long_desc <<~LONG_DESC
|
|
|
|
Iterates over every file attachment of every record and, if its storage schema is outdated, performs the
|
|
|
|
necessary upgrade to the latest one. In practice this means e.g. moving files to different directories.
|
|
|
|
|
|
|
|
Will most likely take a long time.
|
|
|
|
LONG_DESC
|
|
|
|
def storage_schema
|
|
|
|
progress = create_progress_bar(nil)
|
|
|
|
dry_run = dry_run? ? ' (DRY RUN)' : ''
|
|
|
|
records = 0
|
|
|
|
|
|
|
|
klasses = [
|
|
|
|
Account,
|
|
|
|
CustomEmoji,
|
|
|
|
MediaAttachment,
|
|
|
|
PreviewCard,
|
|
|
|
]
|
|
|
|
|
|
|
|
klasses.each do |klass|
|
|
|
|
attachment_names = klass.attachment_definitions.keys
|
|
|
|
|
|
|
|
klass.find_each do |record|
|
|
|
|
attachment_names.each do |attachment_name|
|
|
|
|
attachment = record.public_send(attachment_name)
|
2020-05-16 01:15:24 +10:00
|
|
|
upgraded = false
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
next if attachment.blank? || attachment.storage_schema_version >= CURRENT_STORAGE_SCHEMA_VERSION
|
|
|
|
|
2020-05-16 01:15:24 +10:00
|
|
|
styles = attachment.styles.keys
|
|
|
|
|
|
|
|
styles << :original unless styles.include?(:original)
|
|
|
|
|
|
|
|
styles.each do |style|
|
2023-02-19 09:09:40 +11:00
|
|
|
success = case Paperclip::Attachment.default_options[:storage]
|
|
|
|
when :s3
|
|
|
|
upgrade_storage_s3(progress, attachment, style)
|
|
|
|
when :fog
|
|
|
|
upgrade_storage_fog(progress, attachment, style)
|
|
|
|
when :filesystem
|
|
|
|
upgrade_storage_filesystem(progress, attachment, style)
|
|
|
|
end
|
2020-04-27 07:29:08 +10:00
|
|
|
|
2020-05-16 01:15:24 +10:00
|
|
|
upgraded = true if style == :original && success
|
|
|
|
|
2020-04-27 07:29:08 +10:00
|
|
|
progress.increment
|
|
|
|
end
|
|
|
|
|
2020-05-16 01:15:24 +10:00
|
|
|
attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION) if upgraded
|
2020-04-27 07:29:08 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
if record.changed?
|
|
|
|
record.save unless dry_run?
|
|
|
|
records += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
progress.total = progress.progress
|
|
|
|
progress.finish
|
|
|
|
|
|
|
|
say("Upgraded storage schema of #{records} records#{dry_run}", :green, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def upgrade_storage_s3(progress, attachment, style)
|
|
|
|
previous_storage_schema_version = attachment.storage_schema_version
|
|
|
|
object = attachment.s3_object(style)
|
2020-05-16 01:15:24 +10:00
|
|
|
success = true
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
|
|
|
|
|
2020-05-16 01:15:24 +10:00
|
|
|
new_object = attachment.s3_object(style)
|
2020-04-27 07:29:08 +10:00
|
|
|
|
2020-05-16 01:15:24 +10:00
|
|
|
if new_object.key != object.key && object.exists?
|
|
|
|
progress.log("Moving #{object.key} to #{new_object.key}") if options[:verbose]
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
begin
|
2020-05-18 01:27:36 +10:00
|
|
|
object.move_to(new_object, acl: attachment.s3_permissions(style)) unless dry_run?
|
2020-04-27 07:29:08 +10:00
|
|
|
rescue => e
|
|
|
|
progress.log(pastel.red("Error processing #{object.key}: #{e}"))
|
2020-05-16 01:15:24 +10:00
|
|
|
success = false
|
2020-04-27 07:29:08 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Because we move files style-by-style, it's important to restore
|
|
|
|
# previous version at the end. The upgrade will be recorded after
|
|
|
|
# all styles are updated
|
|
|
|
attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
|
2020-05-16 01:15:24 +10:00
|
|
|
success
|
2020-04-27 07:29:08 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def upgrade_storage_fog(_progress, _attachment, _style)
|
|
|
|
say('The fog storage driver is not supported for this operation at this time', :red)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def upgrade_storage_filesystem(progress, attachment, style)
|
|
|
|
previous_storage_schema_version = attachment.storage_schema_version
|
|
|
|
previous_path = attachment.path(style)
|
2020-05-16 01:15:24 +10:00
|
|
|
success = true
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
|
|
|
|
|
|
|
|
upgraded_path = attachment.path(style)
|
|
|
|
|
|
|
|
if upgraded_path != previous_path && File.exist?(previous_path)
|
|
|
|
progress.log("Moving #{previous_path} to #{upgraded_path}") if options[:verbose]
|
|
|
|
|
|
|
|
begin
|
|
|
|
unless dry_run?
|
|
|
|
FileUtils.mkdir_p(File.dirname(upgraded_path))
|
|
|
|
FileUtils.mv(previous_path, upgraded_path)
|
|
|
|
|
|
|
|
begin
|
2020-05-04 21:51:34 +10:00
|
|
|
FileUtils.rmdir(File.dirname(previous_path), parents: true)
|
2020-04-27 07:29:08 +10:00
|
|
|
rescue Errno::ENOTEMPTY
|
|
|
|
# OK
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
progress.log(pastel.red("Error processing #{previous_path}: #{e}"))
|
2020-05-16 01:15:24 +10:00
|
|
|
success = false
|
2020-04-27 07:29:08 +10:00
|
|
|
|
|
|
|
unless dry_run?
|
|
|
|
begin
|
2020-05-04 21:51:34 +10:00
|
|
|
FileUtils.rmdir(File.dirname(upgraded_path), parents: true)
|
2020-04-27 07:29:08 +10:00
|
|
|
rescue Errno::ENOTEMPTY
|
|
|
|
# OK
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Because we move files style-by-style, it's important to restore
|
|
|
|
# previous version at the end. The upgrade will be recorded after
|
|
|
|
# all styles are updated
|
|
|
|
attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
|
2020-05-16 01:15:24 +10:00
|
|
|
success
|
2020-04-27 07:29:08 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|