20170717074009_move_system_upload_folder.rb 1.67 KB
Newer Older
1
class MoveSystemUploadFolder < ActiveRecord::Migration[4.2]
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  include Gitlab::Database::MigrationHelpers
  disable_ddl_transaction!

  DOWNTIME = false

  def up
    unless file_storage?
      say 'Using object storage, no need to move.'
      return
    end

    unless File.directory?(old_directory)
      say "#{old_directory} doesn't exist, no need to move it."
      return
    end

18 19 20 21 22
    if File.directory?(new_directory)
      say "#{new_directory} already exists. No need to redo the move."
      return
    end

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    FileUtils.mkdir_p(File.join(base_directory, '-'))

    say "Moving #{old_directory} -> #{new_directory}"
    FileUtils.mv(old_directory, new_directory)
    FileUtils.ln_s(new_directory, old_directory)
  end

  def down
    unless file_storage?
      say 'Using object storage, no need to move.'
      return
    end

    unless File.directory?(new_directory)
      say "#{new_directory} doesn't exist, no need to move it."
      return
    end

41 42 43 44 45
    if !File.symlink?(old_directory) && File.directory?(old_directory)
      say "#{old_directory} already exists and is not a symlink, no need to revert."
      return
    end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    if File.symlink?(old_directory)
      say "Removing #{old_directory} -> #{new_directory} symlink"
      FileUtils.rm(old_directory)
    end

    say "Moving #{new_directory} -> #{old_directory}"
    FileUtils.mv(new_directory, old_directory)
  end

  def new_directory
    File.join(base_directory, '-', 'system')
  end

  def old_directory
    File.join(base_directory, 'system')
  end

  def base_directory
    File.join(Rails.root, 'public', 'uploads')
  end

  def file_storage?
    CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File
  end
end