djschemadiff is a tool to help you keep the schema your Django web application's database up to date.

It compares the schema of your current database with that which would be produced if you started with a fresh, empty database and then ran syncdb, and shows you the difference between the two.

Run djschemadiff.py —help for usage information.

Downloads

Releases of djschemadiff can be downloaded from http://robots.org.uk/src/djschemadiff/releases/.

Requirements

Sample Output

Here is what the default udiff (unified diff) mode produces:

$ djschemadiff.py mysite/settings.py
... lots of output from PostgreSQL elided ...
--- current-schema
+++ new-schema
 CREATE INDEX articles_article_author_id ON articles_article USING btree (author_id);
 CREATE INDEX articles_article_category_id ON articles_article USING btree (category_id);
-CREATE INDEX articles_category_slug ON articles_category USING btree (slug);
+CREATE INDEX articles_article_slug ON articles_article USING btree (slug);
+CREATE UNIQUE INDEX articles_category_slug ON articles_category USING btree (slug);
 CREATE INDEX articles_comment_article_id ON articles_comment USING btree (article_id);
 CREATE INDEX articles_comment_author_id ON articles_comment USING btree (author_id);
 CREATE TABLE articles_category (
        description text NOT NULL,
        id serial NOT NULL,
        name character varying (32) NOT NULL,
-       slug character varying (50) NOT NULL
+       slug character varying (50) NOT NULL,
+       summary text NOT NULL
 );
-ALTER TABLE ONLY articles_article ADD CONSTRAINT articles_article_author_id_fkey FOREIGN KEY (author_id) REFERENCES auth_user(id);
-ALTER TABLE ONLY articles_article ADD CONSTRAINT articles_article_category_id_fkey FOREIGN KEY (category_id) REFERENCES articles_category(id);
+ALTER TABLE ONLY articles_article ADD CONSTRAINT articles_article_author_id_fkey FOREIGN KEY (author_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED;
+ALTER TABLE ONLY articles_article ADD CONSTRAINT articles_article_category_id_fkey FOREIGN KEY (category_id) REFERENCES articles_category(id) DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE ONLY articles_article ADD CONSTRAINT articles_article_pkey PRIMARY KEY (id);

Bugs and Limitations

Currently djschemadiff is limited to only supporting PostgreSQL 8.1, but only because that is the only DBMS that I use. Extending djschemadiff to support other databases (including different versions of PostgreSQL) should be fairly easy.

First, the sqlparse module would need to be altered so that it can understand the output of the new DBMS's dump tool. Then, the pgembed module would need to be altered (and probably renamed!) so that it can create/populate/dump/destroy an instance of the new database.

Contributing

If you find this tool useful or have any other comments, ideas for improvements or even patches, please let me know: send email to Sam Morris.

I use the Git version control system to manage the source code of djschemadiff. You can fetch a copy of the repository with the following command:

git-clone http://robots.org.uk/src/djschemadiff/djschemadiff.git

Copyright, License and Warranty

© 2007 Sam Morris <sam@robots.org.uk>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.