开发者

How to handle mnesia schemas the standard way?

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 sche开发者_JS百科ma, then start the mnesia application with

mnesia:start().

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