lib/ddbcli/ddb-driver.rb in ddbcli-0.4.2.beta2 vs lib/ddbcli/ddb-driver.rb in ddbcli-0.5.0.beta
- old
+ new
@@ -59,10 +59,12 @@
do_show_regions(parsed)
when :SHOW_CREATE_TABLE
do_show_create_table(parsed)
when :ALTER_TABLE
do_alter_table(parsed)
+ when :ALTER_TABLE_INDEX
+ do_alter_table_index(parsed)
when :USE
do_use(parsed)
when :CREATE
do_create(parsed)
when :CREATE_LIKE
@@ -285,10 +287,11 @@
global_indexes = {}
(table_info['GlobalSecondaryIndexes'] || []).each do |i|
index_name = i['IndexName']
+ next unless i['KeySchema']
key_names = i['KeySchema'].map {|j| j['AttributeName'] }
proj_type = i['Projection']['ProjectionType']
proj_attrs = i['Projection']['NonKeyAttributes']
idx_throughput = i['ProvisionedThroughput']
@@ -357,37 +360,62 @@
end
def do_alter_table(parsed)
req_hash = {'TableName' => parsed.table}
- if parsed.index_name
- req_hash['GlobalSecondaryIndexUpdates'] = [{
- 'Update' => {
- 'IndexName' => parsed.index_name,
- 'ProvisionedThroughput' => throughput,
- },
- }]
- else
- if parsed.capacity
- req_hash['ProvisionedThroughput'] = {
- 'ReadCapacityUnits' => parsed.capacity[:read],
- 'WriteCapacityUnits' => parsed.capacity[:write],
+ if parsed.capacity
+ req_hash['ProvisionedThroughput'] = {
+ 'ReadCapacityUnits' => parsed.capacity[:read],
+ 'WriteCapacityUnits' => parsed.capacity[:write],
+ }
+ end
+
+ unless parsed.stream.nil?
+ if parsed.stream
+ view_type = (parsed.stream == true) ? 'KEYS_ONLY' : parsed.stream.to_s.upcase
+
+ req_hash['StreamSpecification'] = {
+ 'StreamEnabled' => true,
+ 'StreamViewType' => view_type,
}
+ else
+ req_hash['StreamSpecification'] = {'StreamEnabled' => false}
end
+ end
- unless parsed.stream.nil?
- if parsed.stream
- view_type = (parsed.stream == true) ? 'KEYS_ONLY' : parsed.stream.to_s.upcase
+ @client.query('UpdateTable', req_hash)
+ nil
+ end
- req_hash['StreamSpecification'] = {
- 'StreamEnabled' => true,
- 'StreamViewType' => view_type,
- }
- else
- req_hash['StreamSpecification'] = {'StreamEnabled' => false}
- end
- end
+ def do_alter_table_index(parsed)
+ req_hash = {'TableName' => parsed.table}
+ index_definition = parsed.index_definition
+ gsi_updates = req_hash['GlobalSecondaryIndexUpdates'] = []
+
+ case parsed.action
+ when 'Update'
+ gsi_updates << {
+ 'Update' => {
+ 'IndexName' => index_definition[:name],
+ 'ProvisionedThroughput' => {
+ 'ReadCapacityUnits' => index_definition[:capacity][:read],
+ 'WriteCapacityUnits' => index_definition[:capacity][:write],
+ },
+ },
+ }
+ when 'Create'
+ attr_defs = req_hash['AttributeDefinitions'] = []
+
+ gsi_updates << {
+ 'Create' => define_index(index_definition, attr_defs, :global => true),
+ }
+ when 'Delete'
+ gsi_updates << {
+ 'Delete' => {
+ 'IndexName' => index_definition[:name],
+ },
+ }
end
@client.query('UpdateTable', req_hash)
nil
end
@@ -456,108 +484,106 @@
# secondary index
local_indices = (parsed.indices || []).select {|i| not i[:global] }
global_indices = (parsed.indices || []).select {|i| i[:global] }
- define_attribute = lambda do |attr_name, attr_type|
- attr_defs = req_hash['AttributeDefinitions']
- same_attr = attr_defs.find {|i| i['AttributeName'] == attr_name }
+ # local secondary index
+ unless local_indices.empty?
+ req_hash['LocalSecondaryIndexes'] = []
- if same_attr
- if same_attr['AttributeType'] != attr_type
- raise DynamoDB::Error, "different types have been defined: #{attr_name}"
- end
- else
- attr_defs << {
- 'AttributeName' => attr_name,
- 'AttributeType' => attr_type,
- }
+ local_indices.each do |idx_def|
+ local_secondary_index = define_index(idx_def, req_hash['AttributeDefinitions'], :global => false, :hash_name => parsed.hash[:name])
+ req_hash['LocalSecondaryIndexes'] << local_secondary_index
end
end
- define_index = lambda do |idx_def, def_idx_opts|
- global_idx = def_idx_opts[:global]
+ # global secondary index
+ unless global_indices.empty?
+ req_hash['GlobalSecondaryIndexes'] = []
-
- if global_idx
- idx_def[:keys].each do |key_type, name_type|
- define_attribute.call(name_type[:key], name_type[:type])
- end
- else
- define_attribute.call(idx_def[:key], idx_def[:type])
+ global_indices.each do |idx_def|
+ global_secondary_index = define_index(idx_def, req_hash['AttributeDefinitions'], :global => true, :capacity => parsed.capacity)
+ req_hash['GlobalSecondaryIndexes'] << global_secondary_index
end
+ end
- secondary_index = {
- 'IndexName' => idx_def[:name],
- 'Projection' => {
- 'ProjectionType' => idx_def[:projection][:type],
- }
- }
+ @client.query('CreateTable', req_hash)
+ nil
+ end
- if global_idx
- secondary_index['KeySchema'] = []
+ def define_attribute(attr_name, attr_type, attr_defs)
+ same_attr = attr_defs.find {|i| i['AttributeName'] == attr_name }
- [:hash, :range].each do |key_type|
- name_type = idx_def[:keys][key_type]
-
- if name_type
- secondary_index['KeySchema'] << {
- 'AttributeName' => name_type[:key],
- 'KeyType' => key_type.to_s.upcase,
- }
- end
- end
- else
- secondary_index['KeySchema'] = [
- {
- 'AttributeName' => parsed.hash[:name],
- 'KeyType' => 'HASH',
- },
- {
- 'AttributeName' => idx_def[:key],
- 'KeyType' => 'RANGE',
- },
- ]
+ if same_attr
+ if same_attr['AttributeType'] != attr_type
+ raise DynamoDB::Error, "different types have been defined: #{attr_name}"
end
+ else
+ attr_defs << {
+ 'AttributeName' => attr_name,
+ 'AttributeType' => attr_type,
+ }
+ end
+ end
- if idx_def[:projection][:attrs]
- secondary_index['Projection']['NonKeyAttributes'] = idx_def[:projection][:attrs]
- end
+ def define_index(idx_def, attr_defs, def_idx_opts)
+ global_idx = def_idx_opts[:global]
- if global_idx
- capacity = idx_def[:capacity] || parsed.capacity
-
- secondary_index['ProvisionedThroughput'] = {
- 'ReadCapacityUnits' => capacity[:read],
- 'WriteCapacityUnits' => capacity[:write],
- }
+ if global_idx
+ idx_def[:keys].each do |key_type, name_type|
+ define_attribute(name_type[:key], name_type[:type], attr_defs)
end
+ else
+ define_attribute(idx_def[:key], idx_def[:type], attr_defs)
+ end
- secondary_index
- end # define_index
+ secondary_index = {
+ 'IndexName' => idx_def[:name],
+ 'Projection' => {
+ 'ProjectionType' => idx_def[:projection][:type],
+ }
+ }
- # local secondary index
- unless local_indices.empty?
- req_hash['LocalSecondaryIndexes'] = []
+ if global_idx
+ secondary_index['KeySchema'] = []
- local_indices.each do |idx_def|
- local_secondary_index = define_index.call(idx_def, :global => false)
- req_hash['LocalSecondaryIndexes'] << local_secondary_index
+ [:hash, :range].each do |key_type|
+ name_type = idx_def[:keys][key_type]
+
+ if name_type
+ secondary_index['KeySchema'] << {
+ 'AttributeName' => name_type[:key],
+ 'KeyType' => key_type.to_s.upcase,
+ }
+ end
end
+ else
+ secondary_index['KeySchema'] = [
+ {
+ 'AttributeName' => def_idx_opts.fetch(:hash_name),
+ 'KeyType' => 'HASH',
+ },
+ {
+ 'AttributeName' => idx_def[:key],
+ 'KeyType' => 'RANGE',
+ },
+ ]
end
- # global secondary index
- unless global_indices.empty?
- req_hash['GlobalSecondaryIndexes'] = []
+ if idx_def[:projection][:attrs]
+ secondary_index['Projection']['NonKeyAttributes'] = idx_def[:projection][:attrs]
+ end
- global_indices.each do |idx_def|
- global_secondary_index = define_index.call(idx_def, :global => true)
- req_hash['GlobalSecondaryIndexes'] << global_secondary_index
- end
+ if global_idx
+ capacity = idx_def[:capacity] || def_idx_opts.fetch(:capacity)
+
+ secondary_index['ProvisionedThroughput'] = {
+ 'ReadCapacityUnits' => capacity[:read],
+ 'WriteCapacityUnits' => capacity[:write],
+ }
end
- @client.query('CreateTable', req_hash)
- nil
+ secondary_index
end
def do_create_like(parsed)
table_info = @client.query('DescribeTable', 'TableName' => parsed.like)['Table']