|Version 7 (modified by andrew, 4 years ago) (diff)|
South is mainly used via the console and its two important commands: migrate and startmigration.
The migrate command is used to control the migration of the system forwards or backwards through the series of migrations for any given app.
The most common use is:
./manage.py migrate myapp
This will migrate the app myapp forwards through all the migrations. If you want to migrate all the apps at once, run:
./manage.py migrate myapp
This has the same effect as calling the first example for every app, and will deal with Dependencies properly.
You can also specify a specific migration to migrate to:
./manage.py migrate myapp 0002_add_username
Note that, if the system has already migrated past the specified migration, it will roll back to it instead. If you want to migrate all the way back, specify the special migration name zero:
./manage.py migrate myapp zero
You can also just give prefixes of migrations, to save typing:
./manage.py migrate myapp 0002
But they must be unique:
$ ./manage.py migrate myapp 000 Running migrations for myapp: - Prefix 00 matches more than one migration: 0001_initial 0002_add_username
- --all: Used instead of an app name, allows you to migrate all applications to the same target. For example, ./manage.py migrate --all --fake 0001 if you are converting a lot of apps.
- --list: Shows what migrations are available, and puts a * next to ones which have been applied.
- --merge: Runs any missed (out-of-order) migrations without rolling back to them.
- --no-initial-data: Doesn't load in any initial data fixtures after a full upwards migration, if there are any.
- --fake: Records the migration sequence as having been applied, but doesn't actually run it. Useful for ConvertingAnApp.
- --db-dry-run: Loads and runs the migration, but doesn't actually access the database (the SQL generated is thrown away at the last minute). The migration is also not recorded as being run; this is useful for sanity-testing migrations to check API calls are correct.
South's migration system really comes into its own when you start getting conflicting migrations - that is, migrations that have been applied in the wrong sequence.
One example is if Anne writes new migrations 0003_foo and 0004_bar, runs the migration up to 0004 to make sure her local copy is up-to-date, and then updates her code from (say) Subversion. In the meantime, her coworker Bob has written a migration 0003_baz, which gets pulled in.
Now, there's a problem. 0003_phou should have been applied before 0004_bar, but it hasn't been; in this situation, South will helpfully say something like
Running migrations for aeblog: - Current migration: 5 (after 0004_bar) - Target migration: 5 (after 0004_bar) ! These migrations should have been applied already, but aren't: - 0003_phou ! Please re-run migrate with one of these switches: --skip: Ignore this migration mismatch and keep going --merge: Just apply the missing migrations out of order If you want to roll back to the first of these migrations and then roll forward, do: ./manage.py migrate --skip 0002_add_username ./manage.py migrate
As you can see, you have two real options; --merge, which will just apply the missing migration and continue, and the two commands which roll back to before the missing migration (using --skip to ignore the error we're dealing with) and then migrating properly, in order, from there to the end.
Using --skip by itself will let you continue, but isn't much of a solution; South will still complain the next time you run a migrate without --skip.
Sometimes, even worse things happen and South finds out that an applied migration has gone missing from the filesystem. In this scenario, it will politely tell you to go fix the problem yourself.
Initial Data and post_syncdb
South will load initial_data files in the same way as syncdb, but it loads them at the end of every successful migration process, so ensure they are kept up-to-date, along with the rest of your fixtures (something to help ease the pain of migrating fixtures may appear shortly in South).
South also sends the post_syncdb signal when a model's table is first created (this functionality requires that you generated those migrations with startmigration). This behaviour is intended to mirror the behaviour of syncdb, although for sanity reasons you may want to consider moving any setup code connected to such a signal into a migration.
While migrate is the real meat and bones of South, startmigration is by comparison an entirely optional extra. It's a utility to help write some of your migrations (specifically, the ones to create tables for new models) for you; if you like, you can ignore it and write everything youself, in which case we wish you good luck, and happy typing.
However, if you have a sense of reason, you'll realise that having the skeletons of migrations and the whole of model creation migrations created for you is really Quite Handy.
The main use of startmigration is when you've just finished your shiny new models.py and want to load up your database. In vanilla Django, you'd just run syncdb - however, with migrations, you'll need a migration to create the tables.
In this scenario, you just run:
./manage.py startmigration myapp --initial
That will write one big migration to create all the tables for the models in your app; just run ./manage.py migrate to get it in and you're done in only one more step than syncdb!
Later on, you'll add models to your app; to create migrations for individual models, simply run
./manage.py startmigration myapp two_new_models --model Foo --model Bar
Here, we create a migration called 0002_two_new_models (or a higher number - South picks the next available one) which will create the tables for the models Foo and Bar.
You can also create 'add field' migrations in 0.4 and up:
./manage.py startmigration mitest some_cols --add-field User.age --add-field User.profile --add-field Post.user
See the tutorial for more.
Finally, if you're writing a migration that South can't automatically create for you (yet!) then you can just create a skeleton:
./manage.py startmigration myapp my_new_column_migration
The skeleton looks something like this:
from south.db import db from myapp.models import * class Migration: def forwards(self): "Write your forwards migration here" def backwards(self): "Write your backwards migration here"
- --model: Generates a creation migration for the given modelname. Can be used more than once.
- --add-field: Generates an add-column migration for modelname.field.
- --add-index: Generates an add-index migration for modelname.field.
- --initial: Like having --model for every model in your app. You should use this for your first migration.
- --auto: Generates a migration with automatically-detected actions. Relies on having a previous migration with history information attached.
- --freeze: Use appname or appname.modelname to freeze additional models into the app.
- --stdout: Writes the migration to stdout instead of a file.