Making schema changes

This document describes how to make database schema changes to OpenHatch in Django using South.

Making schema changes
 Make the schema change you want to the relevant Django model (or add a model). An example model file is .

Use South's schema migration facilities to calculate the differences between the known schema and your updated schema. Run:

./manage.py schemamigration APPNAME --auto

where APPNAME is the  app in which you've made the schema change. Example appnames are "profile", "customs", and "account", which correspond to app directories within.

This command produces a migration file within the  subdirectory of the app's directory, of the form , where   is the number of the migration for this app.

Update the database schema by applying the contents of the migration file:

./manage.py migrate APPNAME 

Commit the model change and migration file. 

Here's an example
 Make the schema change in the model file:

--- a/mysite/profile/models.py +++ b/mysite/profile/models.py @@ -650,6 +650,7 @@ class PortfolioEntry(models.Model): is_archived = models.BooleanField(default=False) sort_order = models.IntegerField(default=0) use_my_description = models.BooleanField(default=True, verbose_name='') +   receive_maintainer_notifications = models.BooleanField(default=True) 

Generate the migration file:

oh-mainline$ ./manage.py schemamigration profile --auto + Added field receive_maintainer_notifications on profile.PortfolioEntry Created 0088_auto__add_field_portfolioentry_receive_maintainer_notifications.py. You can now apply this migration with: ./manage.py migrate profile 

Apply the schema change:

oh-mainline$ ./manage.py migrate profile Running migrations for profile: - Migrating forwards to 0088_add_field_portfolioentry_receive_maintainer_notifications. > profile:0088_add_field_portfolioentry_receive_maintainer_notifications 2011-02-20 19:50:14,498 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ADD COLUMN `receive_maintainer_notifications` bool NOT NULL DEFAULT True;" with params "[]" 2011-02-20 19:50:14,499 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ;" with params "[]" 2011-02-20 19:50:14,500 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ALTER COLUMN `receive_maintainer_notifications` DROP DEFAULT;" with params "[]" 2011-02-20 19:50:14,501 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` MODIFY `receive_maintainer_notifications` bool NOT NULL;;" with params "[]" 2011-02-20 19:50:14,502 execute:129 DEBUG   south execute "SET FOREIGN_KEY_CHECKS=1;" with params "[]" 2011-02-20 19:50:14,504 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ADD COLUMN `receive_maintainer_notifications` bool NOT NULL DEFAULT True;" with params "[]" 2011-02-20 19:50:14,634 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ;" with params "[]" 2011-02-20 19:50:14,636 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` ALTER COLUMN `receive_maintainer_notifications` DROP DEFAULT;" with params "[]" 2011-02-20 19:50:14,699 execute:129 DEBUG   south execute "ALTER TABLE `profile_portfolioentry` MODIFY `receive_maintainer_notifications` bool NOT NULL;;" with params "[]" - Loading initial data for profile. No fixtures found. </li>

Commit the changes to  and  .</li> </ol>