Class: DynamoDbFramework::TableManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_framework/dynamodb_table_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ TableManager

Returns a new instance of TableManager.



6
7
8
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 6

def initialize(store)
  @dynamodb = store
end

Instance Attribute Details

#dynamodbObject (readonly)

Returns the value of attribute dynamodb.



4
5
6
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 4

def dynamodb
  @dynamodb
end

Instance Method Details

#add_index(table_name, attributes, global_index, billing_mode = 'PROVISIONED') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 69

def add_index(table_name, attributes, global_index, billing_mode = 'PROVISIONED')

  attribute_definitions = []

  attributes.each do |a|
    attribute_definitions.push({ :attribute_name => a[:name], :attribute_type => a[:type] })
  end

  table = {
      :table_name => table_name,
      :attribute_definitions => attribute_definitions,
      :global_secondary_index_updates => [
          :create => global_index
      ],
      :billing_mode => billing_mode
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  DynamoDbFramework.logger.info "[#{self.class}] -Adding global index: #{global_index[:index_name]}."
  wait_until_index_active(table_name, global_index[:index_name])
  DynamoDbFramework.logger.info "[#{self.class}] -Index added."
end

#create(table_name, attributes, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, global_indexes = nil, billing_mode = 'PROVISIONED') ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 284

def create(table_name, attributes, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, global_indexes = nil, billing_mode = 'PROVISIONED')

  if exists?(table_name)
    return
  end

  attribute_definitions = []

  attributes.each do |a|
    attribute_definitions.push({ :attribute_name => a[:name], :attribute_type => a[:type] })
  end

  key_schema = []
  key_schema.push({ :attribute_name => partition_key, :key_type => :HASH })
  if range_key != nil
    key_schema.push({ :attribute_name => range_key, :key_type => :RANGE })
  end

  table = {
      :table_name => table_name,
      :attribute_definitions => attribute_definitions,
      :key_schema => key_schema,
      :billing_mode => billing_mode
  }

  unless billing_mode == 'PAY_PER_REQUEST'
    table = table.merge(
      :provisioned_throughput => {
        :read_capacity_units => read_capacity,
        :write_capacity_units => write_capacity
      }
    )
  end

  if global_indexes != nil
    table[:global_secondary_indexes] = global_indexes
  end

  dynamodb.client.create_table(table)

  # wait for table to be created
  DynamoDbFramework.logger.info "[#{self.class}] -Waiting for table: [#{table_name}] to be created."
  dynamodb.client.wait_until(:table_exists, table_name: table_name)
  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] created."
end

#create_global_index(name, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, billing_mode = 'PROVISIONED') ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 399

def create_global_index(name, partition_key, range_key = nil, read_capacity = 20, write_capacity = 10, billing_mode = 'PROVISIONED')

  key_schema = []

  key_schema.push({ :attribute_name => partition_key, :key_type => :HASH })
  if range_key != nil
    key_schema.push({ :attribute_name => range_key, :key_type => :RANGE })
  end

  index = {
      :index_name => name,
      :key_schema => key_schema,
      :projection => {
          :projection_type => :ALL
      }
  }

  if billing_mode == 'PROVISIONED'
    index = index.merge(
      :provisioned_throughput => {
        :read_capacity_units => read_capacity,
        :write_capacity_units => write_capacity,
      }
    )
  end

  return index
end

#create_table(options = {}) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 330

