1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
% This is called from init on multiple identical nodes started at the same time.
% The idea is for all the nodes to be equivalent, with no one of them being 
% different. What we want is to have a table 'data' replicated on all nodes at
% the end (whether the table already existed, or not at the time of calling
% setup_database).

setup_database() ->
    mnesia:start(),
    case lists:member(data, mnesia:system_info(tables)) of
        true -> ok; 
        false ->  
            case mnesia:create_table(data, [{attributes, record_info(fields, data)}, {disc_copies, nodes()}]) of
                {atomic, ok} -> ok;
                {aborted, already_exists} -> mnesia:add_table_copy(data, node(), disc_copies);
                {aborted, _} -> timer:sleep(random:uniform(5000)), setup_database()
            end
    end.