Module: OceanDynamo::Tables::ClassMethods
- Defined in:
- lib/ocean-dynamo/tables.rb
Overview
Class methods
Instance Method Summary collapse
- #attribute_type(name) ⇒ Object
- #create_table ⇒ Object
- #delete_table ⇒ Object
- #dynamo_schema(table_hash_key = :id, table_range_key = nil, table_name: compute_table_name, table_name_prefix: nil, table_name_suffix: nil, read_capacity_units: 10, write_capacity_units: 5, connect: :late, create: false, **keywords, &block) ⇒ Object
- #establish_db_connection ⇒ Object
- #setup_dynamo ⇒ Object
- #table_attribute_definitions ⇒ Object
- #table_exists?(table) ⇒ Boolean
- #table_key_schema ⇒ Object
- #update_table_if_required ⇒ Object
- #wait_until_table_is_active ⇒ Object
Instance Method Details
#attribute_type(name) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ocean-dynamo/tables.rb', line 144 def attribute_type(name) vals = fields[name][:type] case vals when :string, :serialized, :reference return "S" when :integer, :float, :datetime return "N" when :boolean return "B" else raise "Unknown OceanDynamo type: #{name} - #{vals.inspect}" end end |
#create_table ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ocean-dynamo/tables.rb', line 97 def create_table attrs = table_attribute_definitions keys = table_key_schema = { table_name: table_full_name, provisioned_throughput: { read_capacity_units: table_read_capacity_units, write_capacity_units: table_write_capacity_units }, attribute_definitions: attrs, key_schema: keys } dynamo_resource.create_table() sleep 1 until dynamo_table.table_status == "ACTIVE" setup_dynamo true end |
#delete_table ⇒ Object
159 160 161 162 163 |
# File 'lib/ocean-dynamo/tables.rb', line 159 def delete_table return false unless dynamo_table.data_loaded? && dynamo_table.table_status == "ACTIVE" dynamo_table.delete true end |
#dynamo_schema(table_hash_key = :id, table_range_key = nil, table_name: compute_table_name, table_name_prefix: nil, table_name_suffix: nil, read_capacity_units: 10, write_capacity_units: 5, connect: :late, create: false, **keywords, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ocean-dynamo/tables.rb', line 17 def dynamo_schema(table_hash_key=:id, table_range_key=nil, table_name: compute_table_name, table_name_prefix: nil, table_name_suffix: nil, read_capacity_units: 10, write_capacity_units: 5, connect: :late, create: false, **keywords, &block) self.dynamo_client = nil self.dynamo_resource = nil self.dynamo_table = nil self.table_connected = false self.table_connect_policy = connect self.table_create_policy = create self.table_hash_key = table_hash_key self.table_range_key = table_range_key self.table_name = table_name self.table_name_prefix = table_name_prefix self.table_name_suffix = table_name_suffix self.table_read_capacity_units = read_capacity_units self.table_write_capacity_units = write_capacity_units # Connect if asked to establish_db_connection if connect == true end |
#establish_db_connection ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ocean-dynamo/tables.rb', line 46 def establish_db_connection setup_dynamo if table_exists?(dynamo_table) wait_until_table_is_active self.table_connected = true update_table_if_required else raise(TableNotFound, table_full_name) unless table_create_policy create_table end end |
#setup_dynamo ⇒ Object
59 60 61 62 63 |
# File 'lib/ocean-dynamo/tables.rb', line 59 def setup_dynamo self.dynamo_client ||= Aws::DynamoDB::Client.new self.dynamo_resource ||= Aws::DynamoDB::Resource.new(client: dynamo_client) self.dynamo_table = dynamo_resource.table(table_full_name) end |
#table_attribute_definitions ⇒ Object
129 130 131 132 133 134 |
# File 'lib/ocean-dynamo/tables.rb', line 129 def table_attribute_definitions attrs = [] attrs << { attribute_name: table_hash_key.to_s, attribute_type: attribute_type(table_hash_key) } attrs << { attribute_name: table_range_key.to_s, attribute_type: attribute_type(table_range_key) } if table_range_key attrs end |
#table_exists?(table) ⇒ Boolean
66 67 68 69 70 71 72 73 74 |
# File 'lib/ocean-dynamo/tables.rb', line 66 def table_exists?(table) return true if table.data_loaded? begin table.load rescue Aws::DynamoDB::Errors::ResourceNotFoundException return false end true end |
#table_key_schema ⇒ Object
136 137 138 139 140 141 |
# File 'lib/ocean-dynamo/tables.rb', line 136 def table_key_schema keys = [] keys << { attribute_name: table_hash_key.to_s, key_type: "HASH" } keys << { attribute_name: table_range_key.to_s, key_type: "RANGE" } if table_range_key keys end |
#update_table_if_required ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ocean-dynamo/tables.rb', line 116 def update_table_if_required attrs = table_attribute_definitions active_attrs = [] dynamo_table.attribute_definitions.each do |k| active_attrs << { attribute_name: k.attribute_name, attribute_type: k.attribute_type } end return false if active_attrs == attrs = { attribute_definitions: attrs } dynamo_table.update() true end |
#wait_until_table_is_active ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ocean-dynamo/tables.rb', line 77 def wait_until_table_is_active loop do case dynamo_table.table_status when "ACTIVE" update_table_if_required return when "UPDATING", "CREATING" sleep 1 next when "DELETING" sleep 1 while table_exists?(dynamo_table) create_table return else raise UnknownTableStatus.new("Unknown DynamoDB table status '#{dynamo_table.table_status}'") end end end |