Ecto.Adapters.DynamoDB.Migration (ecto_adapters_dynamodb v3.1.2) View Source

Implements Ecto migrations for create table and alter table.

The functions, add, remove and modify correspond to indexes on the DynamoDB table. Using add, the second parameter, field type (which corresponds with the DynamoDB attribute) must be specified. Use the third parameter to specify a primary key not already specified. For a HASH-only primary key, use primary_key: true as the third parameter. For a composite primary key (HASH and RANGE), in addition to the primary_key specification, set the third parameter on the range key attribute to range_key: true. There should be only one primary key (hash or composite) specified per table.

To specify index details, such as provisioned throughput, create_if_not_exists/drop_if_exists, billing_mode, and global and local indexes, use the options keyword in create table and alter table, please see the examples below for greater detail.

Please note that change may not work as expected on rollback. We recommend specifying up and down instead.

Example:

# Migration file 1:

  def change do
    create table(:post,
      primary_key: false,
      options: [
        global_indexes: [
          [index_name: "email_content",
           keys: [:email, :content],
           provisioned_throughput: [100, 100]] # [read_capacity, write_capacity]
          ],
        provisioned_throughput: [20,20]
      ]) do

      add :email,   :string, primary_key: true  # primary composite key
      add :title,   :string, range_key: true    # primary composite key
      add :content, :string
    end
  end


# Migration file 2:

  def up do
    create_if_not_exists table(:rabbit,
      primary_key: false,
      options: [
        billing_mode: :pay_per_request,
        global_indexes: [
          [index_name: "name",
            keys: [:name]]
        ]
      ]) do

      add :id, :string, primary_key: true
      add :name, :string, hash_key: true
    end
  end

  def down do
    drop_if_exists table(:rabbit)
  end


# Migration file 3:

  def up do
    alter table(:post,
      options: [
        global_indexes: [
          [index_name: "content",
           keys: [:content],
           create_if_not_exists: true,
           provisioned_throughput: [1,1],
           projection: [projection_type: :include, non_key_attributes: [:email]]]
        ]
      ]) do

      add :content, string
    end
  end

  def down do
    alter table(:post,
      options: [
        global_indexes: [
          [index_name: "content",
            drop_if_exists: true]]
      ]
    ) do
      remove :content
    end
  end


# Migration file 4:

  def up do
    alter table(:post) do
      # modify will not be processed in a rollback if 'change' is used
      modify :"email_content", :string, provisioned_throughput: [2,2]
      remove :content
    end
  end

  def down do
    alter table(:post,
      options: [
        global_indexes: [
          [index_name: "content",
           keys: [:content],
           projection: [projection_type: :include, non_key_attributes: [:email]]]
        ]
      ]) do

      modify :"email_content", :string, provisioned_throughput: [100,100]
      add :content, :string
    end
  end

Link to this section Summary

Link to this section Functions

Link to this function

execute_ddl(arg1, string, options)

View Source