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.



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

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:



11
12
13
# File 'lib/aws-record/record/table_migration.rb', line 11

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):

  • :billing_mode (Hash)

    Accepts values ‘PAY_PER_REQUEST’ or ‘PROVISIONED’. If :provisioned_throughput option is specified, this option is not required, as ‘PROVISIONED’ is assumed. If :provisioned_throughput is not specified, this option is required and must be set to ‘PAY_PER_REQUEST’.

  • :provisioned_throughput (Hash)

    Unless :billing_mode is set to ‘PAY_PER_REQUEST’, 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, unless :billing_mode is set to ‘PAY_PER_REQUEST’. 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.



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

def create!(opts)
  gsit = opts.delete(:global_secondary_index_throughput)
  _validate_billing(opts)

  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 || opts[:billing_mode] == 'PAY_PER_REQUEST'
      raise ArgumentError.new(
        'If you define global secondary indexes, you must also define'\
          ' :global_secondary_index_throughput on table creation,'\
          " unless :billing_mode is set to 'PAY_PER_REQUEST'."
      )
    end
    gsis_opts = if opts[:billing_mode] == 'PAY_PER_REQUEST'
                  gsis
                else
                  _add_throughput_to_gsis(gsis, gsit)
                end
    create_opts[:global_secondary_indexes] = gsis_opts
    _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:



120
121
122
123
124
125
126
# File 'lib/aws-record/record/table_migration.rb', line 120

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:



103
104
105
106
107
108
109
110
111
112
# File 'lib/aws-record/record/table_migration.rb', line 103

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.



132
133
134
# File 'lib/aws-record/record/table_migration.rb', line 132

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