38 lines
1.1 KiB
Erlang
Executable File
38 lines
1.1 KiB
Erlang
Executable File
%% @doc Create calendar_follow table and indexes if missing.
|
|
-module('20260720210000_calendar_follow').
|
|
|
|
-export([up/0, down/0]).
|
|
|
|
-include("records.hrl").
|
|
|
|
up() ->
|
|
ensure_table(calendar_follow, record_info(fields, calendar_follow)),
|
|
ensure_index(calendar_follow, calendar_id),
|
|
ensure_index(calendar_follow, user_id),
|
|
ok.
|
|
|
|
down() ->
|
|
_ = mnesia:delete_table(calendar_follow),
|
|
ok.
|
|
|
|
ensure_table(Table, Attrs) ->
|
|
case lists:member(Table, mnesia:system_info(tables)) of
|
|
true ->
|
|
ok;
|
|
false ->
|
|
case mnesia:create_table(Table, [{disc_copies, [node()]}, {attributes, Attrs}]) of
|
|
{atomic, ok} -> ok;
|
|
{aborted, {already_exists, Table}} -> ok;
|
|
{aborted, Reason} -> error({create_table_failed, Table, Reason})
|
|
end
|
|
end.
|
|
|
|
ensure_index(Table, Attr) ->
|
|
case mnesia:add_table_index(Table, Attr) of
|
|
{atomic, ok} -> ok;
|
|
{aborted, {already_exists, Table, _Pos}} -> ok;
|
|
{aborted, {already_exists, Table, Attr}} -> ok;
|
|
{aborted, {already_exists, _}} -> ok;
|
|
{aborted, Reason} -> error({add_index_failed, Table, Attr, Reason})
|
|
end.
|