Best Practices for Creating a Mnesia Table if None Exists on Startup
Hi everyone,
we are currently working on an Erlang application that uses Mnesia for its embedded database, and I’m looking for the best way to handle table creation on startup when a table might not exist yet. I’ve explored a few potential solutions, but I’ve encountered some uncertainties and would appreciate insights from the community.
What I’ve Explored So Far:
-
Using
mnesia:table_info/2
:- The idea here is to use
mnesia:table_info(TableName, size)
to check if the table exists. If the table doesn’t exist, the function is supposed to return{aborted, {no_exists, TableName}}
. However, it’s unclear how reliable this method is, especially when dealing with tables that aren’t fully loaded yet.
- The idea here is to use
-
Checking with
mnesia:system_info/1
:- Another approach is to use
mnesia:system_info(tables)
to get a list of all known tables and check if the desired table is among them. This seems more straightforward but doesn’t address the potential issue of partially loaded tables.
- Another approach is to use
-
Using
mnesia:wait_for_tables/2
:- To ensure that the table is loaded,
mnesia:wait_for_tables([TableName], Timeout)
can be used. This is a good follow-up step after confirming the table’s existence, but it still depends on how the existence check is performed.
- To ensure that the table is loaded,
-
Understanding
mnesia_gvar
:- We found that Mnesia uses an internal ETS table (
mnesia_gvar
) to store global variables, including table properties. Functions likemnesia_schema:get_table_properties/1
interact with this table. However, the exact mechanics of when and howmnesia_gvar
is populated are not entirely clear, making it hard to rely on this knowledge for table creation checks.
- We found that Mnesia uses an internal ETS table (
What I Need Help With:
Given the above explorations, I’m looking for the best practice to:
- Check if a Mnesia table exists on startup.
- Create the table if it doesn’t exist.
I want to ensure that the approach is reliable and handles all edge cases, including tables that might not be fully loaded. What are the recommended strategies for this scenario? Is there a more straightforward or reliable method that I might have overlooked?
Thanks in advance for your help!