[erlang-questions] Strategies for updating Mnesia tables
Igor Ribeiro Sucupira
igorrs@REDACTED
Tue May 13 06:13:29 CEST 2008
On Fri, May 9, 2008 at 12:07 PM, Alexander Lamb
<alexander.lamb@REDACTED> wrote:
> Hello,
>
> Although Mnesia columns can store any kind of Erlang terms, it can
> happen that we need to add a column after an application is deployed.
> I understood (and tried) how to update the table.
>
> However, on the contrary to traditionnal SQL databases, this will have
> an impact on my code. Indeed, something like:
>
> ps_get_profiles(System_Name,Production_Type) ->
> F = fun() -> mnesia:match_object(profiles,{profiles,
> {'_','_','_'},System_Name,Production_Type,'_','_','_'},read) end,
> case mnesia:transaction(F) of
> {atomic, Records} -> {ok, Records};
> {aborted, Reason} -> {error, Reason}
> end.
>
> Will simply find 0 records once I add a column, hence failing silently.
Well... the approach below is VERY ugly, but it should make you able
to add columns without recompiling.
ps_get_profiles(System_Name,Production_Type) ->
F = fun() -> qlc:eval(qlc:q([ProfilesRecord ||
ProfilesRecord <-
mnesia:table(profiles),
System_Name =:= element(3,
ProfilesRecord),
Production_Type =:=
element(4, ProfilesRecord),
is_tuple(element(2, ProfilesRecord)),
size(element(2,
ProfilesRecord)) =:= 3]))
end,
case mnesia:transaction(F) of
{atomic, Records} -> {ok, Records};
{aborted, Reason} -> {error, Reason}
end.
But, as I'm pretty new to Erlang (couple of weeks), I suppose there
must be a more beautiful way of doing that.
Igor.
> Is there a way to match against a variable number of columns, giving
> the first columns patterns. Something like:
>
> F = fun() -> mnesia:match_object(profiles,{profiles,
> {'_','_','_'},System_Name,Production_Type,'_','_','_', * },read) end,
>
> See the "*" at the end of the match_object match part.
>
> What is the strategy when updating software that necessitates adding
> or changing Mnesia tables.
>
> Thanks,
>
> Alex
> --
> Alexander Lamb
> Founding Associate
> RODANOTECH Sàrl
>
> 4 ch. de la Tour de Champel
> 1206 Geneva
> Switzerland
>
> Tel: 022 347 77 37
> Fax: 022 347 77 38
>
> http://www.rodanotech.ch
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list