Mnesia schema transaction: Checking if a table exists

Ulf Wiger ulf.wiger@REDACTED
Thu Mar 4 17:05:31 CET 2004


The way to do this using the code that already exists in mnesia
is perhaps this:


(boo@REDACTED)22> Create = fun(Name) ->
         mnesia_schema:do_create_table(
           mnesia_schema:list2cs([{name,Name},{ram_copies,[node()]}])) end.
#Fun<erl_eval.6.39074546>
(boo@REDACTED)23> mnesia_schema:schema_transaction(
                    fun() -> case catch Create(a) of
                               {'EXIT',_} -> Create(b);
                                _ -> ok
                             end end).
{atomic,ok}
(boo@REDACTED)24> mnesia_schema:schema_transaction(
                   fun() -> case catch Create(a) of
                              {'EXIT',_} ->
                                 Create(b);
                              _ -> ok
                            end end).
{atomic,ok}
(boo@REDACTED)25> mnesia:system_info(tables).
[b,a,schema]


OK, ugly example, but basically, you catch do_create_table/1.
If it exits (actually with reason {already_exists, Tab}), you
know that the table is there. Not pretty, but should work.

If you feel like patching mnesia_schema.erl, you might want
to invent a function like mnesia:system_info/1, but that
takes into account tables being created in an ongoing transaction:

local_system_info(tables) ->
     TidTs = get(mnesia_activity_state),
     case TidTs of
	{_Mod, Tid, Ts} when record(Ts, tidstore)->
	    Store = Ts#tidstore.store,
	    NewTabs =
		ets:select(Store, [{{op,create_table,
				     [{name,'$1'}|'_']},[],['$1']}]),
	    NewTabs ++ mnesia:system_info(tables);
	_ ->
	    mnesia:system_info(tables)
     end.

I'd recommend the first option.  (:

/Uffe


On Thu, 4 Mar 2004 15:13:09 +0100, Rudolph van Graan 
<rvg@REDACTED> wrote:

> Hi again,
>
> Thanks for the information - I can now create tables in a transaction.
> However, this leads to the next problem...
>
> I am busy creating say 10 tables... As part of my creation script, I
> need to check if another table exists, so I made a function like this
> one:
>
> table_exists(TableName) ->
>    Tables = mnesia:system_info(tables),
>    lists:member(TableName,Tables).
>
> This works fine if I use it outside the schema_transaction for a table
> that has just been created. If I use it inside the schema_transaction,
> on a table that has just been created in the same transaction, it
> fails... Is there some other function I can use to check for a table
> that has just been created in the same function and that will query the
> changes for the current transaction instead? [The idea is that the
> transaction must abort and all the changes rolled back if something
> goes wrong here]. I.e. if I do create_table(), and immediately run this
> function afterwards, it must return true while inside the schema
> transaction.
>
> Regards.
>
> Rudolph van Graan



-- 
Ulf Wiger, Senior System Architect
EAB/UPD/S

This communication is confidential and intended solely for the addressee(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you believe this message has been sent to you in error, please notify the sender by replying to this transmission and delete the message without disclosing it. Thank you.

E-mail including attachments is susceptible to data corruption, interruption, unauthorized amendment, tampering and viruses, and we only send and receive e-mails on the basis that we are not liable for any such corruption, interception, amendment, tampering or viruses or any consequences thereof.




More information about the erlang-questions mailing list