2014-09-06 22:07:34 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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
|
2014-06-18 07:07:54 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management.color import no_style
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management.sql import emit_post_migrate_signal, sql_flush
|
2015-02-28 23:02:20 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections, transaction
|
2013-03-19 13:04:59 +08:00
|
|
|
from django.utils import six
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.six.moves import input
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
|
2014-06-18 07:07:54 +08:00
|
|
|
class Command(BaseCommand):
|
2014-06-09 10:30:15 +08:00
|
|
|
help = ('Removes ALL DATA from the database, including data added during '
|
2014-12-27 02:23:38 +08:00
|
|
|
'migrations. 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):
|
2015-09-07 21:17:55 +08:00
|
|
|
parser.add_argument('--noinput', '--no-input',
|
|
|
|
action='store_false', dest='interactive', default=True,
|
2014-06-07 04:39:33 +08:00
|
|
|
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.')
|
|
|
|
|
2014-06-18 07:07:54 +08:00
|
|
|
def handle(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"
|
2014-09-04 20:15:09 +08:00
|
|
|
"Hint: Look at the output of 'django-admin sqlflush'. "
|
|
|
|
"That's the SQL this command wasn't able to run.\n"
|
2013-03-19 13:04:59 +08:00
|
|
|
"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
|
|
|
|
2015-02-28 23:36:19 +08:00
|
|
|
# Empty sql_list may signify an empty database and post_migrate would then crash
|
|
|
|
if sql_list and not inhibit_post_migrate:
|
2015-02-28 23:02:20 +08:00
|
|
|
# Emit the post migrate signal. This allows individual applications to
|
|
|
|
# respond as if the database had been migrated from scratch.
|
|
|
|
emit_post_migrate_signal(verbosity, interactive, database)
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
2012-04-27 03:56:20 +08:00
|
|
|
self.stdout.write("Flush cancelled.\n")
|