south.management.commands.convert_to_south: 48 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/south/management/commands/convert_to_south.py

Stats: 0 executed, 37 missed, 11 excluded, 45 ignored

  1. """
  2. Quick conversion command module.
  3. """
  4. from optparse import make_option
  5. import sys
  6. from django.core.management.base import BaseCommand
  7. from django.core.management.color import no_style
  8. from django.conf import settings
  9. from django.db import models
  10. from django.core import management
  11. from django.core.exceptions import ImproperlyConfigured
  12. from south.migration import Migrations
  13. from south.hacks import hacks
  14. from south.exceptions import NoMigrations
  15. class Command(BaseCommand):
  16. option_list = BaseCommand.option_list
  17. if '--verbosity' not in [opt.get_opt_string() for opt in BaseCommand.option_list]:
  18. option_list += (
  19. make_option('--verbosity', action='store', dest='verbosity', default='1',
  20. type='choice', choices=['0', '1', '2'],
  21. help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
  22. )
  23. option_list += (
  24. make_option('--delete-ghost-migrations', action='store_true', dest='delete_ghosts', default=False,
  25. help="Tells South to delete any 'ghost' migrations (ones in the database but not on disk)."),
  26. make_option('--ignore-ghost-migrations', action='store_true', dest='ignore_ghosts', default=False,
  27. help="Tells South to ignore any 'ghost' migrations (ones in the database but not on disk) and continue to apply new migrations."),
  28. )
  29. help = "Quickly converts the named application to use South if it is currently using syncdb."
  30. def handle(self, app=None, *args, **options):
  31. # Make sure we have an app
  32. if not app:
  33. print "Please specify an app to convert."
  34. return
  35. # See if the app exists
  36. app = app.split(".")[-1]
  37. try:
  38. app_module = models.get_app(app)
  39. except ImproperlyConfigured:
  40. print "There is no enabled application matching '%s'." % app
  41. return
  42. # Try to get its list of models
  43. model_list = models.get_models(app_module)
  44. if not model_list:
  45. print "This application has no models; this command is for applications that already have models syncdb'd."
  46. print "Make some models, and then use ./manage.py schemamigration %s --initial instead." % app
  47. return
  48. # Ask South if it thinks it's already got migrations
  49. try:
  50. Migrations(app)
  51. except NoMigrations:
  52. pass
  53. else:
  54. print "This application is already managed by South."
  55. return
  56. # Finally! It seems we've got a candidate, so do the two-command trick
  57. verbosity = int(options.get('verbosity', 0))
  58. management.call_command("schemamigration", app, initial=True, verbosity=verbosity)
  59. # Now, we need to re-clean and sanitise appcache
  60. hacks.clear_app_cache()
  61. hacks.repopulate_app_cache()
  62. # And also clear our cached Migration classes
  63. Migrations._clear_cache()
  64. # Now, migrate
  65. management.call_command(
  66. "migrate",
  67. app,
  68. "0001",
  69. fake=True,
  70. verbosity=verbosity,
  71. ignore_ghosts=options.get("ignore_ghosts", False),
  72. delete_ghosts=options.get("delete_ghosts", False),
  73. )
  74. print
  75. print "App '%s' converted. Note that South assumed the application's models matched the database" % app
  76. print "(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app
  77. print "directory, revert models.py so it matches the database, and try again."