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
|
|
|
|
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):
|
2014-06-09 10:30:15 +08:00
|
|
|
help = ('Removes ALL DATA from the database, including data added during '
|
|
|
|
'migrations. Unmigrated apps will also have their initial_data '
|
|
|
|
'fixture reloaded. Does not achieve a "fresh install" state.')
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('--noinput', action='store_false', dest='interactive', default=True,
|
|
|
|
help='Tells Django to NOT prompt the user for input of any kind.')
|
|
|
|
parser.add_argument('--database', action='store', dest='database',
|
|
|
|
default=DEFAULT_DB_ALIAS,
|
|
|
|
help='Nominates a database to flush. Defaults to the "default" database.')
|
|
|
|
parser.add_argument('--no-initial-data', action='store_false',
|
|
|
|
dest='load_initial_data', default=True,
|
|
|
|
help='Tells Django not to load any initial data after database synchronization.')
|
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
def handle_noargs(self, **options):
|
2014-03-22 21:36:01 +08:00
|
|
|
database = options.get('database')
|
|
|
|
connection = connections[database]
|
2014-06-07 04:39:33 +08:00
|
|
|
verbosity = 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,
|
2014-06-09 10:30:15 +08:00
|
|
|
and return each table to an empty 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:
|
2014-03-22 21:36:01 +08:00
|
|
|
with transaction.atomic(using=database,
|
|
|
|
savepoint=connection.features.can_rollback_ddl):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as 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:
|
2014-03-22 21:36:01 +08:00
|
|
|
self.emit_post_migrate(verbosity, interactive, database)
|
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-31 06:53:54 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2013-12-30 04:21:23 +08:00
|
|
|
all_models.extend(router.get_migratable_models(app_config, database, include_auto_created=True))
|
2013-07-30 18:52:52 +08:00
|
|
|
emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
|