Class: Aws::Record::TableConfig
- Inherits:
-
Object
- Object
- Aws::Record::TableConfig
- Defined in:
- lib/aws-record/record/table_config.rb
Overview
Aws::Record::TableConfig
provides a DSL for describing and modifying the remote configuration of your DynamoDB tables. A table configuration object can perform intelligent comparisons and incremental migrations versus the current remote configuration, if the table exists, or do a full create if it does not. In this manner, table configuration becomes fully declarative.
Defined Under Namespace
Classes: GlobalSecondaryIndex
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
-
.define(&block) ⇒ Object
Creates a new table configuration, using a DSL in the provided block.
Instance Method Summary collapse
- #client_options(opts) ⇒ Object private
-
#compatible? ⇒ Boolean
Checks the remote table for compatibility.
- #configure_client ⇒ Object private
-
#exact_match? ⇒ Boolean
Checks against the remote table’s configuration.
- #global_secondary_index(name, &block) ⇒ Object private
-
#initialize ⇒ TableConfig
constructor
private
A new instance of TableConfig.
-
#migrate! ⇒ Object
Performs a migration, if needed, against the remote table.
- #model_class(model) ⇒ Object private
- #read_capacity_units(units) ⇒ Object private
- #write_capacity_units(units) ⇒ Object private
Constructor Details
#initialize ⇒ TableConfig
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of TableConfig.
136 137 138 139 |
# File 'lib/aws-record/record/table_config.rb', line 136 def initialize @client_options = {} @global_secondary_indexes = {} end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
79 80 81 |
# File 'lib/aws-record/record/table_config.rb', line 79 def client @client end |
Class Method Details
.define(&block) ⇒ Object
Creates a new table configuration, using a DSL in the provided block. The DSL has the following methods:
-
#model_class
A class name reference to theAws::Record
model class. -
#read_capacity_units
Sets the read capacity units for the table. -
#write_capacity_units
Sets the write capacity units for the table. -
#global_secondary_index(index_symbol, &block) Defines a global secondary index with capacity attributes in a block:
-
#read_capacity_units
Sets the read capacity units for the index. -
#write_capacity_units
Sets the write capacity units for the index.
-
127 128 129 130 131 132 |
# File 'lib/aws-record/record/table_config.rb', line 127 def define(&block) cfg = TableConfig.new cfg.instance_eval(&block) cfg.configure_client cfg end |
Instance Method Details
#client_options(opts) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
164 165 166 |
# File 'lib/aws-record/record/table_config.rb', line 164 def (opts) @client_options = opts end |
#compatible? ⇒ Boolean
Checks the remote table for compatibility. Similar to #exact_match?
, this will return false
if the remote table does not exist. It also checks the keys, declared global secondary indexes, declared attribute definitions, and throughput for exact matches. However, if the remote end has additional attribute definitions and global secondary indexes not defined in your config, will still return true
. This allows for a check that is friendly to single table inheritance use cases.
224 225 226 227 228 229 230 231 |
# File 'lib/aws-record/record/table_config.rb', line 224 def compatible? begin resp = @client.describe_table(table_name: @model_class.table_name) _compatible_check(resp) rescue DynamoDB::Errors::ResourceNotFoundException false end end |
#configure_client ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
169 170 171 |
# File 'lib/aws-record/record/table_config.rb', line 169 def configure_client @client = Aws::DynamoDB::Client.new(@client_options) end |
#exact_match? ⇒ Boolean
Checks against the remote table’s configuration. If the remote table does not exist, guaranteed false
. Otherwise, will check if the remote throughput, keys, attribute definitions, and global secondary indexes are exactly equal to your declared configuration.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/aws-record/record/table_config.rb', line 239 def exact_match? begin resp = @client.describe_table(table_name: @model_class.table_name) _throughput_equal(resp) && _keys_equal(resp) && _ad_equal(resp) && _gsi_equal(resp) rescue DynamoDB::Errors::ResourceNotFoundException false end end |
#global_secondary_index(name, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 160 161 |
# File 'lib/aws-record/record/table_config.rb', line 157 def global_secondary_index(name, &block) gsi = GlobalSecondaryIndex.new gsi.instance_eval(&block) @global_secondary_indexes[name] = gsi end |
#migrate! ⇒ Object
Performs a migration, if needed, against the remote table. If #compatible?
would return true, the remote table already has the same throughput, key schema, attribute definitions, and global secondary indexes, so no further API calls are made. Otherwise, a DynamoDB table will be created or updated to match your declared configuration.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/aws-record/record/table_config.rb', line 178 def migrate! _validate_required_configuration begin resp = @client.describe_table(table_name: @model_class.table_name) if _compatible_check(resp) nil else # Gotcha: You need separate migrations for indexes and throughput unless _throughput_equal(resp) @client.update_table( table_name: @model_class.table_name, provisioned_throughput: { read_capacity_units: @read_capacity_units, write_capacity_units: @write_capacity_units } ) @client.wait_until( :table_exists, table_name: @model_class.table_name ) end unless _gsi_superset(resp) @client.update_table(_update_index_opts(resp)) @client.wait_until( :table_exists, table_name: @model_class.table_name ) end end rescue DynamoDB::Errors::ResourceNotFoundException # Code Smell: Exception as control flow. # Can I use SDK ability to skip raising an exception for this? @client.create_table(_create_table_opts) @client.wait_until(:table_exists, table_name: @model_class.table_name) end end |
#model_class(model) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
142 143 144 |
# File 'lib/aws-record/record/table_config.rb', line 142 def model_class(model) @model_class = model end |
#read_capacity_units(units) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
147 148 149 |
# File 'lib/aws-record/record/table_config.rb', line 147 def read_capacity_units(units) @read_capacity_units = units end |
#write_capacity_units(units) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
152 153 154 |
# File 'lib/aws-record/record/table_config.rb', line 152 def write_capacity_units(units) @write_capacity_units = units end |