south.management.commands.syncdb: 66 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/syncdb.py

Stats: 0 executed, 51 missed, 15 excluded, 45 ignored

  1. """
  2. Overridden syncdb command
  3. """
  4. import sys
  5. from optparse import make_option
  6. from django.core.management.base import NoArgsCommand, BaseCommand
  7. from django.core.management.color import no_style
  8. from django.utils.datastructures import SortedDict
  9. from django.core.management.commands import syncdb
  10. from django.conf import settings
  11. from django.db import models
  12. from django.db.models.loading import cache
  13. from django.core import management
  14. from south.db import dbs
  15. from south import migration
  16. from south.exceptions import NoMigrations
  17. def get_app_label(app):
  18. return '.'.join( app.__name__.split('.')[0:-1] )
  19. class Command(NoArgsCommand):
  20. option_list = syncdb.Command.option_list + (
  21. make_option('--migrate', action='store_true', dest='migrate', default=False,
  22. help='Tells South to also perform migrations after the sync. Default for during testing, and other internal calls.'),
  23. make_option('--all', action='store_true', dest='migrate_all', default=False,
  24. help='Makes syncdb work on all apps, even migrated ones. Be careful!'),
  25. )
  26. if '--verbosity' not in [opt.get_opt_string() for opt in syncdb.Command.option_list]:
  27. option_list += (
  28. make_option('--verbosity', action='store', dest='verbosity', default='1',
  29. type='choice', choices=['0', '1', '2'],
  30. help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
  31. )
  32. help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created, except those which use migrations."
  33. def handle_noargs(self, migrate_all=False, **options):
  34. # Import the 'management' module within each installed app, to register
  35. # dispatcher events.
  36. # This is copied from Django, to fix bug #511.
  37. try:
  38. from django.utils.importlib import import_module
  39. except ImportError:
  40. pass # TODO: Remove, only for Django1.0
  41. else:
  42. for app_name in settings.INSTALLED_APPS:
  43. try:
  44. import_module('.management', app_name)
  45. except ImportError, exc:
  46. msg = exc.args[0]
  47. if not msg.startswith('No module named') or 'management' not in msg:
  48. raise
  49. # Work out what uses migrations and so doesn't need syncing
  50. apps_needing_sync = []
  51. apps_migrated = []
  52. for app in models.get_apps():
  53. app_label = get_app_label(app)
  54. if migrate_all:
  55. apps_needing_sync.append(app_label)
  56. else:
  57. try:
  58. migrations = migration.Migrations(app_label)
  59. except NoMigrations:
  60. # It needs syncing
  61. apps_needing_sync.append(app_label)
  62. else:
  63. # This is a migrated app, leave it
  64. apps_migrated.append(app_label)
  65. verbosity = int(options.get('verbosity', 0))
  66. # Run syncdb on only the ones needed
  67. if verbosity:
  68. print "Syncing..."
  69. old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, apps_needing_sync
  70. old_app_store, cache.app_store = cache.app_store, SortedDict([
  71. (k, v) for (k, v) in cache.app_store.items()
  72. if get_app_label(k) in apps_needing_sync
  73. ])
  74. # This will allow the setting of the MySQL storage engine, for example.
  75. for db in dbs.values():
  76. db.connection_init()
  77. # OK, run the actual syncdb
  78. syncdb.Command().execute(**options)
  79. settings.INSTALLED_APPS = old_installed
  80. cache.app_store = old_app_store
  81. # Migrate if needed
  82. if options.get('migrate', True):
  83. if verbosity:
  84. print "Migrating..."
  85. management.call_command('migrate', **options)
  86. # Be obvious about what we did
  87. if verbosity:
  88. print "\nSynced:\n > %s" % "\n > ".join(apps_needing_sync)
  89. if options.get('migrate', True):
  90. if verbosity:
  91. print "\nMigrated:\n - %s" % "\n - ".join(apps_migrated)
  92. else:
  93. if verbosity:
  94. print "\nNot synced (use migrations):\n - %s" % "\n - ".join(apps_migrated)
  95. print "(use ./manage.py migrate to migrate these)"