Fix `tootctl upgrade storage-schema` misbehaving (#13761)
- Fix not moving original files of custom emojis - Fix command failing to move any files with S3 storage - Fix command marking records as upgraded when move failed Fix #13594
This commit is contained in:
		
							parent
							
								
									a319c1e60f
								
							
						
					
					
						commit
						2b91a3dac0
					
				| 
						 | 
					@ -41,23 +41,32 @@ module Mastodon
 | 
				
			||||||
        klass.find_each do |record|
 | 
					        klass.find_each do |record|
 | 
				
			||||||
          attachment_names.each do |attachment_name|
 | 
					          attachment_names.each do |attachment_name|
 | 
				
			||||||
            attachment = record.public_send(attachment_name)
 | 
					            attachment = record.public_send(attachment_name)
 | 
				
			||||||
 | 
					            upgraded   = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            next if attachment.blank? || attachment.storage_schema_version >= CURRENT_STORAGE_SCHEMA_VERSION
 | 
					            next if attachment.blank? || attachment.storage_schema_version >= CURRENT_STORAGE_SCHEMA_VERSION
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            attachment.styles.each_key do |style|
 | 
					            styles = attachment.styles.keys
 | 
				
			||||||
              case Paperclip::Attachment.default_options[:storage]
 | 
					
 | 
				
			||||||
              when :s3
 | 
					            styles << :original unless styles.include?(:original)
 | 
				
			||||||
                upgrade_storage_s3(progress, attachment, style)
 | 
					
 | 
				
			||||||
              when :fog
 | 
					            styles.each do |style|
 | 
				
			||||||
                upgrade_storage_fog(progress, attachment, style)
 | 
					              success = begin
 | 
				
			||||||
              when :filesystem
 | 
					                case Paperclip::Attachment.default_options[:storage]
 | 
				
			||||||
                upgrade_storage_filesystem(progress, attachment, style)
 | 
					                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
 | 
				
			||||||
              end
 | 
					              end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              upgraded = true if style == :original && success
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              progress.increment
 | 
					              progress.increment
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
 | 
					            attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION) if upgraded
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          if record.changed?
 | 
					          if record.changed?
 | 
				
			||||||
| 
						 | 
					@ -78,18 +87,20 @@ module Mastodon
 | 
				
			||||||
    def upgrade_storage_s3(progress, attachment, style)
 | 
					    def upgrade_storage_s3(progress, attachment, style)
 | 
				
			||||||
      previous_storage_schema_version = attachment.storage_schema_version
 | 
					      previous_storage_schema_version = attachment.storage_schema_version
 | 
				
			||||||
      object                          = attachment.s3_object(style)
 | 
					      object                          = attachment.s3_object(style)
 | 
				
			||||||
 | 
					      success                         = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
 | 
					      attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      upgraded_path = attachment.path(style)
 | 
					      new_object = attachment.s3_object(style)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if upgraded_path != object.key && object.exists?
 | 
					      if new_object.key != object.key && object.exists?
 | 
				
			||||||
        progress.log("Moving #{object.key} to #{upgraded_path}") if options[:verbose]
 | 
					        progress.log("Moving #{object.key} to #{new_object.key}") if options[:verbose]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        begin
 | 
					        begin
 | 
				
			||||||
          object.move_to(upgraded_path) unless dry_run?
 | 
					          object.move_to(new_object) unless dry_run?
 | 
				
			||||||
        rescue => e
 | 
					        rescue => e
 | 
				
			||||||
          progress.log(pastel.red("Error processing #{object.key}: #{e}"))
 | 
					          progress.log(pastel.red("Error processing #{object.key}: #{e}"))
 | 
				
			||||||
 | 
					          success = false
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -97,6 +108,7 @@ module Mastodon
 | 
				
			||||||
      # previous version at the end. The upgrade will be recorded after
 | 
					      # previous version at the end. The upgrade will be recorded after
 | 
				
			||||||
      # all styles are updated
 | 
					      # all styles are updated
 | 
				
			||||||
      attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
 | 
					      attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
 | 
				
			||||||
 | 
					      success
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def upgrade_storage_fog(_progress, _attachment, _style)
 | 
					    def upgrade_storage_fog(_progress, _attachment, _style)
 | 
				
			||||||
| 
						 | 
					@ -107,6 +119,7 @@ module Mastodon
 | 
				
			||||||
    def upgrade_storage_filesystem(progress, attachment, style)
 | 
					    def upgrade_storage_filesystem(progress, attachment, style)
 | 
				
			||||||
      previous_storage_schema_version = attachment.storage_schema_version
 | 
					      previous_storage_schema_version = attachment.storage_schema_version
 | 
				
			||||||
      previous_path                   = attachment.path(style)
 | 
					      previous_path                   = attachment.path(style)
 | 
				
			||||||
 | 
					      success                         = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
 | 
					      attachment.instance_write(:storage_schema_version, CURRENT_STORAGE_SCHEMA_VERSION)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -128,6 +141,7 @@ module Mastodon
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
        rescue => e
 | 
					        rescue => e
 | 
				
			||||||
          progress.log(pastel.red("Error processing #{previous_path}: #{e}"))
 | 
					          progress.log(pastel.red("Error processing #{previous_path}: #{e}"))
 | 
				
			||||||
 | 
					          success = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          unless dry_run?
 | 
					          unless dry_run?
 | 
				
			||||||
            begin
 | 
					            begin
 | 
				
			||||||
| 
						 | 
					@ -143,6 +157,7 @@ module Mastodon
 | 
				
			||||||
      # previous version at the end. The upgrade will be recorded after
 | 
					      # previous version at the end. The upgrade will be recorded after
 | 
				
			||||||
      # all styles are updated
 | 
					      # all styles are updated
 | 
				
			||||||
      attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
 | 
					      attachment.instance_write(:storage_schema_version, previous_storage_schema_version)
 | 
				
			||||||
 | 
					      success
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue