Ejemplo n.º 1
0
 def test_get_app_label(self):
     self.assertEqual(
         "southtest",
         get_app_label(self.create_fake_app("southtest.models")),
     )
     self.assertEqual(
         "baz",
         get_app_label(self.create_fake_app("foo.bar.baz.models")),
     )
Ejemplo n.º 2
0
 def test_get_app_label(self):
     self.assertEqual(
         "southtest",
         get_app_label(self.create_fake_app("southtest.models")),
     )
     self.assertEqual(
         "baz",
         get_app_label(self.create_fake_app("foo.bar.baz.models")),
     )
Ejemplo n.º 3
0
    def sync_apps(self, app_labels, app_name_to_app_map, options):
        if DJANGO_17:
            from django.db.migrations.executor import MigrationExecutor
            from django.core.management.commands import migrate

            apps_to_sync = []
            for app_label in app_labels:
                app_label = app_name_to_app_map[app_label].label if app_label in app_name_to_app_map else app_label
                apps_to_sync.append(app_label)

            connection = connections[options.get('database', 'default')]

            cmd = migrate.Command()
            cmd.stdout = self.stdout
            cmd.stderr = self.stderr
            cmd.run_syncdb = True
            cmd.verbosity = int(options.get('verbosity'))
            cmd.interactive = options.get('interactive')
            cmd.show_traceback = options.get('traceback')
            cmd.load_initial_data = options.get('load_initial_data')
            cmd.test_database = options.get('test_database', False)
            cmd.sync_apps(connection, apps_to_sync)
        else:
            old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, app_labels
            old_app_store, cache.app_store = cache.app_store, SortedDict([
                (k, v) for (k, v) in cache.app_store.items()
                if get_app_label(k) in app_labels
            ])

            # OK, run the actual syncdb
            syncdb.Command().execute(**options)

            settings.INSTALLED_APPS = old_installed
            cache.app_store = old_app_store
Ejemplo n.º 4
0
    def sync_apps(self, app_labels, app_name_to_app_map, options):
        if DJANGO_17:
            from django.db.migrations.executor import MigrationExecutor
            from django.core.management.commands import migrate

            apps_to_sync = []
            for app_label in app_labels:
                app_label = app_name_to_app_map[
                    app_label].label if app_label in app_name_to_app_map else app_label
                apps_to_sync.append(app_label)

            connection = connections[options.get('database', 'default')]

            cmd = migrate.Command()
            cmd.stdout = self.stdout
            cmd.stderr = self.stderr
            cmd.run_syncdb = True
            cmd.verbosity = int(options.get('verbosity'))
            cmd.interactive = options.get('interactive')
            cmd.show_traceback = options.get('traceback')
            cmd.load_initial_data = options.get('load_initial_data')
            cmd.test_database = options.get('test_database', False)
            cmd.sync_apps(connection, apps_to_sync)
        else:
            old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, app_labels
            old_app_store, cache.app_store = cache.app_store, SortedDict([
                (k, v) for (k, v) in cache.app_store.items()
                if get_app_label(k) in app_labels
            ])

            # OK, run the actual syncdb
            syncdb.Command().execute(**options)

            settings.INSTALLED_APPS = old_installed
            cache.app_store = old_app_store
Ejemplo n.º 5
0
 def __call__(self, application_or_app_label, **kwds):
     if isinstance(application_or_app_label, six.string_types):
         app_label = application_or_app_label
     else:
         app_label = get_app_label(application_or_app_label)
     if app_label not in self.instances:
         self.instances[app_label] = super(
             MigrationsMetaclass, self).__call__(
             application_or_app_label, **kwds)
     return self.instances[app_label]
Ejemplo n.º 6
0
 def __call__(self, application_or_app_label, **kwds):
     if isinstance(application_or_app_label, six.string_types):
         app_label = application_or_app_label
     else:
         app_label = get_app_label(application_or_app_label)
     if app_label not in self.instances:
         self.instances[app_label] = super(MigrationsMetaclass,
                                           self).__call__(
                                               application_or_app_label,
                                               **kwds)
     return self.instances[app_label]
Ejemplo n.º 7
0
def all_migrations(applications=None):
    """
    Returns all Migrations for all `applications` that are migrated.
    """
    if applications is None:
        applications = models.get_apps()
    for model_module in applications:
        app_label = get_app_label(model_module)
        try:
            yield Migrations(app_label)
        except exceptions.NoMigrations:
            pass
Ejemplo n.º 8
0
def all_migrations(applications=None):
    """
    Returns all Migrations for all `applications` that are migrated.
    """
    if applications is None:
        applications = models.get_apps()
    for model_module in applications:
        app_label = get_app_label(model_module)
        try:
            yield Migrations(app_label)
        except exceptions.NoMigrations:
            pass
Ejemplo n.º 9
0
 def app_label(self):
     return get_app_label(self._migrations)
Ejemplo n.º 10
0
 def app_label(self):
     return get_app_label(self._migrations)
