﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
695	sqlupdate, creates sql to update the database based on an updated model file	brantley (deadwisdom@…	Adrian Holovaty	"I've created a function for django-admin.py that dumps the sql that will transition an old database model (the one that is in the database currently) to a new one based an updated django model.  So if you change the name of a model, or add a column, or delete a column, etc. it will output the sql to alter the database for you.  And I think it's a shame that no one else has this, as it is remarkably usefull.

For instance, if I have this originally:
{{{
class Poll(meta.Model):
   question = meta.CharField(maxlength=128)
   responses = meta.IntegerField(default=0)
}}}

And I want to go to this:
{{{
class Poll(meta.Model):
   question = meta.CharField(maxlength=128)
   title = meta.CharField(maxlength=128)
}}}

It will create the sql to drop ""responses"" and add ""title"".

There are two steps in doing this.  The first is to create a ""transition"" file.  This file allows you to tweak the changes that the code thinks should be done.  For instance, if you change Poll a lot, and then changed the name to Survey or something, it might think you dropped Poll and created a table called Survey.  So it would create in the file:
{{{
Drop(""Poll"")
Add(""Survey"")
}}}


But you might want to change it to:
{{{
Name(""app_polls"", ""Survey"")
Change(""Survey"")
}}}

Which tells it that the table that was named polls, should be called ""Survey"" now, and also it should make all the changes to ""Survey"".

This all goes in a file right next to your model file.  If your model file was called ""model.py"", the transition file would be called ""model.transition.py""

The next step is to actually run the sqlupdate, that will analyze the transition file, and spew out the SQL to realize those changes.

To perform the complete steps:
  1. django-admin.py transition <app_name>
  2. <check the transition file to make sure it's kosher>
  3. django-admin.py sqlupdate <app_name>  # which won't commit the changes
  4. django-admin.py sqlupdate <app_name> | mysql -p <project_name>   # which will commit the changes (in mysql)
"	defect	closed	Database layer (models, ORM)	0.91	blocker	duplicate			Unreviewed	0	0	0	0	0	0
