1
from django.core.management.base import NoArgsCommand, CommandError
2
from django.core.management.color import no_style
3
from optparse import make_option
5
class Command(NoArgsCommand):
6
option_list = NoArgsCommand.option_list + (
7
make_option('--verbosity', action='store', dest='verbosity', default='1',
8
type='choice', choices=['0', '1', '2'],
9
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
10
make_option('--noinput', action='store_false', dest='interactive', default=True,
11
help='Tells Django to NOT prompt the user for input of any kind.'),
13
help = "Executes ``sqlflush`` on the current database."
15
def handle_noargs(self, **options):
16
from django.conf import settings
17
from django.db import connection, transaction, models
18
from django.core.management.sql import sql_flush, emit_post_sync_signal
20
verbosity = int(options.get('verbosity', 1))
21
interactive = options.get('interactive')
23
self.style = no_style()
25
# Import the 'management' module within each installed app, to register
27
for app_name in settings.INSTALLED_APPS:
29
__import__(app_name + '.management', {}, {}, [''])
33
sql_list = sql_flush(self.style, only_django=True)
36
confirm = raw_input("""You have requested a flush of the database.
37
This will IRREVERSIBLY DESTROY all data currently in the %r database,
38
and return each table to the state it was in after syncdb.
39
Are you sure you want to do this?
41
Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME)
47
cursor = connection.cursor()
51
transaction.rollback_unless_managed()
52
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
53
* The database isn't running or isn't configured correctly.
54
* At least one of the expected database tables doesn't exist.
55
* The SQL was invalid.
56
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
57
The full error: %s""" % (settings.DATABASE_NAME, e))
58
transaction.commit_unless_managed()
60
# Emit the post sync signal. This allows individual
61
# applications to respond as if the database had been
62
# sync'd from scratch.
63
emit_post_sync_signal(models.get_models(), verbosity, interactive)
65
# Reinstall the initial_data fixture.
66
from django.core.management import call_command
67
call_command('loaddata', 'initial_data', **options)
70
print "Flush cancelled."