Module: Dynomite::Migration::Dsl::PrimaryKey

Includes:
Types
Included in:
Dynomite::Migration::Dsl
Defined in:
lib/dynomite/migration/dsl/primary_key.rb

Constant Summary

Constants included from Types

Types::TYPE_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types

#type_map

Instance Attribute Details

#partition_key_identifierObject (readonly)



9
10
11
# File 'lib/dynomite/migration/dsl/primary_key.rb', line 9

def partition_key_identifier
  @partition_key_identifier
end

#sort_key_identifierObject (readonly)

sort_key is optional



17
18
19
# File 'lib/dynomite/migration/dsl/primary_key.rb', line 17

def sort_key_identifier
  @sort_key_identifier
end

Instance Method Details

#adjust_schema_and_attributes(identifier, key_type) ⇒ Object

Parameters:

identifier: "id:string" or "id"
key_type: "HASH" OR "RANGE"

Adjusts the parameters for create_table to add the partition_key and sort_key



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynomite/migration/dsl/primary_key.rb', line 37

def adjust_schema_and_attributes(identifier, key_type)
  name, attribute_type = identifier.split(':')
  attribute_type ||= "string" # default to string

  partition_key = {
    attribute_name: name,
    key_type: key_type.upcase
  }

  if partition_key[:key_type] == "RANGE"
    @key_schema << partition_key unless @key_schema.include?(partition_key)
  else # HASH - add to beginning
    @key_schema.unshift(partition_key) unless @key_schema.include?(partition_key)
  end

  attribute_definition = {
    attribute_name: name,
    attribute_type: type_map(attribute_type)
  }
  unless @attribute_definitions.include?(attribute_definition)
    @attribute_definitions << attribute_definition
  end
  @attribute_definitions
end

#partition_key(identifier) ⇒ Object



10
11
12
13
14
# File 'lib/dynomite/migration/dsl/primary_key.rb', line 10

def partition_key(identifier)
  identifier = identifier.to_s
  @partition_key_identifier = identifier # for later use. useful for conventional_index_name
  adjust_schema_and_attributes(identifier, "HASH")
end

#sort_key(identifier) ⇒ Object



18
19
20
21
22
# File 'lib/dynomite/migration/dsl/primary_key.rb', line 18

def sort_key(identifier)
  identifier = identifier.to_s
  @sort_key_identifier = identifier # for later use. useful for conventional_index_name
  adjust_schema_and_attributes(identifier, "RANGE")
end