def create_table(options = {})

  if options[:name] == nil
    raise 'A valid table name must be specified.'
  end

  table_name = options[:name]
  if options[:namespace] != nil
    table_name = "#{options[:namespace]}.#{options[:name]}"
  end

  if exists?(options[:name])
    return
  end

  options[:read_capacity] ||= 20
  options[:write_capacity] ||= 20

  if !options[:attributes].is_a?(Array)
    raise 'A valid :attributes array must be specified.'
  end

  attribute_definitions = options[:attributes].map { |a| { :attribute_name => a[:name], :attribute_type => a[:type] } }

  hash_key = options[:attributes].detect { |a| a[:key] == :hash }
  if hash_key == nil
    raise 'No Hash Key attribute has been specified.'
  end

  range_key = options[:attributes].detect { |a| a[:key] == :range }

  key_schema = []
  key_schema.push({ :attribute_name => hash_key[:name], :key_type => :HASH })
  if range_key != nil
    key_schema.push({ :attribute_name => range_key[:name], :key_type => :RANGE })
  end

  table = {
      :table_name => table_name,
      :attribute_definitions => attribute_definitions,
      :key_schema => key_schema,
      :billing_mode => options[:billing_mode] || 'PROVISIONED'
  }

  unless options[:billing_mode] == 'PAY_PER_REQUEST'
    table = table.merge(
      :provisioned_throughput => {
        :read_capacity_units => options[:read_capacity],
        :write_capacity_units => options[:write_capacity]
      }
    )
  end

  if options[:global_indexes] != nil
    table[:global_secondary_indexes] = options[:global_indexes]
  end

  begin
    dynamodb.client.create_table(table)
  rescue Aws::DynamoDB::Errors::ResourceInUseException => e
    DynamoDbFramework.logger.warn "[#{self.class}] - Table #{table_name} already exists!"
  end

  # wait for table to be created
  DynamoDbFramework.logger.info "[#{self.class}] - Waiting for table: [#{table_name}] to be created."
  dynamodb.client.wait_until(:table_exists, table_name: table_name)
  DynamoDbFramework.logger.info "[#{self.class}] - Table: [#{table_name}] created."
end

#drop(table_name) ⇒ Object



428
429
430
431
432
433
434
435
436
437
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 428

def drop(table_name)

  if !exists?(table_name)
    return
  end

  DynamoDbFramework.logger.info "[#{self.class}] -Dropping table: [#{table_name}]."
  dynamodb.client.delete_table({ table_name: table_name })
  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] dropped."
end

#drop_index(table_name, index_name) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 146

def drop_index(table_name, index_name)
  unless has_index?(table_name, index_name)
    return
  end

  table = {
      :table_name => table_name,
      :global_secondary_index_updates => [
          :delete => {
              :index_name => index_name
          }
      ]
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  DynamoDbFramework.logger.info "[#{self.class}] -Deleting global index: #{index_name}."
  wait_until_index_dropped(table_name, index_name)

  DynamoDbFramework.logger.info "[#{self.class}] -Index: [#{index_name}] dropped."
end

#drop_table(table_name) ⇒ Object



439
440
441
442
443
444
445
446
447
448
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 439

def drop_table(table_name)

  if !exists?(table_name)
    return
  end

  DynamoDbFramework.logger.info "[#{self.class}] -Dropping table: [#{table_name}]."
  dynamodb.client.delete_table({ table_name: table_name })
  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] dropped."
end

#exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 10

def exists?(table_name)

  exists = true

  begin
    dynamodb.client.describe_table(:table_name => table_name)
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    exists = false
  end

  return exists

end

#get_index_status(table_name, index_name) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 174

def get_index_status(table_name, index_name)
  result = dynamodb.client.describe_table(:table_name => table_name)

  if result.table[:global_secondary_indexes] == nil
    return nil
  end

  index = result.table[:global_secondary_indexes].select { |i| i[:index_name] == index_name }

  if index.length > 0
    return index[0][:index_status]
  end

  return nil
end

#get_status(table_name) ⇒ Object



169
170
171
172
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 169

def get_status(table_name)
  result = dynamodb.client.describe_table(:table_name => table_name)
  return result.table[:table_status]
end

#get_ttl_status(table_name) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 138

def get_ttl_status(table_name)
  table = {
    :table_name => table_name
  }

  dynamodb.client.describe_time_to_live(table)['time_to_live_description']
end

#has_index?(table_name, index_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 24

def has_index?(table_name, index_name)
  exists = true

  begin
    result = dynamodb.client.describe_table(:table_name => table_name)

    if result.table[:global_secondary_indexes] == nil
      return false
    end

    if result.table[:global_secondary_indexes].select { |i| i[:index_name] == index_name }.length > 0
      exists = true
    else
      exists = false
    end
  rescue Aws::DynamoDB::Errors::ResourceNotFoundException
    exists = false
  end

  return exists
end

