7

I want to create an mnesia schema and table in my code after the system starts, so I need to detect weather the mnesia schema and table have been created. If not, I want to create them. Is this a good idea? And how can I detect the mnesia schema and table?

Kevin Albrecht
  • 6,974
  • 7
  • 44
  • 56
why
  • 23,923
  • 29
  • 97
  • 142
  • even calling: `mnesia:info().` in shell will echo many useful things – Muzaaya Joshua Feb 08 '12 at 07:12
  • I'm not satisfied with these answers, I wish there was a more direct way to do this. Calling mnesia:scheme() seems to be the cleanest approach, it only errors that first time. You can catch the error and create the schema and the table. But all the other times it runs successfully and logs the info instead of returning it, which seems sloppy. – RustyToms Nov 05 '18 at 01:35

2 Answers2

5

One way to handle this is -

  1. Try creating table using mnesia:create_table(Table_name, ...)

  2. If the table already exists (1) would return {aborted, {already_exists, Table_name}}

  3. If table doesn't exit, it will be created and {atomic,ok} will be returned if successful

  4. If there is any error in table creation in (3), {aborted, Reason} will be returned.

Handle each of these return values as required.

spkhaira
  • 821
  • 7
  • 18
  • Before you can create a table you need to create the schema, so I don't see how this solution works. You'd need to catch the error thrown when the schema is created and when `application:start(mnesia)` is called as well. – RustyToms Nov 05 '18 at 01:33
3

check out mnesia:system_info/1, mnesia:schema/0, mnesia:schema/1 and mnesia:table_info/2.

Muzaaya Joshua
  • 7,736
  • 3
  • 47
  • 86