views:

80

answers:

2

Suppose i have an application A which depends on mnesia with disk-enabled schema being present. What i'd like to do is ensure that mnesia is running and allowing disc_copiestables from within A. I'm also considering the case where multiple applications need to access mnesia.

What would be the most portable (and standard) way to achieve this kind of thing, without hard coding mnesia startup and schema creation into the application callback module of A?

When developing interactively i simply do a

mnesia:create_schema([node()]).

within the Erlang shell to initialize an on-disk schema, then start the mnesia application with

mnesia:start().

and finally start the other ones that depend on a database being present.

+2  A: 

You can list dependendent applications in your .app file, see the {applications, Apps} field. That way you can make sure the app is not started without mnesia running, and when creating a release, it can generate a script that starts mnesia before your app.

Since the mnesia schema can be made persistent, creating the schema is not something your application should need to do in ordinary application startup. You can write an escript that sets up the schema as you need it.

When your application starts, it can then use mnesia:wait_for_tables/2 to make sure the tables are ready to use.

Christian
so "do it with a script" is the right approach?
Felix Lange
Schema creation should be considered part of the applications setup.Mnesia startup defined in the .app file where you define the entire process tree dependency chain for your application. The generated release file is then used to actually start your application with all dependent applications as well.
Jeremy Wall
i'd like to make deployment of our application as painless as possible and setting up the schema manually (or even from an install script) isn't acceptable in the scenario we face.
Felix Lange
A: 

I've found a solution myself. This is by no means the standard method, but it works.

Calling

mnesia:change_table_copy_type(schema, node(), disc_copies).

on application startup will ensure that the schema is disk-based while allowing mnesia to be started by a boot script. This blog entry was very helpful.

Felix Lange