|
1 from optparse import make_option |
|
2 |
|
3 from django.conf import settings |
|
4 from django.db import connections, transaction, models, DEFAULT_DB_ALIAS |
|
5 from django.core.management import call_command |
1 from django.core.management.base import NoArgsCommand, CommandError |
6 from django.core.management.base import NoArgsCommand, CommandError |
2 from django.core.management.color import no_style |
7 from django.core.management.color import no_style |
|
8 from django.core.management.sql import sql_flush, emit_post_sync_signal |
3 from django.utils.importlib import import_module |
9 from django.utils.importlib import import_module |
4 from optparse import make_option |
10 |
|
11 |
5 |
12 |
6 class Command(NoArgsCommand): |
13 class Command(NoArgsCommand): |
7 option_list = NoArgsCommand.option_list + ( |
14 option_list = NoArgsCommand.option_list + ( |
8 make_option('--noinput', action='store_false', dest='interactive', default=True, |
15 make_option('--noinput', action='store_false', dest='interactive', default=True, |
9 help='Tells Django to NOT prompt the user for input of any kind.'), |
16 help='Tells Django to NOT prompt the user for input of any kind.'), |
|
17 make_option('--database', action='store', dest='database', |
|
18 default=DEFAULT_DB_ALIAS, help='Nominates a database to flush. ' |
|
19 'Defaults to the "default" database.'), |
10 ) |
20 ) |
11 help = "Executes ``sqlflush`` on the current database." |
21 help = "Executes ``sqlflush`` on the current database." |
12 |
22 |
13 def handle_noargs(self, **options): |
23 def handle_noargs(self, **options): |
14 from django.conf import settings |
24 db = options.get('database', DEFAULT_DB_ALIAS) |
15 from django.db import connection, transaction, models |
25 connection = connections[db] |
16 from django.core.management.sql import sql_flush, emit_post_sync_signal |
|
17 |
|
18 verbosity = int(options.get('verbosity', 1)) |
26 verbosity = int(options.get('verbosity', 1)) |
19 interactive = options.get('interactive') |
27 interactive = options.get('interactive') |
20 |
28 |
21 self.style = no_style() |
29 self.style = no_style() |
22 |
30 |
26 try: |
34 try: |
27 import_module('.management', app_name) |
35 import_module('.management', app_name) |
28 except ImportError: |
36 except ImportError: |
29 pass |
37 pass |
30 |
38 |
31 sql_list = sql_flush(self.style, only_django=True) |
39 sql_list = sql_flush(self.style, connection, only_django=True) |
32 |
40 |
33 if interactive: |
41 if interactive: |
34 confirm = raw_input("""You have requested a flush of the database. |
42 confirm = raw_input("""You have requested a flush of the database. |
35 This will IRREVERSIBLY DESTROY all data currently in the %r database, |
43 This will IRREVERSIBLY DESTROY all data currently in the %r database, |
36 and return each table to the state it was in after syncdb. |
44 and return each table to the state it was in after syncdb. |
37 Are you sure you want to do this? |
45 Are you sure you want to do this? |
38 |
46 |
39 Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME) |
47 Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME']) |
40 else: |
48 else: |
41 confirm = 'yes' |
49 confirm = 'yes' |
42 |
50 |
43 if confirm == 'yes': |
51 if confirm == 'yes': |
44 try: |
52 try: |
45 cursor = connection.cursor() |
53 cursor = connection.cursor() |
46 for sql in sql_list: |
54 for sql in sql_list: |
47 cursor.execute(sql) |
55 cursor.execute(sql) |
48 except Exception, e: |
56 except Exception, e: |
49 transaction.rollback_unless_managed() |
57 transaction.rollback_unless_managed(using=db) |
50 raise CommandError("""Database %s couldn't be flushed. Possible reasons: |
58 raise CommandError("""Database %s couldn't be flushed. Possible reasons: |
51 * The database isn't running or isn't configured correctly. |
59 * The database isn't running or isn't configured correctly. |
52 * At least one of the expected database tables doesn't exist. |
60 * At least one of the expected database tables doesn't exist. |
53 * The SQL was invalid. |
61 * The SQL was invalid. |
54 Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. |
62 Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. |
55 The full error: %s""" % (settings.DATABASE_NAME, e)) |
63 The full error: %s""" % (connection.settings_dict['NAME'], e)) |
56 transaction.commit_unless_managed() |
64 transaction.commit_unless_managed(using=db) |
57 |
65 |
58 # Emit the post sync signal. This allows individual |
66 # Emit the post sync signal. This allows individual |
59 # applications to respond as if the database had been |
67 # applications to respond as if the database had been |
60 # sync'd from scratch. |
68 # sync'd from scratch. |
61 emit_post_sync_signal(models.get_models(), verbosity, interactive) |
69 emit_post_sync_signal(models.get_models(), verbosity, interactive, db) |
62 |
70 |
63 # Reinstall the initial_data fixture. |
71 # Reinstall the initial_data fixture. |
64 from django.core.management import call_command |
72 kwargs = options.copy() |
65 call_command('loaddata', 'initial_data', **options) |
73 kwargs['database'] = db |
|
74 call_command('loaddata', 'initial_data', **kwargs) |
66 |
75 |
67 else: |
76 else: |
68 print "Flush cancelled." |
77 print "Flush cancelled." |