1
from optparse import make_option
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
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.'),
11
21
help = "Executes ``sqlflush`` on the current database."
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
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')
28
36
except ImportError:
31
sql_list = sql_flush(self.style, only_django=True)
39
sql_list = sql_flush(self.style, connection, only_django=True)
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?
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'])
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)
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)
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)
68
77
print "Flush cancelled."