View Source EctoTablestore.Migration (ecto_tablestore v0.15.1)

Migrations are used to create your tables.

Support the partition key is autoincrementing based on this library's wrapper, for this use case, we can use the migration to automatically create an another separated table to generate the serial value when :insert (viz ExAliyunOts.put_row/5) or :batch_write (viz ExAliyunOts.batch_write/3) with :put option.

In practice, we don't create migration files by hand either, we typically use mix ecto.ots.gen.migration to generate the file with the proper timestamp and then we just fill in its contents:

$ mix ecto.ots.gen.migration create_posts_table

And then we can fill the table definition details:

defmodule EctoTablestore.TestRepo.Migrations.CreatePostsTable do
  use EctoTablestore.Migration

  def change do
    create table("ecto_ots_test_posts") do
      add :post_id, :integer, partition_key: true, auto_increment: true
    end
  end
end

After we filled the above migration content, you can run the migration above by going to the root of your project and typing:

$ mix ecto.ots.migrate

Finally, we successfully create the "ecto_ots_test_posts" table, since the above definition added an autoincrementing column for the partition key, there will automatically create an "ecto_ots_test_posts_seq" table to generate a serial integer for :post_id field when insert a new record.

Summary

Functions

Adds a primary key when creating a table.

Adds a column what already be pre-defined column when creating a secondary index.

Adds a pre-defined column when creating a table.

Adds a secondary index when creating a table.

Adds a primary key when creating a secondary index.

Adds a primary key when creating a table.

Define the primary key(s) of the table to create.

Drops one of the following

Drops a table or index if it exists.

Returns a search index struct that can be given to create/2.

Returns a secondary index struct that can be given to create/2.

Returns a table struct that can be given to create/2.

Functions

Link to this macro

add(column, type, opts \\ [])

View Source (macro)

Adds a primary key when creating a table.

This function only accepts types as :string | :binary | :integer | :hashids | :id.

About :auto_increment option:

  • set :auto_increment as true and its field is primary key of non-partitioned key, there will use Tablestore's auto-increment column to process it.

  • set :auto_increment as true and its field is partition key, there will use ex_aliyun_ots's built-in Sequence function, the actual principle behind it is to use the atomic update operation though another separate table when generate serial integer, by default there will add an :id partition key as :integer type, the initial value of the sequence is 0, and the increment step is 1.

Tablestore can only have up to 4 primary keys, meanwhile the first defined primary key is the partition key, Please know that the order of the primary key definition will be directly mapped to the created table.

About :hashids type to define the partition key:

  • set partition_key as true is required.
  • set auto_increment as true is required.

Examples

The auto generated serial integer for partition key:

create table("posts") do
  add :title, :string
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
  add :title, :string
end

The explicitly defined field with partition_key:

create table("posts") do
  add :title, :string
end

# The above is equivalent to

create table("posts") do
  add :id, :integer, partition_key: true, auto_increment: true
  add :title, :string
end

The :auto_increment integer for primary key of non-partitioned key:

create table("posts") do
  add :tag, :integer, auto_increment: true
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
  add :version, :integer, auto_increment: true
end

The :hashids type for the partition key with the built-in sequence feature:

create table("posts") do
  add :id, :hashids, auto_increment: true, partition_key: true
end

The :id type for the partition key with the built-in sequence feature:

create table("posts") do
  add :id, :id
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
end

Options

  • :partition_key - when true, marks this field as the partition key, only the first explicitly defined field is available for this option.
  • :auto_increment - when true and this field is non-partitioned key, Tablestore automatically generates the primary key value, which is unique in the partition key, and which increases progressively, when true and this field is a partition key, use ex_aliyun_ots's Sequence to build a serial number for this field, the auto_increment: true option only allows binding of one primary key.
Link to this macro

add_column(column)

View Source (macro)

Adds a column what already be pre-defined column when creating a secondary index.

Link to this macro

add_column(column, type)

View Source (macro)

Adds a pre-defined column when creating a table.

This function only accepts types as :integer | :double | :boolean | :string | :binary.

For more information see the Chinese Docs | English Docs

Examples

create table("posts") do
  add_pk(:id, :integer, partition_key: true)
  add_pk(:owner_id, :string)
  add_column(:title, :string)
  add_column(:content, :string)
end
Link to this macro

add_index(index_name, primary_keys, defined_columns, options \\ [])

View Source (macro)

Adds a secondary index when creating a table.

For more information see the Chinese Docs | English Docs

Examples

create table("posts") do
  add_pk(:id, :integer, partition_key: true)
  add_pk(:owner_id, :string)
  add_column(:title, :string)
  add_column(:content, :string)
  add_index("posts_owner", [:owner_id, :id], [:title, :content])
  add_index("posts_title", [:title, :id], [:content], type: :global)
end

