def handle(self, app, *args, **kwargs):
        app_migrations = Migrations(app)
        migrations_module = app_migrations.migrations_module()

        applied = MigrationHistory.objects.filter(app_name=app) \
            .values_list('migration_name', flat=True).order_by('applied_on')
        workplan = to_apply(app_migrations, applied)

        for m in workplan:
            module = import_migration_module(migrations_module, m)
            module.Migration().apply()
            MigrationHistory.objects.create(app_name=app, migration_name=m)
    def handle(self, app, name, *args, **kwargs):
        if re.search('[^_\w]', name) and name != '-':
            self.error('Migration names should contain only alphanumeric characters and underscores.')

        try:
            models.get_app(app)
        except ImproperlyConfigured:
            self.error("There is no enabled application matching '%s'." % app)

        migrations = Migrations(app)
        new_filename = migrations.next_filename(name)

        with open(os.path.join(migrations.migrations_dir(), new_filename), "w") as fp:
            if args:
                args = join_args(args)
                fp.write(CALL_COMMAND_MIGRATION_TEMPLATE.format(args))
            else:
                fp.write(MIGRATION_TEMPLATE)