Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fletcherm/5349050 to your computer and use it in GitHub Desktop.
Save fletcherm/5349050 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
module TestProductionDataImportToFedora
module_function
def go(dir)
source_dir = find_source dir
validate_running_from_rails_root
reset_everything
load_mysql_data(source_dir)
stage_avatar_data(source_dir)
stage_project_content(source_dir)
migrate
import_into_fedora
end
def find_source(source_dir)
source_dir ||= Pathname.new(Dir.home).join('Desktop', 'production')
source_dir = Pathname.new(source_dir)
if !source_dir.exist?
puts "I couldn't find your source directory [#{source_dir}]!"
exit 1
end
source_dir
end
def validate_running_from_rails_root
if !(File.exist?('Rakefile') && File.exist?('config/environment.rb'))
puts "It doesn't look like you're running this from the Rails root directory! Please run this task from there."
exit 2
end
end
def reset_everything
task 'Resetting all local data in the development environment.' do
%w[ db:drop db:create file_store:clear fedora:clean ].each do |rake_task|
rake rake_task
end
end
end
def load_mysql_data(source_dir)
mysql_archive = find_file(source_dir, 'mysql')
task "Loading mysql data from [#{mysql_archive}]." do
run "gunzip -c #{mysql_archive} | mysql awesome_database"
end
end
def stage_avatar_data(source_dir)
avatar_data = find_file(source_dir, 'avatars')
avatar_directory = 'public/uploads'
task "Staging avatar data from [#{avatar_data}]." do
run "mkdir -p #{avatar_directory}"
run "tar xfj #{avatar_data} --directory=#{avatar_directory}"
run "mv #{avatar_directory}/{production,development}"
end
end
def stage_project_content(source_dir)
project_content = find_file(source_dir, 'uploads')
project_content_directory = 'uploads'
task "Staging project content from [#{project_content}]." do
run "mkdir -p #{project_content_directory}"
run "tar xfj #{project_content} --directory=#{project_content_directory}"
run "mv #{project_content_directory}/{production,development}"
end
end
def migrate
task 'Migrating the database to the newest schema.' do
rake 'db:migrate'
end
end
def import_into_fedora
task 'Running the Fedora data import. Good luck!' do
rake 'fedora:import'
end
end
def find_file(source_dir, name)
Dir["#{source_dir}/*"].find do |file| file.match(Regexp.new(name)) end
end
def task(message)
puts message
yield
puts "Done."
puts
end
def run(command)
environment = { 'RAILS_ENV' => 'development', 'LOG_FEDORA' => 'yesplease' }
puts "Running [#{command}]."
system environment, command
end
def rake(rake_task)
run "rake #{rake_task}"
end
end
TestProductionDataImportToFedora.go(ARGV.first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment