Class: Mara::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/mara/table.rb

Overview

Manage Dev/Test tables.

While this can be used to create real tables, we don’t recommend it.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Constant Summary collapse

SUPPORTED_ENVS =

Default supported environments

Since:

  • 1.0.0

%w[development test].freeze

Class Method Summary collapse

Class Method Details

.prepare!(table_params) ⇒ true, false

Note:

If the table_params do not include the table name, The default table name from the config will be used.

Create a new table if it doesn’t exist.

Parameters:

  • table_params (Hash)

    DynamoDB create table params hash.

Returns:

  • (true, false)

Since:

  • 1.0.0



29
30
31
# File 'lib/mara/table.rb', line 29

def prepare!(table_params)
  prepare_table!(table_params, SUPPORTED_ENVS, true)
end

.prepare_table!(table_params, envs, wait) ⇒ true, false

Prepare the table with extra options.

Parameters:

  • table_params (Hash)

    DynamoDB create table params hash.

  • envs (Array<String>)

    The environments that are allowed to check if this action is allowed.

  • wait (true, false)

    Should this function wait for the table to become fully available before returning.

Returns:

  • (true, false)

Since:

  • 1.0.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mara/table.rb', line 68

def prepare_table!(table_params, envs, wait)
  env =  Mara.config.env
  unless Array(envs).include?(env)
    raise ArgumentError, "Can't prepare table outside of #{envs.join('/')}"
  end

  table_name = table_params.fetch(:table_name,  Mara.config.dynamodb.table_name)

  if table_exists?(table_name)
    return true
  end

  table_params = normalize_table_params(table_params, table_name)

  log(" Mara create_table(\"#{table_name}\")")

   Mara::Client.shared.create_table(table_params)
   Mara::Client.shared.wait_until(:table_exists, table_name: table_name) if wait

  true
end

.table_exists?(table_name) ⇒ true, false

Check if a table exists.

Parameters:

  • table_name (String)

    the name of the table to check if it exists.

Returns:

  • (true, false)

Since:

  • 1.0.0



52
53
54
# File 'lib/mara/table.rb', line 52

def table_exists?(table_name)
   Mara::Client.shared.list_tables.table_names.include?(table_name)
end

.teardown!(table_params = {}) ⇒ true, false

Note:

If the table_params do not include the table name, The default table name from the config will be used.

Teardown the table if it exists.

Parameters:

  • table_params (Hash) (defaults to: {})

    DynamoDB table name params.

Returns:

  • (true, false)

Since:

  • 1.0.0



42
43
44
# File 'lib/mara/table.rb', line 42

def teardown!(table_params = {})
  teardown_table!(table_params, SUPPORTED_ENVS, true)
end

.teardown_table!(table_params, envs, wait) ⇒ true, false

Teardown the table with extra options.

Parameters:

  • table_params (Hash)

    DynamoDB create table params hash.

  • envs (Array<String>)

    The environments that are allowed to check if this action is allowed.

  • wait (true, false)

    Should this function wait for the table to become fully available before returning.

Returns:

  • (true, false)

Since:

  • 1.0.0



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mara/table.rb', line 102

def teardown_table!(table_params, envs, wait)
  env =  Mara.config.env
  unless envs.include?(env)
    raise ArgumentError, "Can't prepare table outside of #{envs.join('/')}"
  end

  table_name = table_params.fetch(:table_name,  Mara.config.dynamodb.table_name)

  unless  Mara::Client.shared.list_tables.table_names.include?(table_name)
    return true
  end

  log(" Mara destroy_table(\"#{table_name}\")")

   Mara::Client.shared.delete_table(table_name: table_name)
   Mara::Client.shared.wait_until(:table_not_exists, table_name: table_name) if wait

  true
end