/django/core/management/commands/reset.py

https://github.com/blacktear23/django · Python · 63 lines · 54 code · 8 blank · 1 comment · 7 complexity · 4d1c981c022aabbce8449f560335ec99 MD5 · raw file

  1. from optparse import make_option
  2. from django.conf import settings
  3. from django.core.management.base import AppCommand, CommandError
  4. from django.core.management.color import no_style
  5. from django.core.management.sql import sql_reset
  6. from django.db import connections, transaction, DEFAULT_DB_ALIAS
  7. class Command(AppCommand):
  8. option_list = AppCommand.option_list + (
  9. make_option('--noinput', action='store_false', dest='interactive', default=True,
  10. help='Tells Django to NOT prompt the user for input of any kind.'),
  11. make_option('--database', action='store', dest='database',
  12. default=DEFAULT_DB_ALIAS, help='Nominates a database to reset. '
  13. 'Defaults to the "default" database.'),
  14. )
  15. help = "Executes ``sqlreset`` for the given app(s) in the current database."
  16. args = '[appname ...]'
  17. output_transaction = True
  18. def handle_app(self, app, **options):
  19. # This command breaks a lot and should be deprecated
  20. import warnings
  21. warnings.warn(
  22. 'This command has been deprecated. The command ``flush`` can be used to delete everything. You can also use ALTER TABLE or DROP TABLE statements manually.',
  23. PendingDeprecationWarning
  24. )
  25. using = options.get('database', DEFAULT_DB_ALIAS)
  26. connection = connections[using]
  27. app_name = app.__name__.split('.')[-2]
  28. self.style = no_style()
  29. sql_list = sql_reset(app, self.style, connection)
  30. if options.get('interactive'):
  31. confirm = raw_input("""
  32. You have requested a database reset.
  33. This will IRREVERSIBLY DESTROY any data for
  34. the "%s" application in the database "%s".
  35. Are you sure you want to do this?
  36. Type 'yes' to continue, or 'no' to cancel: """ % (app_name, connection.settings_dict['NAME']))
  37. else:
  38. confirm = 'yes'
  39. if confirm == 'yes':
  40. try:
  41. cursor = connection.cursor()
  42. for sql in sql_list:
  43. cursor.execute(sql)
  44. except Exception, e:
  45. transaction.rollback_unless_managed()
  46. raise CommandError("""Error: %s couldn't be reset. Possible reasons:
  47. * The database isn't running or isn't configured correctly.
  48. * At least one of the database tables doesn't exist.
  49. * The SQL was invalid.
  50. Hint: Look at the output of 'django-admin.py sqlreset %s'. That's the SQL this command wasn't able to run.
  51. The full error: %s""" % (app_name, app_name, e))
  52. transaction.commit_unless_managed()
  53. else:
  54. print "Reset cancelled."