Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import glob
  2. import os
  3.  
  4. from django.conf import settings
  5. from django.core.management import call_command
  6. from django.core.management.base import BaseCommand, CommandError
  7.  
  8.  
  9. class Command(BaseCommand):
  10. """A command to remove all migrations from django apps.
  11. """
  12. help = 'Remove all previous migrations \
  13. (from and app or all apps) and recreate them.'
  14.  
  15. def add_arguments(self, parser):
  16. """Add optional argument to specify which app is being reset.
  17. """
  18. parser.add_argument(
  19. 'app_module_name',
  20. nargs='+',
  21. type=str,
  22. default=None
  23. )
  24.  
  25. def handle(self, *args, **options):
  26. """Called when the command in run from the manager.
  27. """
  28. app_names = options.get('app_module_name')
  29.  
  30. try:
  31. if app_names:
  32. for app_name in options['app_module_name']:
  33. self.clear_migrations_for_app(app_name)
  34. else:
  35. self.clear_all_migrations()
  36. except BaseExcetion as e:
  37. raise e from None
  38. raise CommandError('Failed to reset migrations!')
  39.  
  40. def clear_migrations_for_app(self, app_name):
  41. """Clearmigrations for a certain app.
  42. """
  43. migration_files = glob.iglob(
  44. os.path.join(settings.BASE_DIR, f'{app_name}/migrations/[!__]*.py')
  45. )
  46.  
  47. [os.remove(filename) for filename in migration_files]
  48.  
  49. call_command('makemigrations', app_name, verbosity=3)
  50.  
  51. def clear_all_migrations(self):
  52. """Clear migrations for all apps in the project.
  53. """
  54. migration_files = glob.iglob(
  55. os.path.join(settings.BASE_DIR, '**/migrations/[!__]*.py')
  56. )
  57.  
  58. [os.remove(filename) for filename in migration_files]
  59.  
  60. call_command('makemigrations', verbosity=3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement