| 1 |
from importlib import import_module |
| 2 |
|
| 3 |
from django.apps import apps |
| 4 |
from django.core.management.base import BaseCommand, CommandError |
| 5 |
from django.core.management.color import no_style |
| 6 |
from django.core.management.sql import emit_post_migrate_signal, sql_flush |
| 7 |
from django.db import DEFAULT_DB_ALIAS, connections |
| 8 |
|
| 9 |
|
| 10 |
class Command(BaseCommand): |
| 11 |
help = ( |
| 12 |
"Removes ALL DATA from the database, including data added during " |
| 13 |
'migrations. Does not achieve a "fresh install" state.' |
| 14 |
) |
| 15 |
stealth_options = ("reset_sequences", "allow_cascade", "inhibit_post_migrate") |
| 16 |
|
| 17 |
def add_arguments(self, parser): |
| 18 |
parser.add_argument( |
| 19 |
"--noinput", |
| 20 |
"--no-input", |
| 21 |
action="store_false", |
| 22 |
dest="interactive", |
| 23 |
help="Tells Django to NOT prompt the user for input of any kind.", |
| 24 |
) |
| 25 |
parser.add_argument( |
| 26 |
"--database", |
| 27 |
default=DEFAULT_DB_ALIAS, |
| 28 |
choices=tuple(connections), |
| 29 |
help='Nominates a database to flush. Defaults to the "default" database.', |
| 30 |
) |
| 31 |
|
| 32 |
def handle(self, **options): |
| 33 |
database = options["database"] |
| 34 |
connection = connections[database] |
| 35 |
verbosity = options["verbosity"] |
| 36 |
interactive = options["interactive"] |
| 37 |
# The following are stealth options used by Django's internals. |
| 38 |
reset_sequences = options.get("reset_sequences", True) |
| 39 |
allow_cascade = options.get("allow_cascade", False) |
| 40 |
inhibit_post_migrate = options.get("inhibit_post_migrate", False) |
| 41 |
|
| 42 |
self.style = no_style() |
| 43 |
|
| 44 |
# Import the 'management' module within each installed app, to register |
| 45 |
# dispatcher events. |
| 46 |
for app_config in apps.get_app_configs(): |
| 47 |
try: |
| 48 |
import_module(".management", app_config.name) |
| 49 |
except ImportError: |
| 50 |
pass |
| 51 |
|
| 52 |
sql_list = sql_flush( |
| 53 |
self.style, |
| 54 |
connection, |
| 55 |
reset_sequences=reset_sequences, |
| 56 |
allow_cascade=allow_cascade, |
| 57 |
) |
| 58 |
|
| 59 |
if interactive: |
| 60 |
confirm = input("""You have requested a flush of the database. |
| 61 |
This will IRREVERSIBLY DESTROY all data currently in the "%s" database, |
| 62 |
and return each table to an empty state. |
| 63 |
Are you sure you want to do this? |
| 64 |
|
| 65 |
Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict["NAME"]) |
| 66 |
else: |
| 67 |
confirm = "yes" |
| 68 |
|
| 69 |
if confirm == "yes": |
| 70 |
try: |
| 71 |
connection.ops.execute_sql_flush(sql_list) |
| 72 |
except Exception as exc: |
| 73 |
raise CommandError( |
| 74 |
"Database %s couldn't be flushed. Possible reasons:\n" |
| 75 |
" * The database isn't running or isn't configured correctly.\n" |
| 76 |
" * At least one of the expected database tables doesn't exist.\n" |
| 77 |
" * The SQL was invalid.\n" |
| 78 |
"Hint: Look at the output of 'django-admin sqlflush'. " |
| 79 |
"That's the SQL this command wasn't able to run." |
| 80 |
% (connection.settings_dict["NAME"],) |
| 81 |
) from exc |
| 82 |
|
| 83 |
# Empty sql_list may signify an empty database and post_migrate |
| 84 |
# would then crash. |
| 85 |
if sql_list and not inhibit_post_migrate: |
| 86 |
# Emit the post migrate signal. This allows individual applications to |
| 87 |
# respond as if the database had been migrated from scratch. |
| 88 |
emit_post_migrate_signal(verbosity, interactive, database) |
| 89 |
else: |
| 90 |
self.stdout.write("Flush cancelled.") |