tags:

views:

60

answers:

3

Is there a side effect to calling mnesia:create_schema() on each application start?

From what I keep reading, this function should only be called once per database instance. Is it a big issue to call it more than once on an existing database?

A: 

Well it could throw an exception on the second call. Just catch it.

ko-dos
are you sure about the `exception` because that's not listed in the doc: http://www.erlang.org/doc/man/mnesia.html#create_schema-1.
jldupont
It does not throw an exception. It returns with {error, ...} if anything goes wrong.
Zed
+1  A: 

I've done this before in development and it spits out warnings on the tables that already exist. However I wouldn't make it a practice to rerun it in Production since it's possible that it may have some side-effects I'm unaware of and even if it doesn't now there is no guarantee that it won't in future releases.

Why do you want to run it multiple times?

Jeremy Wall
I also feel the handling of mnesia schema awkward. You have to call it once, as sort of part of your installation procedure, but there is no convenient place to do so.
Zed
@Zed: that's what I feel too. I don't like having to create a "do this once" management path hence I intend to "run" this function on every daemon start.
jldupont
How do you handle postgres and initdb there? Do you call it at startup of your application?
Christian
I usually just have an erlang setup script. to set up the db. It's a deployment step not an applciation startup step and it makes sense to keep them seperate as such.
Jeremy Wall
+1  A: 

It has no side effect, but later calls will result in {error, {Node,{already_exists,Node}}}. You can use something like

ensure_schema() ->
  Node = node(),
  case mnesia:create_schema([Node]) of
    ok -> ok;
    {error, {Node, {already_exists, Node}}} -> ok;
    Error -> Error
  end.
Zed