[erlang-questions] How to upgrade the Mnesia record structure?

Ulf Wiger ulf@REDACTED
Mon Jun 3 09:02:19 CEST 2013


You should absolutely update all existing records in the table. You do this with

mnesia:transform_table(Tab, Fun, NewAttributeList)

Note that the manual says:

"Fun is a function which takes a record of the old type and returns a transformed record of the new type. The Fun argument can also be the atom ignore, it indicates that only the meta data about the table will be updated. Usage of ignore is not recommended but included as a possibility for the user do to his own transform."

The reason for the 'ignore' trick is that it is sometimes not feasible to transform the entire table within a transaction while user requests are held off waiting for the operation to complete. One can then instead transform the table definition and spawn a process that transforms one record at a time.

This would of course mean that any code that reads from the table must be prepared to get an old record, and transform it on the fly to the latest record format. This is not terribly difficult to accomplish*, but requires some forethought.

For various reasons like the above, it is a very good idea to have your own code access mnesia via a database API that *you* control. An alternative is to always use mnesia:activity/2 (even for dirty access), making it possible to introduce these workarounds in your own access module. That is, never use mnesia:transaction/1 - use mnesia:activity/2 instead.

* At least as long as you mainly read objects. If you are e.g. using QLC, it becomes a non-trivial task.

BR,
Ulf W

On 3 Jun 2013, at 08:01, yashgt@REDACTED wrote:

> If a record is defined as:
> 
> -record(shop, {item, quantity, cost}).
> 
> and several records are stored in an Mnesia table, and a few months later the structure is changed to :
> -record(shop, {item, quantity, source, company, cost}).
> 
> Do we need to migrate ALL existing records in the DB to include the additional 2 elements in the tuple? I know that a record is actually a tuple. If we leave the existing records untouched, will the database get messed becaues of new records coming in with the new structure?
> 
> Thanks,
> Yash
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130603/c0f9ced1/attachment.htm>


More information about the erlang-questions mailing list