1
2
3
4
5
6
7
8
9
10
11
12
setup_database() ->
    mnesia:start(),
    mnesia:change_config(extra_db_nodes, nodes()),
    case lists:member(data, mnesia:system_info(tables)) of
        true  -> mnesia:add_table_copy(data, node(), ram_copies);
        false ->
            case mnesia:create_table(data, [{attributes, record_info(fields, data)}, {ram_copies, [node()|nodes()]}]) of
                {atomic, ok} -> ok;
                {aborted, already_exists} -> mnesia:add_table_copy(data, node(), ram_copies);
                {aborted, _} -> timer:sleep(random:uniform(5000)), setup_database()
            end
    end.