Making schema changes

From OpenHatch wiki
Jump to navigation Jump to search

This is a page about improving or modifying OpenHatch.

We call that "Hacking OpenHatch," and there is a whole category of pages about that.


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

Making schema changes[edit]

  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.
  2. 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 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.
  3. Update the database schema by applying the contents of the migration file:
    ./manage.py migrate APPNAME
    
  4. Commit the model change and migration file.

Here's an example[edit]

  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)
    
  2. 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
    
  3. 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.
    
  4. Commit the changes to mysite/profile/models.py and mysite/profile/migrations/0088_auto__add_field_portfolioentry_receive_maintainer_notifications.py.