def dumpdata(database=None, fixture_path=None):
    """ @brief: Dumps data from \a database to \a fixture_path.
        @since: 2014-05-29
        @author: Jivan
        If fixture_path isn't provided, dumps to stdout.
    """
    if database is None: raise Exception('dblabel is a required argument')

    ddc = DumpDataCommand()

    # If fixture_path has been specified, steal output from stdout.
    if fixture_path:
        from cStringIO import StringIO
        old_stdout = sys.stdout
        sys.stdout = mystdout = StringIO()

    exclude = ['auth.permission', 'contenttypes']
    ddc.execute(format='json', natural=True, exclude=exclude, indent=4, database=database)

    # If fixture_path has been specified, dump the stolen output into it.
    if fixture_path:
        sys.stdout = old_stdout
        with open(fixture_path, 'w') as f:
            f.write(mystdout.getvalue())
            f.write('\n')
            mystdout.close()
Beispiel #2
0
def serialize_to_response(app_labels=[],
                          exclude=[],
                          response=None,
                          format=settings.SMUGGLER_FORMAT,
                          indent=settings.SMUGGLER_INDENT):
    response = response or HttpResponse(content_type='text/plain')
    stream = StringIO()
    error_stream = StringIO()
    try:
        dumpdata = DumpData()
        dumpdata.style = no_style()
        dumpdata.execute(
            *app_labels, **{
                'stdout': stream,
                'stderr': error_stream,
                'exclude': exclude,
                'format': format,
                'indent': indent,
                'use_natural_keys': True
            })
    except SystemExit:
        # Django 1.4's implementation of execute catches CommandErrors and
        # then calls sys.exit(1), we circumvent this here.
        errors = error_stream.getvalue().strip().replace('Error: ', '')
        raise CommandError(errors)
    response.write(stream.getvalue())
    return response
Beispiel #3
0
def dumpdata(database=None, fixture_path=None):
    """ @brief: Dumps data from \a database to \a fixture_path.
        @since: 2014-05-29
        @author: Jivan
        If fixture_path isn't provided, dumps to stdout.
    """
    if database is None: raise Exception('dblabel is a required argument')

    ddc = DumpDataCommand()

    # If fixture_path has been specified, steal output from stdout.
    if fixture_path:
        from cStringIO import StringIO
        old_stdout = sys.stdout
        sys.stdout = mystdout = StringIO()

    exclude = ['auth.permission', 'contenttypes']
    ddc.execute(format='json',
                natural=True,
                exclude=exclude,
                indent=4,
                database=database)

    # If fixture_path has been specified, dump the stolen output into it.
    if fixture_path:
        sys.stdout = old_stdout
        with open(fixture_path, 'w') as f:
            f.write(mystdout.getvalue())
            f.write('\n')
            mystdout.close()
Beispiel #4
0
def serialize_to_response(app_labels=[], exclude=[], response=None,
                          format=settings.SMUGGLER_FORMAT,
                          indent=settings.SMUGGLER_INDENT):
    response = response or HttpResponse(content_type='text/plain')
    stream = StringIO()
    error_stream = StringIO()
    try:
        dumpdata = DumpData()
        dumpdata.style = no_style()
        dumpdata.execute(*app_labels, **{
            'stdout': stream,
            'stderr': error_stream,
            'exclude': exclude,
            'format': format,
            'indent': indent,
            'use_natural_keys': True,
            'use_natural_foreign_keys': True,
            'use_natural_primary_keys': True
        })
    except SystemExit:
        # Django 1.4's implementation of execute catches CommandErrors and
        # then calls sys.exit(1), we circumvent this here.
        errors = error_stream.getvalue().strip().replace('Error: ', '')
        raise CommandError(errors)
    response.write(stream.getvalue())
    return response
    def dump_tenant_data(self, schema_name=None):
        dumpdb_command = DumpDataCommand()
        if schema_name:
            print self.style.NOTICE("=== Running dumpdata for schema: %s" % schema_name)
            sync_tenant = get_tenant_model().objects.filter(schema_name=schema_name).get()
            connection.set_tenant(sync_tenant, include_public=True)
            dumpdb_command.execute(*self.app_labels, **self.options)
        else:
            public_schema_name = get_public_schema_name()
            tenant_schemas_count = get_tenant_model().objects.exclude(schema_name=public_schema_name).count()
            if not tenant_schemas_count:
                raise CommandError("No tenant schemas found")

            for tenant_schema in get_tenant_model().objects.exclude(schema_name=public_schema_name).all():
                print self.style.NOTICE("=== Running syncdb for schema %s" % tenant_schema.schema_name)
                try:
                    connection.set_tenant(tenant_schema, include_public=True)
                    dumpdb_command.execute(*self.app_labels, **self.options)
                except Exception as e:
                    print e
