Module: ActiveStorage::AwsRecord::Tables

Defined in:
lib/active_storage/aws_record/tables.rb

Overview

Creates / deletes the gem’s single DynamoDB table. Intended for development and tests (gated by config.manage_table); production tables are managed by the application. The created table uses a String (partition, sort) key —i.e. Mode A — with on-demand (PAY_PER_REQUEST) billing and no GSI. Point the gem at an existing table (any key-attribute names; numeric range → Mode B) to integrate with an app that owns its schema.

Class Method Summary collapse

Class Method Details

.clientObject



12
13
14
# File 'lib/active_storage/aws_record/tables.rb', line 12

def client
  ActiveStorage::AwsRecord.dynamodb_client
end

.create!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_storage/aws_record/tables.rb', line 44

def create!
  client.create_table(
    table_name: table_name,
    attribute_definitions: [
      { attribute_name: partition_key, attribute_type: 'S' },
      { attribute_name: sort_key, attribute_type: 'S' },
    ],
    key_schema: [
      { attribute_name: partition_key, key_type: 'HASH' },
      { attribute_name: sort_key, key_type: 'RANGE' },
    ],
    billing_mode: 'PAY_PER_REQUEST'
  )
  client.wait_until(:table_exists, table_name: table_name)
rescue Aws::DynamoDB::Errors::ResourceInUseException
  nil
end

.delete!Object



62
63
64
65
66
67
# File 'lib/active_storage/aws_record/tables.rb', line 62

def delete!
  client.delete_table(table_name: table_name)
  wait_until_gone!
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  nil
end

.ensure!Object

Create the table if it does not already exist.



31
32
33
34
35
# File 'lib/active_storage/aws_record/tables.rb', line 31

def ensure!
  return if exist?

  create!
end

.exist?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/active_storage/aws_record/tables.rb', line 37

def exist?
  client.describe_table(table_name: table_name)
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

.partition_keyObject

Partition / sort key attribute names for a gem-created (Mode A) table. Production tables are app-managed and may use any names (auto-detected).



22
23
24
# File 'lib/active_storage/aws_record/tables.rb', line 22

def partition_key
  'pk'
end

.reset!Object

Drop and recreate (used by the test harness for a clean slate).



70
71
72
73
# File 'lib/active_storage/aws_record/tables.rb', line 70

def reset!
  delete!
  create!
end

.sort_keyObject



26
27
28
# File 'lib/active_storage/aws_record/tables.rb', line 26

def sort_key
  'sk'
end

.table_nameObject



16
17
18
# File 'lib/active_storage/aws_record/tables.rb', line 16

def table_name
  ActiveStorage::AwsRecord.config.table_name
end

.wait_until_gone!Object



75
76
77
78
79
# File 'lib/active_storage/aws_record/tables.rb', line 75

def wait_until_gone!
  client.wait_until(:table_not_exists, table_name: table_name)
rescue Aws::Waiters::Errors::WaiterFailed
  nil
end