Move the uploads/system folder to uploads/-/system

Without downtime, so we need the symlinks
This commit is contained in:
Bob Van Landuyt 2017-07-17 13:02:28 +02:00
parent 27a6aa4f51
commit 79f591df4d
5 changed files with 198 additions and 1 deletions

View file

@ -0,0 +1,60 @@
class MoveSystemUploadFolder < ActiveRecord::Migration
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
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
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

View file

@ -0,0 +1,40 @@
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class CleanupMoveSystemUploadFolderSymlink < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
if File.symlink?(old_directory)
say "Removing #{old_directory} -> #{new_directory} symlink"
FileUtils.rm(old_directory)
else
say "Symlink #{old_directory} non existant, nothing to do."
end
end
def down
if File.directory?(new_directory)
say "Symlinking #{old_directory} -> #{new_directory}"
FileUtils.ln_s(new_directory, old_directory)
else
say "#{new_directory} doesn't exist, skipping."
end
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
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170713104829) do
ActiveRecord::Schema.define(version: 20170717111152) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -0,0 +1,35 @@
require 'spec_helper'
require Rails.root.join("db", "post_migrate", "20170717111152_cleanup_move_system_upload_folder_symlink.rb")
describe CleanupMoveSystemUploadFolderSymlink do
let(:migration) { described_class.new }
let(:test_base) { File.join(Rails.root, 'tmp', 'tests', 'move-system-upload-folder') }
let(:test_folder) { File.join(test_base, '-', 'system') }
before do
allow(migration).to receive(:base_directory).and_return(test_base)
FileUtils.rm_rf(test_base)
FileUtils.mkdir_p(test_folder)
allow(migration).to receive(:say)
end
describe '#up' do
before do
FileUtils.ln_s(test_folder, File.join(test_base, 'system'))
end
it 'removes the symlink' do
migration.up
expect(File.exist?(File.join(test_base, 'system'))).to be_falsey
end
end
describe '#down' do
it 'creates the symlink' do
migration.down
expect(File.symlink?(File.join(test_base, 'system'))).to be_truthy
end
end
end

View file

@ -0,0 +1,62 @@
require 'spec_helper'
require Rails.root.join("db", "migrate", "20170717074009_move_system_upload_folder.rb")
describe MoveSystemUploadFolder do
let(:migration) { described_class.new }
let(:test_base) { File.join(Rails.root, 'tmp', 'tests', 'move-system-upload-folder') }
before do
allow(migration).to receive(:base_directory).and_return(test_base)
FileUtils.rm_rf(test_base)
FileUtils.mkdir_p(test_base)
allow(migration).to receive(:say)
end
describe '#up' do
let(:test_folder) { File.join(test_base, 'system') }
let(:test_file) { File.join(test_folder, 'file') }
before do
FileUtils.mkdir_p(test_folder)
FileUtils.touch(test_file)
end
it 'moves the related folder' do
migration.up
expect(File.exist?(File.join(test_base, '-', 'system', 'file'))).to be_truthy
end
it 'creates a symlink linking making the new folder available on the old path' do
migration.up
expect(File.symlink?(File.join(test_base, 'system'))).to be_truthy
expect(File.exist?(File.join(test_base, 'system', 'file'))).to be_truthy
end
end
describe '#down' do
let(:test_folder) { File.join(test_base, '-', 'system') }
let(:test_file) { File.join(test_folder, 'file') }
before do
FileUtils.mkdir_p(test_folder)
FileUtils.touch(test_file)
end
it 'moves the system folder back to the old location' do
migration.down
expect(File.exist?(File.join(test_base, 'system', 'file'))).to be_truthy
end
it 'removes the symlink if it existed' do
FileUtils.ln_s(test_folder, File.join(test_base, 'system'))
migration.down
expect(File.directory?(File.join(test_base, 'system'))).to be_truthy
expect(File.symlink?(File.join(test_base, 'system'))).to be_falsey
end
end
end