Class: Dynomite::Migration::Dsl::Index::Gsi

Inherits:
Base
  • Object
show all
Defined in:
lib/dynomite/migration/dsl/index/gsi.rb

Constant Summary

Constants included from Types

Types::TYPE_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#conventional_index_name, #get_attribute_name, #get_attribute_type, #partition_key_attribute_name, #partition_key_attribute_type, #sort_key_attribute_name, #sort_key_attribute_type

Methods included from Types

#type_map

Constructor Details

#initialize(action, attrs) ⇒ Gsi

Returns a new instance of Gsi.



4
5
6
7
8
9
10
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 4

def initialize(action, attrs)
  @action = action
  @params = attrs.dup
  # Delete the special DSL keys. Keep the rest and pass through to AWS create_table or update_table
  @partition_key = @params.delete(:partition_key) || @params.delete(:hash_key) # required for create, optional for update (only index needed)
  @sort_key = @params.delete(:sort_key) || @params.delete(:range_key)          # optional for create
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 3

def action
  @action
end

#attrsObject (readonly)

Returns the value of attribute attrs.



3
4
5
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 3

def attrs
  @attrs
end

Instance Method Details

#attribute_definitionsObject



41
42
43
44
45
46
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 41

def attribute_definitions
  definitions = []
  definitions << {attribute_name: partition_key_attribute_name, attribute_type: partition_key_attribute_type} unless @partition_key.blank?
  definitions << {attribute_name: sort_key_attribute_name, attribute_type: sort_key_attribute_type} unless @sort_key.blank?
  definitions
end

#normalize_provisioned_throughput(value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 48

def normalize_provisioned_throughput(value)
  if value.is_a?(Hash)
    value
  else
    {
      read_capacity_units: value,
      write_capacity_units: value,
    }
  end
end

#paramsObject



12
13
14
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 12

def params
  send("params_#{@action}")
end

#params_createObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 17

def params_create
  @params[:key_schema] ||= []
  # HASH - add to beginning of array
  @params[:key_schema].unshift(attribute_name: partition_key_attribute_name, key_type: "HASH") unless @partition_key.blank?
  # RANGE - add to end of array
  @params[:key_schema] << {attribute_name: sort_key_attribute_name, key_type: "RANGE"} unless @sort_key.blank?
  @params[:index_name] ||= conventional_index_name
  @params[:projection] ||= {projection_type: "ALL"}
  @params[:provisioned_throughput] = normalize_provisioned_throughput(@params[:provisioned_throughput]) if @params[:provisioned_throughput]
  @params
end

#params_deleteObject



36
37
38
39
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 36

def params_delete
  @params[:index_name] ||= conventional_index_name
  @params
end

#params_updateObject



30
31
32
33
34
# File 'lib/dynomite/migration/dsl/index/gsi.rb', line 30

def params_update
  @params[:index_name] ||= conventional_index_name
  @params[:provisioned_throughput] = normalize_provisioned_throughput(@params[:provisioned_throughput]) if @params[:provisioned_throughput]
  @params
end