#update_index_throughput(table_name, index_name, read_capacity, write_capacity) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 94

def update_index_throughput(table_name, index_name, read_capacity, write_capacity)
  table = {
      :table_name => table_name,
      :global_secondary_index_updates => [
          :update => {
              :index_name => index_name,
              :provisioned_throughput => {
                  :read_capacity_units => read_capacity,
                  :write_capacity_units => write_capacity
              }
          }
      ]
  }

  DynamoDbFramework.logger.info "[#{self.class}] -Updating throughput for global index: #{index_name}."

  dynamodb.client.update_table(table)

  # wait for table to be updated

  DynamoDbFramework.logger.info "[#{self.class}] -Waiting for table: [#{table_name}] to be updated."
  wait_until_active(table_name)
  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] updated."
end

#update_throughput(table_name, read_capacity, write_capacity) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 46

def update_throughput(table_name, read_capacity, write_capacity)

  if !exists?(table_name)
    raise "table: #{table_name}, does not exist."
  end

  table = {
      :table_name => table_name,
      :provisioned_throughput => {
          :read_capacity_units => read_capacity,
          :write_capacity_units => write_capacity
      }
  }

  dynamodb.client.update_table(table)

  # wait for table to be updated
  DynamoDbFramework.logger.info "[#{self.class}] -Waiting for table: [#{table_name}] to be updated."
  wait_until_active(table_name)
  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] updated."

end

#update_ttl_attribute(table_name, enabled, attribute_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 119

def update_ttl_attribute(table_name, enabled, attribute_name)
  table = {
      :table_name => table_name,
      :time_to_live_specification => {
        :enabled => enabled,
        :attribute_name => attribute_name
      }
  }

  DynamoDbFramework.logger.info "[#{self.class}] -Updating TTL Attribute: #{attribute_name}."
  dynamodb.client.update_time_to_live(table)

  # wait for table to be updated
  DynamoDbFramework.logger.info "[#{self.class}] -Waiting for table: [#{table_name}] to be updated."
  wait_until_ttl_changed(table_name)

  DynamoDbFramework.logger.info "[#{self.class}] -Table: [#{table_name}] updated."
end

#wait_timeoutObject



190
191
192
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 190

def wait_timeout
  Time.now + 900 #15 minutes
end

#wait_until_active(table_name) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 194

def wait_until_active(table_name)

  end_time = wait_timeout
  while Time.now < end_time do

    status = get_status(table_name)

    if status == 'ACTIVE'
      return
    end

    sleep(5)
  end

  raise "Timeout occurred while waiting for table: #{table_name}, to become active."

end

#wait_until_dropped(table_name) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 212

def wait_until_dropped(table_name)

  end_time = wait_timeout
  while Time.now < end_time do

    status = get_status(table_name)

    if status == nil
      return
    end

    sleep(5)
  end

  raise "Timeout occurred while waiting for table: #{table_name}, to be dropped."

end

#wait_until_index_active(table_name, index_name) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 230

def wait_until_index_active(table_name, index_name)

  end_time = wait_timeout
  while Time.now < end_time do

    status = get_index_status(table_name, index_name)

    if status == 'ACTIVE'
      return
    end

    sleep(5)
  end

  raise "Timeout occurred while waiting for table: #{table_name}, index: #{index_name}, to become active."

end

#wait_until_index_dropped(table_name, index_name) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 248

def wait_until_index_dropped(table_name, index_name)

  end_time = wait_timeout
  while Time.now < end_time do

    status = get_index_status(table_name, index_name)

    if status == nil
      return
    end

    sleep(5)
  end

  raise "Timeout occurred while waiting for table: #{table_name}, index: #{index_name}, to be dropped."

end

#wait_until_ttl_changed(table_name) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/dynamodb_framework/dynamodb_table_manager.rb', line 266

def wait_until_ttl_changed(table_name)

  end_time = wait_timeout
  while Time.now < end_time do

    status = get_ttl_status(table_name)['time_to_live_status']

    if status == 'ENABLED' || status == 'DISABLED'
      return
    end

    sleep(5)
  end

  raise "Timeout occurred while waiting for table: #{table_name}, to update TTL status."

end