kura_migration behaviour (kura v1.19.2)
View SourceBehaviour 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
-type alter_op() :: {add_column, column_def()} | {drop_column, atom()} | {rename_column, atom(), atom()} | {modify_column, atom(), kura_types:kura_type()}.
-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}.
-type index_def() :: {[atom()], index_opts_map()}.
-type index_opts() :: [unique | {where, binary()}].
-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()}.
Callbacks
-callback down() -> [operation()].
-callback safe() -> [safe_entry()].
-callback up() -> [operation()].