~canonical-django/canonical-django/project-template

« back to all changes in this revision

Viewing changes to trunk/python-packages/django/core/management/commands/flush.py

  • Committer: Matthew Nuzum
  • Date: 2008-11-13 05:46:03 UTC
  • Revision ID: matthew.nuzum@canonical.com-20081113054603-v0kvr6z6xyexvqt3
adding to version control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.core.management.base import NoArgsCommand, CommandError
 
2
from django.core.management.color import no_style
 
3
from optparse import make_option
 
4
 
 
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.'),
 
12
    )
 
13
    help = "Executes ``sqlflush`` on the current database."
 
14
 
 
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
 
19
 
 
20
        verbosity = int(options.get('verbosity', 1))
 
21
        interactive = options.get('interactive')
 
22
 
 
23
        self.style = no_style()
 
24
 
 
25
        # Import the 'management' module within each installed app, to register
 
26
        # dispatcher events.
 
27
        for app_name in settings.INSTALLED_APPS:
 
28
            try:
 
29
                __import__(app_name + '.management', {}, {}, [''])
 
30
            except ImportError:
 
31
                pass
 
32
 
 
33
        sql_list = sql_flush(self.style, only_django=True)
 
34
 
 
35
        if interactive:
 
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?
 
40
 
 
41
    Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME)
 
42
        else:
 
43
            confirm = 'yes'
 
44
 
 
45
        if confirm == 'yes':
 
46
            try:
 
47
                cursor = connection.cursor()
 
48
                for sql in sql_list:
 
49
                    cursor.execute(sql)
 
50
            except Exception, e:
 
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()
 
59
 
 
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)
 
64
 
 
65
            # Reinstall the initial_data fixture.
 
66
            from django.core.management import call_command
 
67
            call_command('loaddata', 'initial_data', **options)
 
68
 
 
69
        else:
 
70
            print "Flush cancelled."