Class: Mara::Table
- Inherits:
-
Object
- Object
- Mara::Table
- 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.
Constant Summary collapse
- SUPPORTED_ENVS =
Default supported environments
%w[development test].freeze
Class Method Summary collapse
-
.prepare!(table_params) ⇒ true, false
Create a new table if it doesn’t exist.
-
.prepare_table!(table_params, envs, wait) ⇒ true, false
Prepare the table with extra options.
-
.table_exists?(table_name) ⇒ true, false
Check if a table exists.
-
.teardown!(table_params = {}) ⇒ true, false
Teardown the table if it exists.
-
.teardown_table!(table_params, envs, wait) ⇒ true, false
Teardown the table with extra options.
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.
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.
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.
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.
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.
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 |