Skip to content

Instantly share code, notes, and snippets.

@nunosilva800
Created December 5, 2014 23:28
Show Gist options
  • Save nunosilva800/288baf157a0dee905d2b to your computer and use it in GitHub Desktop.
Save nunosilva800/288baf157a0dee905d2b to your computer and use it in GitHub Desktop.
Comfy Sofa export and import fixtures to S3
namespace :fixtures do
namespace :s3 do
require 'fileutils'
def cms_export from, to
# create the fixtures files from DB
ComfortableMexicanSofa::Fixture::Exporter.new(from, to).export!
# get the bucket
bucket = AWS::S3.new.buckets[ENV['S3_BUCKET_FIXTURES']]
unless bucket.exists?
bucket = AWS::S3.new.buckets.create(ENV['S3_BUCKET_FIXTURES'])
end
# delete contents on bucket
bucket.objects.with_prefix("db/cms_fixtures/#{to}").delete_all
# GO OVER ALL FILES AND UPLOAD
Dir.glob("db/cms_fixtures/#{to}/**/*.*") do |path|
obj = bucket.objects.create path, Pathname.new(path)
puts "uploaded file #{path}"
end
end
def cms_import from, to
# get the bucket
bucket = AWS::S3.new.buckets[ENV['S3_BUCKET_FIXTURES']]
unless bucket.exists?
raise "Bucket #{ENV['S3_BUCKET_FIXTURES']} not found!"
exit 1
end
# delete contents on folder
FileUtils.rm_rf("db/cms_fixtures/#{from}/")
FileUtils.mkdir_p("db/cms_fixtures/#{from}/")
# download fixture files
bucket.objects.with_prefix("db/cms_fixtures/#{from}").each do |obj|
# create dir
dirname = File.dirname(obj.key)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
# download file
File.open(obj.key, 'wb') do |file|
obj.read do |chunk|
file.write(chunk)
end
end
puts "downloaded file #{obj.key}"
end
# import to DB
ComfortableMexicanSofa::Fixture::Importer.new(from, to, :force).import!
end
desc "Export local CMS fixtures to S3 Bucket"
task :export => :environment do
Comfy::Cms::Site.all.each do |site|
cms_export site.identifier, site.identifier
end
end
desc "Import CMS fixtures on S3 Bucket"
task :import => :environment do
Comfy::Cms::Site.all.each do |site|
cms_import site.identifier, site.identifier
end
end
end
end
@nunosilva800
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment