Making schema changes

From OpenHatch wiki
Revision as of 01:10, 21 February 2011 by imported>Jesstess (Created page with '# Make the schema change you want to the relevant Django model (or add a model). An example model file is <code>mysite/profile/models.py</code>. # Use South's schema migration f…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  1. Make the schema change you want to the relevant Django model (or add a model). An example model file is mysite/profile/models.py.
  1. Use South's schema migration facilities to calculate the differences between the known schema and your updated schema. Run:
bin/mysite schemamigration APPNAME --auto

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

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

  1. Update the database schema by applying the contents of the migration file:
bin/mysite migrate APPNAME
  1. Commit the model change and migration file.

Here's an example

  1. 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)
  1. Generate the migration file:
oh-mainline$ bin/mysite 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
  1. Apply the schema change:
oh-mainline$ bin/mysite 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.