I now have a working solution to ticket #12.

I've added some code to management.py that allows one to run:
> django-admin.py transition <app>

This then compares your current db to your models in that app, and
next to each model creates a "transition file" so if your model was
called "app.py", next to it would appear "app.transition.py".

The transition file has a sort of snapshot of what it's going to do to
your database.  A transition file looks something like this:
# Django transition file
from django.core.meta.transition import *

Add('Document.attachments')
Drop('app_departments_users')
Drop('app_medias')
Add('Document.rating')

django-admin transition figured out that my module "Document" has a
many to many field named "attachments" that isn't represented in the
database.  It also found two tables, "app_departments_users" and
"app_medias", which are in the database but not in my models.  And
finally, it realized that I had added "rating =
meta.IntegerField(default=0)" to my "Document" module.

One can edit this by hand, or if it looks good, go with it:
> django-admin.py sqlupdate <app>

Now, sqlupdate looks at the "app.transition.py" file, and executes the
changes accordingly, spitting out:
BEGIN;
DROP TABLE `app_departments_users`;
DROP TABLE `app_medias`;
ALTER TABLE `app_documents` ADD COLUMN `rating` integer NOT NULL;
CREATE TABLE app_documents_attachments (
    id mediumint(9) unsigned auto_increment NOT NULL PRIMARY KEY,
    document_id integer NOT NULL REFERENCES app_documents (id),
    file_id integer NOT NULL REFERENCES app_files (id),
    UNIQUE (document_id, file_id)
);
COMMIT;

I have only tested this with mysql, but it seems to be working well. 
I'm still adding comments to the code, should I add a patch to #12 or
should I send it to someone?

Thanks

Reply via email to