andrewgodwin / south (http://south.aeracode.org/)
Migrations for Django
Clone this repository (size: 1.5 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/andrewgodwin/south/
| commit 605: | a29e90b115df |
| parent 604: | 1d801ff38f8a |
| branch: | default |
Add --database option to migrate.
Changed (Δ710 bytes):
raw changeset »
south/management/commands/migrate.py (11 lines added, 4 lines removed)
south/migration/__init__.py (7 lines added, 2 lines removed)
Up to file-list south/management/commands/migrate.py:
| … | … | @@ -14,6 +14,7 @@ from south import migration |
14 |
14 |
from south.migration import Migration, Migrations |
15 |
15 |
from south.migration.utils import get_app_label |
16 |
16 |
from south.exceptions import NoMigrations |
17 |
from south.db import DEFAULT_DB_ALIAS |
|
17 |
18 |
|
18 |
19 |
class Command(BaseCommand): |
19 |
20 |
option_list = BaseCommand.option_list + ( |
| … | … | @@ -31,6 +32,9 @@ class Command(BaseCommand): |
31 |
32 |
help="Pretends to do the migrations, but doesn't actually execute them."), |
32 |
33 |
make_option('--db-dry-run', action='store_true', dest='db_dry_run', default=False, |
33 |
34 |
help="Doesn't execute the SQL generated by the db methods, and doesn't store a record that the migration(s) occurred. Useful to test migrations before applying them."), |
35 |
make_option('--database', action='store', dest='database', |
|
36 |
default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' |
|
37 |
'Defaults to the "default" database.'), |
|
34 |
38 |
) |
35 |
39 |
if '--verbosity' not in [opt.get_opt_string() for opt in BaseCommand.option_list]: |
36 |
40 |
option_list += ( |
| … | … | @@ -39,9 +43,9 @@ class Command(BaseCommand): |
39 |
43 |
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), |
40 |
44 |
) |
41 |
45 |
help = "Runs migrations for all apps." |
42 |
args = "[appname] [migrationname|zero] [--all] [--list] [--skip] [--merge] [--no-initial-data] [--fake] [--db-dry-run] |
|
46 |
args = "[appname] [migrationname|zero] [--all] [--list] [--skip] [--merge] [--no-initial-data] [--fake] [--db-dry-run] [--database=dbalias]" |
|
43 |
47 |
|
44 |
def handle(self, app=None, target=None, skip=False, merge=False, backwards=False, fake=False, db_dry_run=False, show_list=False, |
|
48 |
def handle(self, app=None, target=None, skip=False, merge=False, backwards=False, fake=False, db_dry_run=False, show_list=False, database=DEFAULT_DB_ALIAS, **options): |
|
45 |
49 |
|
46 |
50 |
# Work out what the resolve mode is |
47 |
51 |
resolve_mode = merge and "merge" or (skip and "skip" or None) |
| … | … | @@ -78,7 +82,7 @@ class Command(BaseCommand): |
78 |
82 |
|
79 |
83 |
# Do we need to show the list of migrations? |
80 |
84 |
if show_list and apps: |
81 |
list_migrations(apps |
|
85 |
list_migrations(apps, database) |
|
82 |
86 |
|
83 |
87 |
if not show_list: |
84 |
88 |
|
| … | … | @@ -92,18 +96,21 @@ class Command(BaseCommand): |
92 |
96 |
verbosity = int(options.get('verbosity', 0)), |
93 |
97 |
load_initial_data = not options.get('no_initial_data', False), |
94 |
98 |
skip = skip, |
99 |
database = database, |
|
95 |
100 |
) |
96 |
101 |
if result is False: |
97 |
102 |
sys.exit(1) # Migration failed, so the command fails. |
98 |
103 |
|
99 |
104 |
|
100 |
def list_migrations(apps |
|
105 |
def list_migrations(apps, database = DEFAULT_DB_ALIAS): |
|
101 |
106 |
""" |
102 |
107 |
Prints a list of all available migrations, and which ones are currently applied. |
103 |
108 |
Accepts a list of Migrations instances. |
104 |
109 |
""" |
105 |
110 |
from south.models import MigrationHistory |
106 |
111 |
applied_migrations = MigrationHistory.objects.filter(app_name__in=[app.app_label() for app in apps]) |
112 |
if database != DEFAULT_DB_ALIAS: |
|
113 |
applied_migrations = applied_migrations.using(database) |
|
107 |
114 |
applied_migrations = ['%s.%s' % (mi.app_name,mi.migration) for mi in applied_migrations] |
108 |
115 |
|
109 |
116 |
Up to file-list south/migration/__init__.py:
| … | … | @@ -6,9 +6,10 @@ import sys |
6 |
6 |
|
7 |
7 |
from django.core.exceptions import ImproperlyConfigured |
8 |
8 |
|
9 |
import south.db |
|
9 |
10 |
from south import exceptions |
10 |
11 |
from south.models import MigrationHistory |
11 |
from south.db import db |
|
12 |
from south.db import db, DEFAULT_DB_ALIAS |
|
12 |
13 |
from south.migration.migrators import (Backwards, Forwards, |
13 |
14 |
DryRunMigrator, FakeMigrator, |
14 |
15 |
LoadInitialDataMigrator) |
| … | … | @@ -128,7 +129,7 @@ def get_migrator(direction, db_dry_run, |
128 |
129 |
direction = LoadInitialDataMigrator(migrator=direction) |
129 |
130 |
return direction |
130 |
131 |
|
131 |
def migrate_app(migrations, target_name=None, merge=False, fake=False, db_dry_run=False, yes=False, verbosity=0, load_initial_data=False, skip=False |
|
132 |
def migrate_app(migrations, target_name=None, merge=False, fake=False, db_dry_run=False, yes=False, verbosity=0, load_initial_data=False, skip=False, database=DEFAULT_DB_ALIAS): |
|
132 |
133 |
app_label = migrations.app_label() |
133 |
134 |
|
134 |
135 |
verbosity = int(verbosity) |
| … | … | @@ -143,6 +144,10 @@ def migrate_app(migrations, target_name= |
143 |
144 |
|
144 |
145 |
# Check there's no strange ones in the database |
145 |
146 |
applied = MigrationHistory.objects.filter(applied__isnull=False) |
147 |
# If we're using a different database, use that |
|
148 |
if database != DEFAULT_DB_ALIAS: |
|
149 |
applied = applied.using(database) |
|
150 |
south.db.db = south.db.dbs[database] |
|
146 |
151 |
applied = check_migration_histories(applied) |
147 |
152 |
|
148 |
153 |
# Guess the target_name |
