Class: Dynomite::Migration::Dsl

Inherits:
Object
  • Object
show all
Extended by:
Accessor
Includes:
Client, Index, PrimaryKey, ProvisionedThroughput, Helpers
Defined in:
lib/dynomite/migration/dsl/primary_key.rb,
lib/dynomite/migration/dsl.rb,
lib/dynomite/migration/dsl/index.rb,
lib/dynomite/migration/dsl/accessor.rb,
lib/dynomite/migration/dsl/provisioned_throughput.rb

Overview

Common methods to the *SecondaryIndex classes that handle gsi and lsi methods as well a the Dsl class that handles create_table and update_table methods.

Defined Under Namespace

Modules: Accessor, Index, PrimaryKey, ProvisionedThroughput

Constant Summary

Constants included from Types

Types::TYPE_MAP

Instance Attribute Summary collapse

Attributes included from PrimaryKey

#partition_key_identifier, #sort_key_identifier

Instance Method Summary collapse

Methods included from Accessor

define_dsl_accessor, dsl_accessor

Methods included from Helpers

#table_name_with_namespace

Methods included from Index

#add_gsi, #add_lsi, #global_secondary_index_updates, #gsi_secondary_index_creates, #lsi_secondary_index_creates, #normalize_index_attrs, #remove_gsi, #update_gsi

Methods included from PrimaryKey

#adjust_schema_and_attributes, #partition_key, #sort_key

Methods included from Types

#type_map

Methods included from ProvisionedThroughput

#billing_mode, #provisioned_throughput

Constructor Details

#initialize(method_name, table_name, &block) ⇒ Dsl

Returns a new instance of Dsl.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dynomite/migration/dsl.rb', line 13

def initialize(method_name, table_name, &block)
  @method_name = method_name
  @table_name = table_name
  @block = block

  # Dsl fills in atttributes in as methods are called within the block
  # when parition_key and sort_key are called.
  # Attributes for both create_table and updated_table:
  @attribute_definitions = []
  # dont set billing_mode for update_table. otherwise creating an index can set billing_mode to PAY_PER_REQUEST without user knowing
  @billing_mode = "PAY_PER_REQUEST" if @method_name == :create_table
  @provisioned_throughput = nil

  # Attributes for create_table only:
  @key_schema = []

  # Attributes for update_table only:
  @gsi_indexes = []
  @lsi_indexes = []
end

Instance Attribute Details

#attribute_definitionsObject

We’ll also added the attributes from: 1. lsi index 2. gsi indexes 3. existing attributes for update table



109
110
111
# File 'lib/dynomite/migration/dsl.rb', line 109

def attribute_definitions
  @attribute_definitions
end

#key_schemaObject

Returns the value of attribute key_schema.



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

def key_schema
  @key_schema
end

#table_nameObject

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#common_paramsObject

common to create_table and update_table



141
142
143
144
145
146
147
148
# File 'lib/dynomite/migration/dsl.rb', line 141

def common_params
  params = {
    stream_specification: @stream_specification,
    sse_specification: @sse_specification,
    deletion_protection_enabled: deletion_protection_enabled_value,
  }
  params.reject! { |k,v| v.blank? }
end

#deletion_protection_enabled(value = true) ⇒ Object Also known as: deletion_protection



150
151
152
# File 'lib/dynomite/migration/dsl.rb', line 150

def deletion_protection_enabled(value=true)
  @deletion_protection_enabled = value
end

#deletion_protection_enabled_valueObject

To avoid name conflict with DSL t.deletion_protection_enabled



156
157
158
159
# File 'lib/dynomite/migration/dsl.rb', line 156

def deletion_protection_enabled_value
  default = Dynomite.config.migration.deletion_protection_enabled
  @deletion_protection_enabled.nil? ? default : @deletion_protection_enabled
end

#evaluateObject



38
39
40
41
42
# File 'lib/dynomite/migration/dsl.rb', line 38

def evaluate
  return if @evaluated
  @block.call(self) if @block
  @evaluated = true
end

#namespaced_table_nameObject



34
35
36
# File 'lib/dynomite/migration/dsl.rb', line 34

def namespaced_table_name
  table_name_with_namespace(table_name)
end

#paramsObject

executor



47
48
49
50
# File 'lib/dynomite/migration/dsl.rb', line 47

def params
  evaluate # lazy evaluation: wait until as long as possible before evaluating code block
  send "params_#{@method_name}" # IE: params_create_table, params_update_table, params_delete_table
end

#params_create_tableObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dynomite/migration/dsl.rb', line 53

def params_create_table
  params = {
    billing_mode: @billing_mode.to_s.upcase,
    table_name: namespaced_table_name,
    key_schema: @key_schema,
    provisioned_throughput: @provisioned_throughput,
    tags: @tags, # only for create_table
  }
  params.reject! { |k,v| v.blank? }

  params[:local_secondary_indexes] = lsi_secondary_index_creates unless @lsi_indexes.empty?
  params[:global_secondary_indexes] = gsi_secondary_index_creates unless @gsi_indexes.empty?
  params[:attribute_definitions] = attribute_definitions
  params.merge!(common_params)
  params
end

#params_delete_tableObject



89
90
91
# File 'lib/dynomite/migration/dsl.rb', line 89

def params_delete_table
  { table_name: namespaced_table_name }
end

#params_update_tableObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dynomite/migration/dsl.rb', line 72

def params_update_table
  params = {
    billing_mode: @billing_mode.to_s.upcase,
    table_name: namespaced_table_name,
    # update table take values only some values for the "parent" table
    # no key_schema, update_table does not handle key_schema for the "parent" table
    replica_updates: @replica_updates, # only for update_table
  }
  params.reject! { |k,v| v.blank? }

  # only set "parent" table provisioned_throughput if user actually invoked it in the dsl
  params[:provisioned_throughput] = @provisioned_throughput if @provisioned_throughput_set_called
  params[:global_secondary_index_updates] = global_secondary_index_updates unless @gsi_indexes.empty?
  params[:attribute_definitions] = attribute_definitions
  params
end

#tags(values = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dynomite/migration/dsl.rb', line 93

def tags(values=nil)
  if values.nil?
    @tags
  else
    if values.is_a?(Hash)
      @tags = values.map do |key,value|
        {key: key.to_s, value: value.to_s}
      end
    else
      @tags = values
    end
  end
end