Révision 87131a80
Ajouté par Thomas Noël il y a plus de 9 ans
entrouvert/djommon/management/commands/safemigrate.py | ||
---|---|---|
1 |
from django.core.management.base import NoArgsCommand |
|
2 |
from django.db import connection, models |
|
3 |
from south.models import MigrationHistory |
|
4 |
|
|
5 |
from django.core.management.commands.syncdb import Command as DjangoSyncdbCommand |
|
6 |
from south.management.commands.syncdb import Command as SouthSyncdbCommand |
|
7 |
from south.management.commands.migrate import Command as SouthMigrateCommand |
|
8 |
|
|
9 |
|
|
10 |
class Command(NoArgsCommand): |
|
11 |
option_list = DjangoSyncdbCommand.option_list |
|
12 |
help = "syncdb and migrate the project, migrate '--fake app 0001' each app that has a new initial migration" |
|
13 |
|
|
14 |
def handle_noargs(self, *args, **options): |
|
15 |
verbosity = int(options['verbosity']) |
|
16 |
# step 1 : syncdb, create all apps without migrations |
|
17 |
SouthSyncdbCommand().execute(migrate_all=False, migrate=False, **options) |
|
18 |
# step 2 : detect and "fake 0001" all installed apps that had never |
|
19 |
# migrated (applications in database but not in south history) |
|
20 |
if verbosity > 0: |
|
21 |
|
|
22 |
self.fake_if_needed(verbosity) |
|
23 |
# step 3 : migrate |
|
24 |
if verbosity > 0: |
|
25 |
|
|
26 |
SouthMigrateCommand().execute(**options) |
|
27 |
|
|
28 |
def fake_if_needed(self, verbosity=1): |
|
29 |
# detect installed models |
|
30 |
# (code borrowed from django syncdb command) |
|
31 |
tables = connection.introspection.table_names() |
|
32 |
def model_in_database(model): |
|
33 |
opts = model._meta |
|
34 |
converter = connection.introspection.table_name_converter |
|
35 |
return (converter(opts.db_table) in tables) or \ |
|
36 |
(opts.auto_created and converter(opts.auto_created._meta.db_table) in tables) |
|
37 |
|
|
38 |
# list all applications with south migration |
|
39 |
# (code borrowed from south migrate command) |
|
40 |
from south import migration |
|
41 |
apps = list(migration.all_migrations()) |
|
42 |
applied_migrations = MigrationHistory.objects.filter(app_name__in=[app.app_label() for app in apps]) |
|
43 |
applied_migrations_lookup = dict(('%s.%s' % (mi.app_name, mi.migration), mi) for mi in applied_migrations) |
|
44 |
|
|
45 |
if verbosity > 0: |
|
46 |
print 'Status after syncdb:' |
|
47 |
for app in apps: |
|
48 |
# for each app with migrations, list already applied migrations |
|
49 |
applied_migrations = [] |
|
50 |
for migration in app: |
|
51 |
full_name = migration.app_label() + "." + migration.name() |
|
52 |
if full_name in applied_migrations_lookup: |
|
53 |
applied_migration = applied_migrations_lookup[full_name] |
|
54 |
if applied_migration.applied: |
|
55 |
applied_migrations.append(migration.name()) |
|
56 |
# try all models in database, if there none, the application is new |
|
57 |
# (because south syncdb does not create any tables) |
|
58 |
new = True |
|
59 |
for m in models.get_models(app.get_application().models): |
|
60 |
if model_in_database(m): |
|
61 |
new = False |
|
62 |
break |
|
63 |
if new: |
|
64 |
status = 'application-is-new' |
|
65 |
elif not applied_migrations: |
|
66 |
status = 'migration-is-new' |
|
67 |
else: |
|
68 |
status = 'normal' |
|
69 |
|
|
70 |
if verbosity > 0: |
|
71 |
print ' - %s: %s' % (app.app_label(), status) |
|
72 |
if status == 'migration-is-new': |
|
73 |
if verbosity > 0: |
|
74 |
print ' need fake migration to 0001_initial' |
|
75 |
# migrate --fake gdc 0001 |
|
76 |
SouthMigrateCommand().execute(app=app.app_label(), target='0001', fake=True, verbosity=verbosity) |
entrouvert/djommon/safemigrate/management/commands/safemigrate.py | ||
---|---|---|
1 |
from django.core.management.base import NoArgsCommand |
|
2 |
from django.db import connection, models |
|
3 |
from south.models import MigrationHistory |
|
4 |
|
|
5 |
from django.core.management.commands.syncdb import Command as DjangoSyncdbCommand |
|
6 |
from south.management.commands.syncdb import Command as SouthSyncdbCommand |
|
7 |
from south.management.commands.migrate import Command as SouthMigrateCommand |
|
8 |
|
|
9 |
|
|
10 |
class Command(NoArgsCommand): |
|
11 |
option_list = DjangoSyncdbCommand.option_list |
|
12 |
help = "syncdb and migrate the project, migrate '--fake app 0001' each app that has a new initial migration" |
|
13 |
|
|
14 |
def handle_noargs(self, *args, **options): |
|
15 |
verbosity = int(options['verbosity']) |
|
16 |
# step 1 : syncdb, create all apps without migrations |
|
17 |
SouthSyncdbCommand().execute(migrate_all=False, migrate=False, **options) |
|
18 |
# step 2 : detect and "fake 0001" all installed apps that had never |
|
19 |
# migrated (applications in database but not in south history) |
|
20 |
if verbosity > 0: |
|
21 |
|
|
22 |
self.fake_if_needed(verbosity) |
|
23 |
# step 3 : migrate |
|
24 |
if verbosity > 0: |
|
25 |
|
|
26 |
SouthMigrateCommand().execute(**options) |
|
27 |
|
|
28 |
def fake_if_needed(self, verbosity=1): |
|
29 |
# detect installed models |
|
30 |
# (code borrowed from django syncdb command) |
|
31 |
tables = connection.introspection.table_names() |
|
32 |
def model_in_database(model): |
|
33 |
opts = model._meta |
|
34 |
converter = connection.introspection.table_name_converter |
|
35 |
return (converter(opts.db_table) in tables) or \ |
|
36 |
(opts.auto_created and converter(opts.auto_created._meta.db_table) in tables) |
|
37 |
|
|
38 |
# list all applications with south migration |
|
39 |
# (code borrowed from south migrate command) |
|
40 |
from south import migration |
|
41 |
apps = list(migration.all_migrations()) |
|
42 |
applied_migrations = MigrationHistory.objects.filter(app_name__in=[app.app_label() for app in apps]) |
|
43 |
applied_migrations_lookup = dict(('%s.%s' % (mi.app_name, mi.migration), mi) for mi in applied_migrations) |
|
44 |
|
|
45 |
if verbosity > 0: |
|
46 |
print 'Status after syncdb:' |
|
47 |
for app in apps: |
|
48 |
# for each app with migrations, list already applied migrations |
|
49 |
applied_migrations = [] |
|
50 |
for migration in app: |
|
51 |
full_name = migration.app_label() + "." + migration.name() |
|
52 |
if full_name in applied_migrations_lookup: |
|
53 |
applied_migration = applied_migrations_lookup[full_name] |
|
54 |
if applied_migration.applied: |
|
55 |
applied_migrations.append(migration.name()) |
|
56 |
# try all models in database, if there none, the application is new |
|
57 |
# (because south syncdb does not create any tables) |
|
58 |
new = True |
|
59 |
for m in models.get_models(app.get_application().models): |
|
60 |
if model_in_database(m): |
|
61 |
new = False |
|
62 |
break |
|
63 |
if new: |
|
64 |
status = 'application-is-new' |
|
65 |
elif not applied_migrations: |
|
66 |
status = 'migration-is-new' |
|
67 |
else: |
|
68 |
status = 'normal' |
|
69 |
|
|
70 |
if verbosity > 0: |
|
71 |
print ' - %s: %s' % (app.app_label(), status) |
|
72 |
if status == 'migration-is-new': |
|
73 |
if verbosity > 0: |
|
74 |
print ' need fake migration to 0001_initial' |
|
75 |
# migrate --fake gdc 0001 |
|
76 |
SouthMigrateCommand().execute(app=app.app_label(), target='0001', fake=True, verbosity=verbosity) |
Formats disponibles : Unified diff
move safemigrate in entrouvert.djommon app (#5781)
entrouvert.djommon.safemigrate app was just superfluous.