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