How to detect whether the mnesia schema and table have been created in code?
Asked Answered
B

2

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?

Banking answered 8/2, 2012 at 1:59 Comment(2)
even calling: mnesia:info(). in shell will echo many useful thingsVolkman
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.Ruel
Q
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.

Quadrangle answered 8/2, 2012 at 6:1 Comment(1)
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.Ruel
V
3

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

Volkman answered 8/2, 2012 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.