Prev Next

Database / Mnesia basics Interview questions

How do you migrate a Mnesia record definition when its fields change?

Simply changing a -record definition in your code and recompiling doesn't retroactively update records already stored in a Mnesia table — existing rows keep whatever shape they were written with, which will mismatch the new record definition the next time your code tries to read them.

%% old: -record(person, {id, name, age}).
%% new: -record(person, {id, name, age, email}).

mnesia:transform_table(person,
    fun(P) -> P#person{email = undefined} end,
    record_info(fields, person)).

The standard fix is mnesia:transform_table/3: it walks every existing record in the table, applies a transformation function you provide to reshape each one into the new format, and updates the table's schema to match, all while the table can remain usable. This must be run once as a deliberate migration step whenever a record's field list changes, not something that happens automatically just from recompiling.

Does recompiling a changed -record definition automatically update existing Mnesia rows?
What function performs this kind of record shape migration on an existing table?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Robinhood Logo

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Mnesia? What is a Mnesia table? What are the storage types available for a Mnesia table? What is a Mnesia schema? How do you create a Mnesia schema? How do you create a Mnesia table? What is a Mnesia transaction? How do you write a record to a Mnesia table? How do you read a record from a Mnesia table? What are dirty operations in Mnesia? What is mnesia:dirty_read/1? What is a Mnesia record? How do you delete a record from a Mnesia table? What is the purpose of mnesia:start/0? How do you stop Mnesia? What are the Mnesia table types (set, ordered_set, bag)? What is a Mnesia index? How do you add a secondary index to a Mnesia table? What is mnesia:info/0 used for? How do you check if Mnesia is running? What is the Mnesia directory (Mnesia.dir)? What is the primary key in a Mnesia table? What is mnesia:select/2 used for? What is record_info used for when creating a Mnesia table? What are the attributes of a Mnesia table? What is a Mnesia node? How do you replicate a table across multiple Mnesia nodes? What is mnesia:table_info/2 used for? Describe the difference between a Mnesia table and an ETS table? Why should Mnesia operations generally run inside a transaction? When should you use dirty operations instead of a transaction? What is the difference between ram_copies and disc_copies? What is disc_only_copies and when would you use it? What happens to a disc_copies table when its node restarts? How do you change a table's storage type at runtime? What is a Mnesia table lock and how does it work? How does Mnesia keep replicated tables consistent across nodes? How do you migrate a Mnesia record definition when its fields change? What is mnesia:transform_table/3 used for? When should you choose bag over set for a Mnesia table? How do you back up a Mnesia database? How do you restore a Mnesia database from a backup? Explain the lifecycle of a Mnesia transaction from start to commit? How do you troubleshoot a Mnesia table stuck in the "loading" state? What is mnesia:force_load_table/1 and when should you use it? What is the difference between mnesia:read/1 and mnesia:wread/1? What is a Mnesia checkpoint used for? How does Mnesia resolve conflicts after a network partition heals?
Show more question and Answers...

Integration

Comments & Discussions