Ejemplo n.º 11
0
    def handle_noargs(self, **options):
        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        if not hasattr(self, 'stdout'):
            self.stdout = sys.stdout
            self.stderr = sys.stderr
        if DJANGO_17:
            from django.apps import apps
            from django.utils.module_loading import module_has_submodule

            for app_config in apps.get_app_configs():
                if module_has_submodule(app_config.module, "management"):
                    import_module('.management', app_config.name)
        else:
            for app_name in settings.INSTALLED_APPS:
                try:
                    import_module('.management', app_name)
                except ImportError as exc:
                    msg = exc.args[0]
                    if not msg.startswith(
                            'No module named') or 'management' not in msg:
                        raise

        # Work out what uses migrations and so doesn't need syncing
        apps_needing_sync = []
        apps_migrated = []
        app_name_to_app_map = {}

        if DJANGO_17:
            for app_config in apps.get_app_configs():
                if not app_config.models_module:
                    continue

                app_label = get_app_label(app_config.models_module)
                app_name_to_app_map[app_label] = app_config
                try:
                    migrations = migration.Migrations(app_label)
                except NoMigrations:
                    # It needs syncing
                    apps_needing_sync.append(app_label)
                else:
                    # This is a migrated app, leave it
                    apps_migrated.append(app_label)
        else:
            for app in models.get_apps():
                app_label = get_app_label(app)
                try:
                    migrations = migration.Migrations(app_label)
                except NoMigrations:
                    # It needs syncing
                    apps_needing_sync.append(app_label)
                else:
                    # This is a migrated app, leave it
                    apps_migrated.append(app_label)

        verbosity = int(options.get('verbosity', 0))

        # Run syncdb on only the ones needed
        if verbosity:
            self.stdout.write("Syncing...\n")

        # This will allow the setting of the MySQL storage engine, for example.
        for db in dbs.values():
            db.connection_init()

        # In Django 1.7 we need to actually run migrations (Sentry specifically)
        # as it creates the 'auth' table. To run migrations however we still need
        # to create the south tables ahead of time.
        if DJANGO_17:
            self.sync_apps(['south'], app_name_to_app_map, options)
            apps_needing_sync.remove('south')
        # In 1.6 the constraints dont function/get created in the same way, and
        # additionally contenttypes tries to apply so syncing just south isnt enough
        else:
            self.sync_apps(apps_needing_sync, app_name_to_app_map, options)

        # Migrate if needed
        if options.get('migrate', True):
            if verbosity:
                self.stdout.write("Migrating...\n")
            management.call_command('south_migrate', **options)

        if DJANGO_17:
            self.sync_apps(apps_needing_sync, app_name_to_app_map, options)

        # Be obvious about what we did
        if verbosity:
            self.stdout.write("\nSynced:\n > {}\n".format(
                "\n > ".join(apps_needing_sync)))

        if options.get('migrate', True):
            if verbosity:
                self.stdout.write("\nMigrated:\n - {}\n".format(
                    "\n - ".join(apps_migrated)))
        else:
            if verbosity:
                self.stdout.write(
                    "\nNot synced (use migrations):\n - {}\n".format(
                        "\n - ".join(apps_migrated)))
                self.stdout.write(
                    "(use ./manage.py migrate to migrate these)\n")
Ejemplo n.º 12
0
    def handle_noargs(self, **options):
        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        if not hasattr(self, 'stdout'):
            self.stdout = sys.stdout
            self.stderr = sys.stderr
        if DJANGO_17:
            from django.apps import apps
            from django.utils.module_loading import module_has_submodule

            for app_config in apps.get_app_configs():
                if module_has_submodule(app_config.module, "management"):
                    import_module('.management', app_config.name)
        else:
            for app_name in settings.INSTALLED_APPS:
                try:
                    import_module('.management', app_name)
                except ImportError as exc:
                    msg = exc.args[0]
                    if not msg.startswith('No module named') or 'management' not in msg:
                        raise

        # Work out what uses migrations and so doesn't need syncing
        apps_needing_sync = []
        apps_migrated = []
        app_name_to_app_map = {}

        if DJANGO_17:
            for app_config in apps.get_app_configs():
                if not app_config.models_module:
                    continue

                app_label = get_app_label(app_config.models_module)
                app_name_to_app_map[app_label] = app_config
                try:
                    migrations = migration.Migrations(app_label)
                except NoMigrations:
                    # It needs syncing
                    apps_needing_sync.append(app_label)
                else:
                    # This is a migrated app, leave it
                    apps_migrated.append(app_label)
        else:
            for app in models.get_apps():
                app_label = get_app_label(app)
                try:
                    migrations = migration.Migrations(app_label)
                except NoMigrations:
                    # It needs syncing
                    apps_needing_sync.append(app_label)
                else:
                    # This is a migrated app, leave it
                    apps_migrated.append(app_label)

        verbosity = int(options.get('verbosity', 0))

        # Run syncdb on only the ones needed
        if verbosity:
            self.stdout.write("Syncing...\n")

        # This will allow the setting of the MySQL storage engine, for example.
        for db in dbs.values():
            db.connection_init()

        # In Django 1.7 we need to actually run migrations (Sentry specifically)
        # as it creates the 'auth' table. To run migrations however we still need
        # to create the south tables ahead of time.
        if DJANGO_17:
            self.sync_apps(['south'], app_name_to_app_map, options)
            apps_needing_sync.remove('south')
        # In 1.6 the constraints dont function/get created in the same way, and
        # additionally contenttypes tries to apply so syncing just south isnt enough
        else:
            self.sync_apps(apps_needing_sync, app_name_to_app_map, options)

        # Migrate if needed
        if options.get('migrate', True):
            if verbosity:
                self.stdout.write("Migrating...\n")
            management.call_command('south_migrate', **options)

        if DJANGO_17:
            self.sync_apps(apps_needing_sync, app_name_to_app_map, options)

        # Be obvious about what we did
        if verbosity:
            self.stdout.write("\nSynced:\n > {}\n".format("\n > ".join(apps_needing_sync)))

        if options.get('migrate', True):
            if verbosity:
                self.stdout.write("\nMigrated:\n - {}\n".format("\n - ".join(apps_migrated)))
        else:
            if verbosity:
                self.stdout.write(
                    "\nNot synced (use migrations):\n - {}\n".format(
                        "\n - ".join(apps_migrated)))
                self.stdout.write("(use ./manage.py migrate to migrate these)\n")