views:

162

answers:

2

I have a django model in use on a production application and I need to change the name and data type of the field with zero downtime to the site. So here is what I was planning:

1) Create the new field in the database that will replace the original field
2) Everytime an instance of the Model is loaded, convert the data form the original field and store it into the new field, then save the object (only save object if new field is empty)
3) Over time the original field can be removed once every object has a non-blank new field

What method can I attach too for the 2nd step?

+1  A: 

Sounds complex, and effects a lot of production code.

Are you trying to avoid doing this in bulk because of downtime? What volume of data are you working with?

Have you looked at any Django migration tools that are out there. South is a very popular one:

http://south.aeracode.org/

Andy Hume
Good advice, I'm still using Evolutions and not South. It sounds like South would fix a lot of my problems.
MikeN
+1  A: 

Won't you have to change your business logic (and perhaps templates) first to accomodate the new fieldname?
Unless stuff gets assigned to the field in question at dozens of places in your code, you could (after creation of the field in the database)

1) adapt the code to recognize the old (read) and the new field(name)s (write).
2) change the data in the database from old to new field via locking / .update() call, etc.
3) remove the old field(name) from the model/views/templates completely

Without downtime, I don't see how users of your site will not suffer getting "old" values for a few seconds (depending on how many rows are in the table, how costly the recalc to the new datatype, etc.).