Beispiel #6
0
def serialize_to_response(app_labels=None, exclude=None, response=None,
                          format=settings.SMUGGLER_FORMAT,
                          indent=settings.SMUGGLER_INDENT):
    app_labels = app_labels or []
    exclude = exclude or []
    response = response or HttpResponse(content_type='text/plain')
    stream = StringIO()
    error_stream = StringIO()
    dumpdata = DumpData()
    dumpdata.style = no_style()
    dumpdata.execute(*app_labels, **{
        'stdout': stream,
        'stderr': error_stream,
        'exclude': exclude,
        'format': format,
        'indent': indent,
        'use_natural_foreign_keys': True,
        'use_natural_primary_keys': True
    })
    response.write(stream.getvalue())
    return response
Beispiel #7
0
def serialize_to_response(app_labels=None,
                          exclude=None,
                          response=None,
                          format=settings.SMUGGLER_FORMAT,
                          indent=settings.SMUGGLER_INDENT):
    app_labels = app_labels or []
    exclude = exclude or []
    response = response or HttpResponse(content_type='text/plain')
    stream = StringIO()
    error_stream = StringIO()
    dumpdata = DumpData()
    dumpdata.style = no_style()
    dumpdata.execute(
        *app_labels, **{
            'stdout': stream,
            'stderr': error_stream,
            'exclude': exclude,
            'format': format,
            'indent': indent,
            'use_natural_foreign_keys': True,
            'use_natural_primary_keys': True
        })
    response.write(stream.getvalue())
    return response
def migrate_fixture(fixture_path, db='fixture_migrator'):
    """ @brief: Uses South migrations in the current project to update the contents of the
            fixture at \a fixture_path.
        @author: Jivan
        @since: 2014-04-08
    """
    # --- Create empty database migrated to latest migrations.
#     from django.core.management.commands.flush import Command as FlushCommand
#     fc = FlushCommand()
#     fc.execute(database=db, interactive=False, verbosity=0)
    logger.info('--- Syncing Database tables to Current Models')
    from south.management.commands.syncdb import Command as SyncDBCommand
    sc = SyncDBCommand()
    sc.execute(migrate_all=True, migrate=False, database=db, interactive=False, verbosity=0)
    logger.info('--- Faking Migrations to Current Latest')
    from south.management.commands.migrate import Command as MigrateCommand
    mc = MigrateCommand()
    mc.execute(all_apps=True, fake=True, database=db, interactive=False, verbosity=0)
 
    # --- Get South Migration History from fixture.
    # Fixture file
    with open(fixture_path, 'r') as ff:
        fixture_contents = json.load(ff)
        fixture_migrations = [
            { i['fields']['app_name']: i['fields']['migration'] }
                for i in fixture_contents
                if i['model'] == 'south.migrationhistory'
        ]
    if len(fixture_migrations) == 0:
        logger.info('No migration history found in fixture, guessing migrations from last commit this fixture was migrated.')
        fixture_migrations = guess_migrations_from_git_repository(fixture_path)

    fixture_latest_migrations = defaultdict(unicode)
    for app, migration in fixture_migrations.items():
        latest_migration = fixture_latest_migrations[app]
        if latest_migration == '' or migration > latest_migration:
            fixture_latest_migrations[app] = migration
      
    # --- Migrate database to latest migrations in fixture
    logger.info('--- Migrating database backwards to latest migrations in fixture.')
    for app, latest_migration in fixture_latest_migrations.items():
        print('Migrating {} to {}'.format(app, latest_migration))
        try:
            mc.execute(app=app, target=latest_migration, database=db, interactive=False, verbosity=0)
        except ImproperlyConfigured as ex:
            if ex.message == 'App with label {} could not be found'.format(app):
                logger.error("Looks like app '{}' was removed from settings.  "
                             "I'll remove its entries from South's Migration history "
                             "in the new fixture.".format(app))
            MigrationHistory.objects.using(db).filter(app_name=app).delete()
            continue

    # --- Load fixture
    from django.core.management.commands.loaddata import Command as LoadDataCommand
    ldc = LoadDataCommand()
    ldc.execute(fixture_path, database=db, verbosity=1)
    
    # --- Migrate to latest migrations in codebase
    mc.execute(database=db, interactive=False, verbosity=1)
 
    # --- Dump the contents back out to fixture
    from django.core.management.commands.dumpdata import Command as DumpDataCommand
    ddc = DumpDataCommand()
    from cStringIO import StringIO
    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    ddc.execute(format='json', indent=4, database=db, exclude=[])
    sys.stdout = old_stdout
    with open(fixture_path, 'w') as f:
        f.write(mystdout.getvalue())
        mystdout.close()