[erlang-questions] Support for newcomers and the popularity of Erlang

Wojciech Knapik <>
Tue Apr 3 03:39:22 CEST 2012

On Fri, Mar 30, 2012 at 02:08:39AM -0700, Ulf Wiger wrote:

> You can start mnesia with {extra_db_nodes, KnownNodes]}. This will
> establish a ram-only schema on the current node, containing all
> meta-data about the database (assuming KnownNodes contains actual
> schema nodes). After starting, you can call
> mnesia:change_table_copy_type(schema, node(), disc_copies), to make
> the schema on the current node persistent.

Ok, here's what I came up with.
This function is called from gen_server's init callback.
The code is run on multiple identical nodes (code-wise).
Any hints/comments/suggestions would be appreciated.

setup_database() ->
    DBBootstrap = fun() -> 
        mnesia:change_table_copy_type(schema, node(), disc_copies),
        mnesia:create_table(data, [{attributes, record_info(fields, data)}, {disc_copies, [node()]}])
    mnesia:start([{extra_db_nodes, nodes()}]),
    case lists:member(data, mnesia:system_info(tables)) of
        true  -> 
            case lists:member(node(), mnesia:table_info(data, active_replicas)) of
                true  -> ok;
                false -> mnesia:change_table_copy_type(schema, node(), disc_copies),
                         mnesia:add_table_copy(data, node(), disc_copies)
        false -> 
            case global:trans({db_bootstrap, self()}, DBBootstrap, [node()|nodes()], 0) of
                aborted -> mnesia:wait_for_tables([data], 5000), 
                {aborted, Reason} -> erlang:error(Reason);
                _ -> ok

More information about the erlang-questions mailing list