~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/core/management/commands/flush.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
6
from django.core.management.base import NoArgsCommand, CommandError
2
7
from django.core.management.color import no_style
 
8
from django.core.management.sql import sql_flush, emit_post_sync_signal
3
9
from django.utils.importlib import import_module
4
 
from optparse import make_option
 
10
 
 
11
 
5
12
 
6
13
class Command(NoArgsCommand):
7
14
    option_list = NoArgsCommand.option_list + (
8
15
        make_option('--noinput', action='store_false', dest='interactive', default=True,
9
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
21
    help = "Executes ``sqlflush`` on the current database."
12
22
 
13
23
    def handle_noargs(self, **options):
14
 
        from django.conf import settings
15
 
        from django.db import connection, transaction, models
16
 
        from django.core.management.sql import sql_flush, emit_post_sync_signal
17
 
 
 
24
        db = options.get('database', DEFAULT_DB_ALIAS)
 
25
        connection = connections[db]
18
26
        verbosity = int(options.get('verbosity', 1))
19
27
        interactive = options.get('interactive')
20
28
 
28
36
            except ImportError:
29
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
41
        if interactive:
34
42
            confirm = raw_input("""You have requested a flush of the database.
36
44
and return each table to the state it was in after syncdb.
37
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
48
        else:
41
49
            confirm = 'yes'
42
50
 
46
54
                for sql in sql_list:
47
55
                    cursor.execute(sql)
48
56
            except Exception, e:
49
 
                transaction.rollback_unless_managed()
 
57
                transaction.rollback_unless_managed(using=db)
50
58
                raise CommandError("""Database %s couldn't be flushed. Possible reasons:
51
 
      * The database isn't running or isn't configured correctly.
52
 
      * At least one of the expected database tables doesn't exist.
53
 
      * 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.
55
 
    The full error: %s""" % (settings.DATABASE_NAME, e))
56
 
            transaction.commit_unless_managed()
 
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)
57
65
 
58
66
            # Emit the post sync signal. This allows individual
59
67
            # applications to respond as if the database had been
60
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
71
            # Reinstall the initial_data fixture.
64
 
            from django.core.management import call_command
65
 
            call_command('loaddata', 'initial_data', **options)
 
72
            kwargs = options.copy()
 
73
            kwargs['database'] = db
 
74
            call_command('loaddata', 'initial_data', **kwargs)
66
75
 
67
76
        else:
68
77
            print "Flush cancelled."