flush.py 4.9 KB

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