2013-03-19 13:04:59 +08:00
|
|
|
import sys
|
2013-07-29 21:50:58 +08:00
|
|
|
from importlib import import_module
|
2009-12-22 23:18:51 +08:00
|
|
|
from optparse import make_option
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-12-12 06:31:34 +08:00
|
|
|
from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.core.management import call_command
|
2007-08-16 22:34:01 +08:00
|
|
|
from django.core.management.base import NoArgsCommand, CommandError
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management.color import no_style
|
2013-07-30 18:52:52 +08:00
|
|
|
from django.core.management.sql import sql_flush, emit_post_migrate_signal
|
2012-08-09 01:08:05 +08:00
|
|
|
from django.utils.six.moves import input
|
2013-03-19 13:04:59 +08:00
|
|
|
from django.utils import six
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
class Command(NoArgsCommand):
|
2007-09-10 05:57:59 +08:00
|
|
|
option_list = NoArgsCommand.option_list + (
|
|
|
|
make_option('--noinput', action='store_false', dest='interactive', default=True,
|
|
|
|
help='Tells Django to NOT prompt the user for input of any kind.'),
|
2009-12-22 23:18:51 +08:00
|
|
|
make_option('--database', action='store', dest='database',
|
|
|
|
default=DEFAULT_DB_ALIAS, help='Nominates a database to flush. '
|
|
|
|
'Defaults to the "default" database.'),
|
2012-06-05 22:46:15 +08:00
|
|
|
make_option('--no-initial-data', action='store_false', dest='load_initial_data', default=True,
|
2013-05-18 06:18:35 +08:00
|
|
|
help='Tells Django not to load any initial data after database synchronization.'),
|
2007-09-10 05:57:59 +08:00
|
|
|
)
|
2011-09-22 13:21:51 +08:00
|
|
|
help = ('Returns the database to the state it was in immediately after '
|
2013-07-30 18:52:52 +08:00
|
|
|
'migrate was first executed. This means that all data will be removed '
|
|
|
|
'from the database, any post-migration handlers will be '
|
2011-09-22 13:21:51 +08:00
|
|
|
're-executed, and the initial_data fixture will be re-installed.')
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
def handle_noargs(self, **options):
|
2011-10-23 11:43:43 +08:00
|
|
|
db = options.get('database')
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[db]
|
2011-10-23 11:43:43 +08:00
|
|
|
verbosity = int(options.get('verbosity'))
|
2007-08-16 14:06:55 +08:00
|
|
|
interactive = options.get('interactive')
|
2013-06-12 04:56:09 +08:00
|
|
|
# The following are stealth options used by Django's internals.
|
2012-07-25 04:24:16 +08:00
|
|
|
reset_sequences = options.get('reset_sequences', True)
|
2013-06-10 03:04:36 +08:00
|
|
|
allow_cascade = options.get('allow_cascade', False)
|
2013-07-30 18:52:52 +08:00
|
|
|
inhibit_post_migrate = options.get('inhibit_post_migrate', False)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
self.style = no_style()
|
|
|
|
|
|
|
|
# Import the 'management' module within each installed app, to register
|
|
|
|
# dispatcher events.
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2007-08-16 14:06:55 +08:00
|
|
|
try:
|
2013-12-19 22:57:23 +08:00
|
|
|
import_module('.management', app_config.name)
|
2007-08-16 14:06:55 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2013-06-10 03:04:36 +08:00
|
|
|
sql_list = sql_flush(self.style, connection, only_django=True,
|
|
|
|
reset_sequences=reset_sequences,
|
|
|
|
allow_cascade=allow_cascade)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
if interactive:
|
2012-08-09 01:08:05 +08:00
|
|
|
confirm = input("""You have requested a flush of the database.
|
2007-08-16 14:06:55 +08:00
|
|
|
This will IRREVERSIBLY DESTROY all data currently in the %r database,
|
2013-07-30 18:52:52 +08:00
|
|
|
and return each table to a fresh state.
|
2007-08-16 14:06:55 +08:00
|
|
|
Are you sure you want to do this?
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME'])
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
confirm = 'yes'
|
|
|
|
|
|
|
|
if confirm == 'yes':
|
|
|
|
try:
|
2013-03-04 20:12:59 +08:00
|
|
|
with transaction.commit_on_success_unless_managed():
|
|
|
|
cursor = connection.cursor()
|
|
|
|
for sql in sql_list:
|
|
|
|
cursor.execute(sql)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2013-03-19 13:04:59 +08:00
|
|
|
new_msg = (
|
|
|
|
"Database %s couldn't be flushed. Possible reasons:\n"
|
|
|
|
" * The database isn't running or isn't configured correctly.\n"
|
|
|
|
" * At least one of the expected database tables doesn't exist.\n"
|
|
|
|
" * The SQL was invalid.\n"
|
|
|
|
"Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.\n"
|
|
|
|
"The full error: %s") % (connection.settings_dict['NAME'], e)
|
|
|
|
six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])
|
2013-06-12 04:56:09 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
if not inhibit_post_migrate:
|
|
|
|
self.emit_post_migrate(verbosity, interactive, db)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Reinstall the initial_data fixture.
|
2012-06-06 19:53:12 +08:00
|
|
|
if options.get('load_initial_data'):
|
|
|
|
# Reinstall the initial_data fixture.
|
2012-06-05 22:46:15 +08:00
|
|
|
call_command('loaddata', 'initial_data', **options)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
else:
|
2012-04-27 03:56:20 +08:00
|
|
|
self.stdout.write("Flush cancelled.\n")
|
2013-06-12 04:56:09 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2013-07-30 18:52:52 +08:00
|
|
|
def emit_post_migrate(verbosity, interactive, database):
|
|
|
|
# Emit the post migrate signal. This allows individual applications to
|
|
|
|
# respond as if the database had been migrated from scratch.
|
2013-06-12 04:56:09 +08:00
|
|
|
all_models = []
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs(only_with_models_module=True):
|
2013-12-14 17:08:44 +08:00
|
|
|
all_models.extend(router.get_migratable_models(app_config.models_module, database, include_auto_created=True))
|
2013-07-30 18:52:52 +08:00
|
|
|
emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
|