[erlang-questions] Initializing mnesia

Ivan Carmenates Garcia <>
Fri Oct 9 05:51:36 CEST 2015


Hi Lloyd I think there is no need to check all those conditions you write
about, you just have to put the init method in a supervisor three and don't
worry about checking if schema exits because you can always create it and if
it already exists nothing happened mnesia don't erase the previews schema
neither the tables with information. i.e. I have this and works pretty well
for me.

init([]) ->
    case application:get_env(cowboy_enhancer, session_manager) of
        %% TODO: do something with config.
        {ok, Config} ->
            mnesia:create_schema([node()]),
            mnesia:start(),
            mnesia:create_table(session, [
                {disc_copies, [node()]},
                {type, set},
                {attributes, record_info(fields, session)}]),
            case mnesia:wait_for_tables([session], 5000) of
                {timeout, _RemainingTabs} ->
                    {stop, mnesia_timeout};
                ok ->
                    %% starts the session garbage collector.
                    start_garbage_collector(),
                    {ok, #state{}}
            end;
        undefined ->
            {stop, invalid_or_missing_configuration}
    end.

NOTE: you do need to wait for tables to be ready in all involved nodes just
as Vance said to you.



-----Original Message-----
From: 
[mailto:] On Behalf Of

Sent: Thursday, October 8, 2015 7:09 PM
To: Erlang (E-mail)
Subject: [erlang-questions] Initializing mnesia



Hello,

At first blush, initializing mnesia looks like a piece of cake:

init() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(....

But suppose one wants to automate it--- "Look, Ma, no hands!" by, say,
running it under a top-level supervisor.

Seems we need to:

- check to see if we have a schema
    -- if not, create schema
- check to see if mnesia is running
    -- if not, start it
- check to see if tables are defined
    -- if not, define them

Surfing around I found this:

http://erlang.2086793.n4.nabble.com/When-to-create-mnesia-schema-for-OTP-app
lications-td2115607.html

Seems to work with Seth Falcon's fix; but the fix produces a warning when
compiled. I could live with that, but it bugs me.

Seems to me there must be a more elegant way to solve the problem. Spent
half a day on it but the explosion of crash conditions and valid returns put
my brain in tilt mode.

It's not like I'm inventing the wheel here. Does some kind should have a
simple mean-and-lean solution to the problem?

Many thanks,

LRP










*********************************************
My books:

THE GOSPEL OF ASHES
http://thegospelofashes.com

Strength is not enough. Do they have the courage and the cunning? Can they
survive long enough to save the lives of millions?  

FREEIN' PANCHO
http://freeinpancho.com

A community of misfits help a troubled boy find his way 

AYA TAKEO
http://ayatakeo.com

Star-crossed love, war and power in an alternative universe

Available through Amazon or by request from your favorite bookstore


**********************************************

_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list