[erlang-questions] Accessing two versions of records when doing transform_table in mnesia

devdoer bird devdoer2@REDACTED
Thu Jul 17 14:20:11 CEST 2008

2008/7/17, Hakan Mattsson <hakan@REDACTED>:
> On Thu, 17 Jul 2008, devdoer bird wrote:
> db> Hi:
> db> I can use transform_table  to upgrade the table schema in mnesia.
> db>
> db> Eg.The version 1 of user record's defination is
> db>
> db> -record(user_v1,{uuid,name})
> db>
> db> I want to upgrade the user table to version 2
> db>
> db> -record(user_v2,{uuid,name,email}).
> db>
> db> I can use transform_table like this:
> db>
> db>  transform_table(user,fun({user_v1,Uuid,Name})->#user_v2{uuid=Uuid,name=Name,email=""}
> db> end,record_info(fields,user_v2),user_v2).
> db>
> db> My question is
> db> 1. while the upgrade is in in progress, is the user record I
> read  either
> db> the version 1 or the version 2 ?That is I can't  be sure one user
> record's
> db> current version.
> db> 2. while the upgrade is in progress,will the modify  of the old user
> record
> db> be lost?
> It will differ per module and process. During the upgrade you may have
> some processes that are using the old module and some are using the
> new module. If this is an issue, you need to perform a more advanced
> upgrade procedure that first suspends the processes, then loads the
> new code and finaly resumes the processes.

I can't understand. Won't one process read either version 1 or version 2?
Eg. there're 5 user records of version 1 intially, when I do
transform_table,there may
be 3 user records of version 1,and 2 user records of version 2 if the
transform_table operation is not finished yet.

db> 3. If the 1 and 2 are true,how can I  code two handle them? Shall I have
> to
> db> have two versions of process code to handle this situation ,like:
> db> process_one_user(UserRecord)->
> db>    case of current_version(UserRecord) of
> db>         v1-> do something;
> db>         v2->do  something
> db>      end
> db> end
> One way of getting better control of this, is to change the name of the
> record.
> Then it will be easy to test which "version" of the record that you are
> using.
> See also mnesia:transform_table/4.

If I do so,many codes rely on the original record name will be changed, and
I have to code in  if-else style in many place .Am I right?

> ---
> Håkan Mattsson (uabhams)
> Erlang/OTP, Ericsson
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080717/cd1025ea/attachment.htm>

More information about the erlang-questions mailing list