Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@avdi
Created October 28, 2009 06:42
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 avdi/220299 to your computer and use it in GitHub Desktop.
Save avdi/220299 to your computer and use it in GitHub Desktop.
require 'right_aws'
require 'progressbar'
module Simpledb
# Count the objects in a domain
def sdb_count(domain, options={})
results = session.select("select count(*) from #{domain}")
results[:items].first["Domain"]["Count"].first.to_i
end
def sdb_rm(domain)
puts "Are you sure?"
return unless gets =~ /^y/i
session.delete_domain(domain)
end
def sdb_copy(source_domain, destination_domain, options={})
sdb = session
sdb.list_domains do |results|
domains = results[:domains]
if domains.include?(destination_domain)
raise "Destination #{destination_domain} already exists"
end
end
sdb.create_domain(destination_domain)
count = sdb_count(source_domain)
progress = ProgressBar.new('copy', count)
sdb.select("select * from #{source_domain}") do |results|
results[:items].each do |record|
record.each_pair do |key, data|
sdb.put_attributes(destination_domain, key, data)
progress.inc
end
end
end
progress.finish
end
def sdb_dump(domain, options={})
dump = []
sdb = session
count = sdb_count(domain)
progress = ProgressBar.new('copy', count)
sdb.select("select * from #{domain}") do |results|
progress.inc(results.size)
results[:items].each do |record|
record.each_pair do |key, data|
dump << data
end
end
end
progress.finish
dump
end
private
def session
%w[AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY].each do |var|
ENV.key?(var) or raise "#{var} not set"
end
sdb = RightAws::SdbInterface.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment