kura_migration behaviour (kura v1.19.2)

View Source

Behaviour for defining database migrations.

Implement up/0 and down/0 returning a list of DDL operations. Migration modules must be named m<YYYYMMDDHHMMSS>_<name>.

-module(m20250101120000_create_users).
-behaviour(kura_migration).
-include_lib("kura/include/kura.hrl").

up() ->
    [{create_table, <<"users">>, [
        #kura_column{name = id, type = id, primary_key = true},
        #kura_column{name = name, type = string, nullable = false},
        #kura_column{name = email, type = string, nullable = false}
    ]},
    {create_index, <<"users">>, [email], #{unique => true}}].

down() ->
    [{drop_index, <<"users_email_index">>},
     {drop_table, <<"users">>}].

Summary

Functions

Generate an Ecto-style index name: {table}_{cols}_index.

Types

alter_op()

-type alter_op() ::
          {add_column, column_def()} |
          {drop_column, atom()} |
          {rename_column, atom(), atom()} |
          {modify_column, atom(), kura_types:kura_type()}.

column_def()

-type column_def() ::
          #kura_column{name :: atom(),
                       type :: kura_types:kura_type(),
                       nullable :: boolean(),
                       default :: term(),
                       primary_key :: boolean(),
                       references :: {binary(), atom()} | undefined,
                       on_delete :: cascade | restrict | set_null | no_action | undefined,
                       on_update :: cascade | restrict | set_null | no_action | undefined}.

index_def()

-type index_def() :: {[atom()], index_opts_map()}.

index_opts()

-type index_opts() :: [unique | {where, binary()}].

index_opts_map()

-type index_opts_map() :: #{unique => boolean(), where => binary()}.

operation()

-type operation() ::
          {create_table, binary(), [column_def()]} |
          {create_table, binary(), [column_def()], [table_constraint()]} |
          {drop_table, binary()} |
          {alter_table, binary(), [alter_op()]} |
          {create_index, binary(), [atom()], index_opts_map()} |
          {create_index, binary(), binary(), [atom()], index_opts()} |
          {drop_index, binary()} |
          {execute, binary()}.

safe_entry()

-type safe_entry() ::
          {drop_column, atom()} |
          {rename_column, atom()} |
          {modify_column, atom()} |
          {add_column, atom()} |
          drop_table.

table_constraint()

-type table_constraint() :: {unique, [atom()]} | {check, binary()}.

Callbacks

down()

-callback down() -> [operation()].

safe()

(optional)
-callback safe() -> [safe_entry()].

up()

-callback up() -> [operation()].

Functions

index_name(Table, Cols)

-spec index_name(binary(), [atom()]) -> binary().

Generate an Ecto-style index name: {table}_{cols}_index.