Exemplo n.º 1
0
def load_fixture(fixture_path, database=None):
    if database is None:
        raise Exception('database is a required argument')
    ldc = LoadDataCommand()
    from cStringIO import StringIO
    original_stderr = sys.stderr
    sys.stderr = my_stderr = StringIO()
    ldc.execute(fixture_path, database=database, verbosity=1)
    sys.stderr = original_stderr
    ret = my_stderr.getvalue()
    if 'Problem installing fixture' in ret:
        raise Exception(ret)
Exemplo n.º 2
0
def load_fixture(fixture_path, database=None):
    if database is None:
        raise Exception('database is a required argument')
    ldc = LoadDataCommand()
    from cStringIO import StringIO
    original_stderr = sys.stderr
    sys.stderr = my_stderr = StringIO()
    ldc.execute(fixture_path, database=database, verbosity=1)
    sys.stderr = original_stderr
    ret = my_stderr.getvalue()
    if 'Problem installing fixture' in ret:
        raise Exception(ret)
Exemplo n.º 3
0
def load_fixtures(fixtures):
    stream = StringIO()
    error_stream = StringIO()
    loaddata = LoadData()
    loaddata.style = no_style()
    loaddata.execute(*fixtures, **{
        'stdout': stream,
        'stderr': error_stream,
        'ignore': True,
        'database': DEFAULT_DB_ALIAS,
        'verbosity': 1
    })
    return loaddata.loaded_object_count
Exemplo n.º 4
0
def load_fixtures(fixtures):
    stream = StringIO()
    error_stream = StringIO()
    loaddata = LoadData()
    loaddata.style = no_style()
    loaddata.execute(
        *fixtures, **{
            'stdout': stream,
            'stderr': error_stream,
            'ignore': True,
            'database': DEFAULT_DB_ALIAS,
            'verbosity': 1
        })
    return loaddata.loaded_object_count
Exemplo n.º 5
0
    def load_tenant_data(self, schema_name=None):
        loaddb_command = LoadDataCommand()
        if schema_name:
            print self.style.NOTICE("=== Running loaddata 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)
            loaddb_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)
                    loaddb_command.execute(*self.app_labels, **self.options)
                except Exception as e:
                    print e
Exemplo n.º 6
0
def load_fixture(fixture):
    import tempfile, sys
    from django.core import management
    #create a temporary location to store the output generated by loaddata
    tempdir = tempfile.mkdtemp()
    tempout = os.path.join(tempdir, 'temp.txt')
    #call loaddata on the temporary fixture file, storing any output from the loaddata call into a temporary location
    
    #the following lines temporarily redirect the output from stdout to tempout 
    saveout = sys.stdout
    fsock = open(tempout, 'w')
    sys.stdout = fsock
    #this output rediction allows us to capture the output generated by loaddata
    #management.call_command('loaddata', fixture, verbosity=1)
    
    from django.core.management.commands.loaddata import Command
    klass = Command()
    klass.execute(fixture, verbosity=1)
    
    #and the following lines redirect the output back to stdout
    sys.stdout = saveout     
    fsock.close()
    
    #read and store the last line from the output file 
    #(this should be something similar to: 'Installed xx object(s) from xx fixture(s)')
    outfile_reader = open(tempout, 'r')
    output_all = outfile_reader.readlines()
    output = output_all[-1]

    #the following is a pretty lame way of determining whether the fixture loaded anything or not
    #but i'm not sure how else this might be accomplished since the management.call_command to loaddata
    #doesn't seem to provide any additional information about its success or failure
    if 'Installed' in output:
        success = True
    else:
        success = False
        
    return success, output
    
Exemplo n.º 7
0
Arquivo: utils.py Projeto: t0in4/exgit
def load_fixtures(fixtures):
    stream = StringIO()
    error_stream = StringIO()
    loaddata = LoadData()
    loaddata.style = no_style()
    loaddata.execute(*fixtures, **{
        'stdout': stream,
        'stderr': error_stream,
        'ignore': True,
        'database': DEFAULT_DB_ALIAS,
        'verbosity': 1
    })
    if hasattr(loaddata, 'loaded_object_count'):
        return loaddata.loaded_object_count
    else:
        # Django < 1.6 has no loaded_object_count attribute, we need
        # to fetch it from stderror :(
        errors = error_stream.getvalue()
        out = stream.getvalue()
        if errors:
            # The only way to handle errors in Django 1.4 is to inspect stdout
            raise CommandError(errors.strip().splitlines()[-1])
        return int(re.search('Installed ([0-9]+)', out.strip()).group(1))
Exemplo n.º 8
0
def load_fixtures(fixtures):
    stream = StringIO()
    error_stream = StringIO()
    loaddata = LoadData()
    loaddata.style = no_style()
    loaddata.execute(
        *fixtures, **{
            'stdout': stream,
            'stderr': error_stream,
            'ignore': True,
            'database': DEFAULT_DB_ALIAS,
            'verbosity': 1
        })
    if hasattr(loaddata, 'loaded_object_count'):
        return loaddata.loaded_object_count
    else:
        # Django < 1.6 has no loaded_object_count attribute, we need
        # to fetch it from stderror :(
        errors = error_stream.getvalue()
        out = stream.getvalue()
        if errors:
            # The only way to handle errors in Django 1.4 is to inspect stdout
            raise CommandError(errors.strip().splitlines()[-1])
        return int(re.search('Installed ([0-9]+)', out.strip()).group(1))
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()