Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: create a disc schema

If an Erlang application, myapp, requires mnesia to run, then mnesia should be included in its application resource file, under key applications, so that if myapp is started, mnesia would get started automatically - it's node type by default is opt_disc (OTP 18).

What if I want a disc node? I know I can set -mnesia schema_location disc at command line, but this only works if a schema already exists, which means I should perform some initialization before starting myapp, is there an "OTP-ful" way, without removing mnesia from applications, to avoid this initialization? The main objective is to turn "init-then-start" into just "start".

like image 814
Not an ID Avatar asked Jan 14 '16 08:01

Not an ID


1 Answers

This is not correct from your post:

... mnesia should be included in its application resource file, under key applications, so that if myapp is started, mnesia would get started automatically.

The applications which you write as a value of applications key in .app file don't start automatically, but it says that they must be started before your application is started.


Imagine that we want to create foo application which depends on mnesia with some customization. One way is starting it in foo_app.erl file:

-module(foo_app).
-behaviour(application).

-export([start/2, stop/1]).

start(_Type, _Args) ->
    mnesia:start().
    mnesia:change_table_copy_type(schema, node(), disc_copies),

    %% configure mnesia
    %% create your tables
    %% ...

    foo_sup:start_link().

stop(_State) ->
    ok.

This way it creates disc schema whether it was created before or not.


Note: In this solution if you write mnesia as dependency under applications key in you foo.app.src file (which in compile time would create foo.app), when starting the foo application you get {error, {not_started, mnesia}}. So you must not do it and let your application to start it in its foo_app:start/2 function.

like image 134
Hamidreza Soleimani Avatar answered Nov 19 '22 21:11

Hamidreza Soleimani