Options

  • :index_type, the type of the index, optional. Valid values: :global and :local. By default it is :global.
    • If :index_type is not specified or is set to :global, the global secondary index feature is used. If you use the global secondary index feature, Tablestore automatically synchronizes the columns to be indexed and data in primary key columns from a data table to an index table in asynchronous mode. The synchronization latency is within a few milliseconds.
    • If :index_type is set to :local, the local secondary index feature is used. If you use the local secondary index feature, Tablestore automatically synchronizes data from the indexed columns and the primary key columns of a data table to the columns of an index table in synchronous mode. After the data is written to the data table, you can query the data from the index table.
Link to this macro

add_pk(column)

View Source (macro)

Adds a primary key when creating a secondary index.

Link to this macro

add_pk(column, type, opts \\ [])

View Source (macro)

Adds a primary key when creating a table.

Same as add/2, see add/2 for more information.

Link to this macro

create(object, list)

View Source (macro)

Define the primary key(s) of the table to create.

By default, the table will also include an :id primary key field (it is also partition key) that has a type of :integer which is an autoincrementing column. Check the table/2 docs for more information.

There are up to 4 primary key(s) can be added when creation.

Example

create table("posts") do
  add :title, :string
end

# The above is equivalent to

create table("posts") do
  add :id, :integer, partition_key: true, auto_increment: true
  add :title, :string
end

Drops one of the following:

  • a table
  • a secondary index
  • a search index

Examples

drop table("posts")
drop secondary_index("posts", "posts_owner")
drop search_index("posts", "posts_index")

Drops a table or index if it exists.

Does not raise an error if the specified table or index does not exist.

Examples

drop_if_exists table("posts")
drop_if_exists secondary_index("posts", "posts_owner")
drop_if_exists search_index("posts", "posts_index")
Link to this function

search_index(table_name, index_name, opts \\ [])

View Source

Returns a search index struct that can be given to create/2.

For more information see the Chinese Docs | English Docs

Examples

create search_index("posts", "posts_owner") do
  field_schema_keyword("title")
  field_schema_keyword("content")
  field_sort("title")
end

Please refer the link to the available field schema definition options, and the link to the available sort options.

Link to this function

secondary_index(table_name, index_name, opts \\ [])

View Source

Returns a secondary index struct that can be given to create/2.

For more information see the Chinese Docs | English Docs

Examples

create secondary_index("posts", "posts_owner") do
  add_pk(:owner_id)
  add_pk(:id)
  add_column(:title)
  add_column(:content)
end

create secondary_index("posts", "posts_title", index_type: :global) do
  add_pk(:title)
  add_pk(:id)
  add_column(:content)
end

Options

  • :include_base_data, specifies whether the index table includes the existing data in the base table, if set it to true means the index includes the existing data, if set it to false means the index excludes the existing data, optional, by default it is true.

  • :index_type, the type of the index, optional. Valid values: :global and :local. By default it is :global.

    • If :index_type is not specified or is set to :global, the global secondary index feature is used. If you use the global secondary index feature, Tablestore automatically synchronizes the columns to be indexed and data in primary key columns from a data table to an index table in asynchronous mode. The synchronization latency is within a few milliseconds.
    • If :index_type is set to :local, the local secondary index feature is used. If you use the local secondary index feature, Tablestore automatically synchronizes data from the indexed columns and the primary key columns of a data table to the columns of an index table in synchronous mode. After the data is written to the data table, you can query the data from the index table.

Returns a table struct that can be given to create/2.

Since Tablestore is a NoSQL service, there are up to 4 primary key(s) can be added when creation, the first added key is partition key when set partition_key option as false.

Examples

create table("products") do
  add :name, :string
  add :price, :integer
end

create table("products", partition_key: false) do
  add :name, :string
  add :price, :integer
end

Options

  • :partition_key - as true by default, and there will add an :id field as partition key with type as a large autoincrementing integer (as bigserial), Tablestore does not support bigserial type for primary keys, but can use the ex_aliyun_ots lib's wrapper - Sequence to implement it; when false, a partition key field is not generated on table creation.

  • :prefix - the prefix for the table.

  • :meta - define the meta information when create table, can see Tablestore's document for details:

    • :reserved_throughput_write - reserve the throughput for write when create table, an integer, the default value is 0;
    • :reserved_throughput_read - reserve the throughput for read when create table, an integer, the default value is 0;
    • :time_to_live - the survival time of the saved data, a.k.a TTL; an integer, unit as second, the default value is -1 (permanent preservation);
    • :deviation_cell_version_in_sec - maximum version deviation, the default value is 86400 seconds, which is 1 day;
    • stream_spec - set the stream specification of Tablestore:
      • is_enabled, open or close stream
      • expiration_time, the expiration time of the table's stream
    • enable_local_txn - specifies whether to enable the local transaction feature. The value of this parameter is of the :boolean type. Default value: false. If you want to enable the local transaction feature when you create a data table, set this parameter to true.