You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flush.py 3.5KB

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