Created
October 29, 2012 16:32
-
-
Save rnhurt/3974694 to your computer and use it in GitHub Desktop.
Rake task to manage DynamoDB tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :dynamodb do | |
desc "Create DynamoDB tables" | |
task :create => :environment do | |
puts "Creating tables in the #{Rails.env} environment..." | |
Rails.application.eager_load! # Force the loading of modules | |
prefix = AWS::Record.table_prefix # Table name prefix | |
tables = AWS::DynamoDB.new.tables # Create an AWS connection | |
# Loop through all the models and create each table | |
AWS::Record::HashModel.descendants.each do |model| | |
name = prefix + model.name | |
puts " - creating '#{name}' table..." | |
begin | |
model.create_table(10,5) | |
sleep 1 while tables[name].status == :creating | |
rescue Exception => e | |
puts "\tTable '#{name}' not created: #{e}" | |
end | |
end | |
end | |
desc "Delete ALL DynamoDB tables" | |
task :delete => :environment do | |
puts "Deleting tables from the #{Rails.env} environment..." | |
Rails.application.eager_load! # Force the loading of modules | |
prefix = AWS::Record.table_prefix # Table name prefix | |
tables = AWS::DynamoDB.new.tables # Create an AWS connection | |
# Loop through all the models and create each table | |
AWS::Record::HashModel.descendants.each do |model| | |
name = prefix + model.name | |
puts " - deleting '#{name}' table..." | |
begin | |
tables[name].delete | |
# sleep 1 while tables[name].status == :deleting | |
rescue Exception => e | |
puts "\tTable '#{name}' not deleted: #{e}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment