Class: Aws::Record::TableMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-record/record/table_migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = {}) ⇒ TableMigration

Returns a new instance of TableMigration.

Parameters:

Options Hash (opts):

  • :client (Aws::DynamoDB::Client)

    Allows you to inject your own Aws::DynamoDB::Client class. If this option is not included, a client will be constructed for you with default parameters.



31
32
33
34
35
# File 'lib/aws-record/record/table_migration.rb', line 31

def initialize(model, opts = {})
  _assert_model_valid(model)
  @model = model
  @client = opts[:client] || model.dynamodb_client || Aws::DynamoDB::Client.new
end

Instance Attribute Details

#clientAws::DynamoDB::Client

Returns the Aws::DynamoDB::Client class used by this table migration instance.

Returns:



22
23
24
# File 'lib/aws-record/record/table_migration.rb', line 22

def client
  @client
end

Instance Method Details

#create!(opts) ⇒ Object

This method calls Aws::DynamoDB::Client#create_table, populating the attribute definitions and key schema based on your model class, as well as passing through other parameters as provided by you.

Examples:

Creating a table with a global secondary index named :gsi

migration.create!(
  provisioned_throughput: {
    read_capacity_units: 5,
    write_capacity_units: 2
  },
  global_secondary_index_throughput: {
    gsi: {
      read_capacity_units: 3,
      write_capacity_units: 1
    }
  }
)

Parameters:

  • opts (Hash)

    options to pass on to the client call to #create_table. See the documentation above in the AWS SDK for Ruby V2.

Options Hash (opts):

  • :provisioned_throughput (Hash)

    This is a required argument, in which you must specify the :read_capacity_units and :write_capacity_units of your new table.

  • :global_secondary_index_throughput (Hash)

    This argument is required if you define any global secondary indexes. It should map your global secondary index names to their provisioned throughput, similar to how you define the provisioned throughput for the table in general.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aws-record/record/table_migration.rb', line 66

def create!(opts)
  gsit = opts.delete(:global_secondary_index_throughput)
  create_opts = opts.merge({
    table_name: @model.table_name,
    attribute_definitions: _attribute_definitions,
    key_schema: _key_schema
  })
  if lsis = @model.local_secondary_indexes_for_migration
    create_opts[:local_secondary_indexes] = lsis
    _append_to_attribute_definitions(lsis, create_opts)
  end
  if gsis = @model.global_secondary_indexes_for_migration
    unless gsit
      raise ArgumentError.new(
        "If you define global secondary indexes, you must also define"\
          " :global_secondary_index_throughput on table creation."
      )
    end
    gsis_with_throughput = _add_throughout_to_gsis(gsis, gsit)
    create_opts[:global_secondary_indexes] = gsis_with_throughput
    _append_to_attribute_definitions(gsis, create_opts)
  end
  @client.create_table(create_opts)
end

#delete!Object

This method calls Aws::DynamoDB::Client#delete_table using the table name of your model.

Raises:



117
118
119
120
121
122
123
# File 'lib/aws-record/record/table_migration.rb', line 117

def delete!
  begin
    @client.delete_table(table_name: @model.table_name)
  rescue DynamoDB::Errors::ResourceNotFoundException => e
    raise Errors::TableDoesNotExist.new(e)
  end
end

#update!(opts) ⇒ Object

This method calls Aws::DynamoDB::Client#update_table using the parameters that you provide.

Parameters:

  • opts (Hash)

    options to pass on to the client call to #update_table. See the documentation above in the AWS SDK for Ruby V2.

Raises:



100
101
102
103
104
105
106
107
108
109
# File 'lib/aws-record/record/table_migration.rb', line 100

def update!(opts)
  begin
    update_opts = opts.merge({
      table_name: @model.table_name
    })
    @client.update_table(update_opts)
  rescue DynamoDB::Errors::ResourceNotFoundException => e
    raise Errors::TableDoesNotExist.new(e)
  end
end

#wait_until_availableObject

This method waits on the table specified in the model to exist and be marked as ACTIVE in Amazon DynamoDB. Note that this method can run for several minutes if the table does not exist, and is not created within the wait period.



129
130
131
# File 'lib/aws-record/record/table_migration.rb', line 129

def wait_until_available
  @client.wait_until(:table_exists, table_name: @model.table_name)
end