Django Schema Evolution
I’ve ported my schema evolution work from my SoC project last summer to Django v0.96. To use it, download the patch below, and run the following:
$ cd /<path_to_python_dir>/site-packages/django/
$ patch -p1 < ~/<download_dir>/django_schema_evolution-v096patch.txt
It should output the following:
patching file core/management.py
patching file db/backends/mysql/base.py
patching file db/backends/mysql/introspection.py
patching file db/backends/postgresql/base.py
patching file db/backends/postgresql/introspection.py
patching file db/backends/sqlite3/base.py
patching file db/backends/sqlite3/introspection.py
patching file db/models/fields/__init__.py
patching file db/models/options.py
To use it:
$ cd /<path_to_project_dir>/
$ ./manage.py sqlevolve <app_name>
It should output something like this:
BEGIN;
ALTER TABLE `main_query` CHANGE COLUMN `accuracy` `accuracynew` numeric(10, 6) NULL;
ALTER TABLE `main_query` ADD COLUMN `price` varchar(256) NULL;
COMMIT;
Assuming you have a model such as this:
class Query(models.Model):
query = models.CharField(maxlength=256, blank=False)
accuracynew = models.FloatField(max_digits=10, decimal_places=6, null=True, blank=True, aka='accuracy')
price = models.CharField(maxlength=256, null=True, blank=True) # new column
Note the aka field where I changed the name of “accuracy” to “accuracynew”.
Source code:
Documentation:
Let me know if you find any bugs.
UPDATE: This project is now on Google Code: http://code.google.com/p/deseb/








July 20th, 2007 at 11:05 am
This looks very promising. How does this compare to Rails’ Migration feature? Looks a lot smoother.
July 23rd, 2007 at 1:12 am
How to run custom SQL as part of a schema migration? For instance if I wanted to combine two columns into one column?
July 23rd, 2007 at 2:32 pm
Is this going to be merged into Django proper?
July 24th, 2007 at 6:51 pm
Or is there any equavalent for this planned for Django root? does this patch work with .97-latest?
July 24th, 2007 at 10:40 pm
david: that’s my goal
rtconner: yes to your second question
July 28th, 2007 at 3:34 am
Don’t work for postgres_psycopg2 backend! If you copy changes from postgres backend, don’t work also.
File “D:\web\projects\ef\manage.py”, line 11, in ?
execute_manager(settings)
File “d:\program\python24\lib\site-packages\django\core\management.py”, line 1952, in execute_manager
execute_from_command_line(action_mapping, argv)
File “d:\program\python24\lib\site-packages\django\core\management.py”, line 1843, in execute_from_command_line
action_mapping[action](int(options.verbosity), options.interactive)
File “d:\program\python24\lib\site-packages\django\core\management.py”, line 793, in syncdb
for sql in get_sql_evolution(app):
File “d:\program\python24\lib\site-packages\django\core\management.py”, line 546, in get_sql_evolution
output = get_sql_evolution_check_for_dead_fields(klass, new_table_name)
File “d:\program\python24\lib\site-packages\django\core\management.py”, line 698, in get_sql_evolution_check_for_dead_fields
suspect_fields.difference_update(f.aka)
TypeError: iteration over non-sequence
July 30th, 2007 at 1:12 pm
buriy: fixed this in the schema-evolution branch. (i assume this is where you ran across it, not in the v0.96 patch, right?)
July 31st, 2007 at 5:08 am
Looks like excellent stuff! Looking closer at what you’re doing here, it looks like our migration projects would actually work very nicely together! A situation where individual developers in a team could use evolution to generate the migrations they need, review them, and then use dbmigration to automatically roll them out to demo/UAT/live servers would be my ideal way of doing things.
July 31st, 2007 at 2:04 pm
mike, i couldn’t agree more.
August 6th, 2007 at 5:50 am
Please, patch for django-trunk.
By the way, works perfectly for django-newforms admin!
August 15th, 2007 at 7:48 am
This code is very interesting for creating an audit trail for a given model.
http://code.djangoproject.com/wiki/AuditTrail
December 16th, 2007 at 10:45 pm
[...] Aparentemente pisándole los talones al proyecto de Mike, Derek Anderson, el autor del proyecto schema-evolution del Summer of Code 2006, ha actualizado su código para que funcione adecuadamente con la versión 0.96 de Django. Derek afirma que el schema-evolution también debería funcionar correctamente con la versión trunk de Django . [...]
May 15th, 2008 at 12:03 am
I don’t know if you are interested guys but there is an interesting new approach to schema evolution, is called PRISM, developed at UCLA is a tool to support schema evolution in relational DB.
there is a video of th interface here: http://yellowstone.cs.ucla.edu/schema-evolution/documents/Prism-Demo.mov
while from here: http://yellowstone.cs.ucla.edu/schema-evolution/index.php/PrismDemo you can access the on-line demo.
April 30th, 2009 at 10:11 pm
You know, the thing about SQL is, that there is virtually nothing that can replace it.
Does anyone know if a substitute exists for sql? I mean besides MS SQL and Oracle and all that jazz. Thanks.
March 6th, 2010 at 1:47 pm
Thanks for your article. I am new at django and this will be a big help.