Exemple #1
0
def initialize_fixture(fixture_path,
                       database='fixture_tools_db',
                       debug=False,
                       force=False):
    """ @brief: Adds up-to-date South migration history to the fixture at \a fixture_path.
        @author: Jivan
        @since: 2014-05-23
        @param force: If True, existing South migration history in the fixture will be ignored.
            If False, fixtures with South migration history will result in a warning and
            remain unchanged.
    """
    fms = get_latest_fixture_migrations(fixture_path)
    # If there is migration history in the fixture, and we're not forcing an overwrite.
    if len(fms) > 0 and not force:
        msg = 'Found South migration history in fixture:\n{}\n'\
              "If you want to overwrite the fixture's South migration history please use --force."\
              .format(fixture_path)
        logger.warning(msg)
        ret = False
    else:
        django.db.close_connection()
        reset_db(database=database, debug=debug)
        sync_all(database=database, debug=debug)
        load_fixture(fixture_path, database=database)

        # If there is migration history history & we're forcing an overwrite
        if len(fms) > 0 and force:
            # Scrap the migration history from the fixture
            MigrationHistory.objects.all().delete()

        migrate_and_dump(fixture_path, database=database, fake=True)
        ret = True

    return ret
Exemple #2
0
def db_sample(db_obj_iterable, skip_south_history=False, child_depth=1, dest_db_alias=FIXTURE_DB, show_progress=False,
              outfile=None):
    at_least_one_object = False
    if show_progress: print("Sampling {} objects from 'origin' to '{}".format(len(db_obj_iterable), dest_db_alias))

    # Make an empty database with schema reflecting current code.
    if show_progress: print("Emptying destination database '{}'".format(dest_db_alias))
    django.db.close_connection()
    reset_db(database=dest_db_alias)
    sync_all(database=dest_db_alias)
    if not skip_south_history:
        fake_migrations(database=dest_db_alias)
    if show_progress: print("Destination database emptied, sampling objects.")

    # Copy requested objects from default db to fixture db.
    if show_progress:
        output_description = """
            O = top-level object being sampled.
            . = top-level object dependency being saved.
        """
        print(output_description)

    for obj in db_obj_iterable:
        at_least_one_object = True
        if show_progress:
            sys.stdout.write('O')
            sys.stdout.flush()
        sample_object(obj, child_depth=child_depth, dest_db_alias=dest_db_alias, show_progress=show_progress)
    if not at_least_one_object:
        logger.warn('No objects were requested for sampling.  Did you want an empty fixture?')
    if show_progress: print()

    # Dump sampled data
    dumpdata(dest_db_alias, outfile)
def initialize_fixture(fixture_path, database='fixture_tools_db', debug=False, force=False):
    """ @brief: Adds up-to-date South migration history to the fixture at \a fixture_path.
        @author: Jivan
        @since: 2014-05-23
        @param force: If True, existing South migration history in the fixture will be ignored.
            If False, fixtures with South migration history will result in a warning and
            remain unchanged.
    """
    fms = get_latest_fixture_migrations(fixture_path)
    # If there is migration history in the fixture, and we're not forcing an overwrite.
    if len(fms) > 0 and not force:
        msg = 'Found South migration history in fixture:\n{}\n'\
              "If you want to overwrite the fixture's South migration history please use --force."\
              .format(fixture_path)
        logger.warning(msg)
        ret = False
    else:
        django.db.close_connection()
        reset_db(database=database, debug=debug)
        sync_all(database=database, debug=debug)
        load_fixture(fixture_path, database=database)

        # If there is migration history history & we're forcing an overwrite
        if len(fms) > 0 and force:
            # Scrap the migration history from the fixture
            MigrationHistory.objects.all().delete()
    
        migrate_and_dump(fixture_path, database=database, fake=True)
        ret = True

    return ret
Exemple #4
0
def migrate_fixture(fixture_path,
                    database='fixture_tools_db',
                    load_commit=None,
                    debug=False):
    """ @brief: Migrates \a fixture_path from the commit it was last modified to the current
            state of South migrations.
        @author: Jivan
        @since: 2014-05-23
        @param load_commit: If not None, this commit will be used to load \a fixture_path
            instead of the commit in which it was most recently modified commit.
    """
    fms = get_latest_fixture_migrations(fixture_path)
    # If there is no migration history in the fixture, exit with warning
    if len(fms) == 0:
        logger.info('There is no South migration history in this fixture.  You need to '\
                    'initialize the fixture before attempting to migrate it.')
        ret = False
    else:
        logger.info('--- Finding commit when fixture was last modified.')
        original_branch, load_commit = \
            identify_and_check_out_last_modified_commit(
                fixture_path, debug=debug, commit=load_commit
            )
        logger.info('Using commit: {} to load fixture.'.format(
            load_commit[:8]))

        logger.info('--- Creating compatible database.')
        # Prevents 'another session is using the database' errors due to lingering connections.
        django.db.close_connection()
        clear_south_migration_caches()
        reset_db(database=database)
        if debug:
            if not query_yes_no('Database reset, continue?'):
                check_out_branch(original_branch, debug=debug)
                return False
        sync_all(database=database)
        if debug:
            if not query_yes_no('Empty database created, continue?'):
                check_out_branch(original_branch, debug=debug)
                return False

        logger.info('--- Loading fixture.')
        logger.info('Fixture name: {}'.format(fixture_path))
        load_fixture(fixture_path, database=database)

        logger.info('--- Checking out original commit.')
        check_out_branch(original_branch, debug=debug)

        # This is performed in a subprocess because the django model caching gets too confused
        #    by checking out a different commit to do it in this process.
        logger.info(
            '--- Migrating to latest and dumping back to fixture file.')
        migrate_and_dump_cmd = ' '.join([
            'python', '../django_fixture_tools/shared.py', 'migrate_and_dump',
            fixture_path
        ])
        migrate_and_dump_out = check_output(migrate_and_dump_cmd, shell=True)
        ret = True
    return ret
def migrate_fixture(fixture_path, database='fixture_tools_db', load_commit=None, debug=False):
    """ @brief: Migrates \a fixture_path from the commit it was last modified to the current
            state of South migrations.
        @author: Jivan
        @since: 2014-05-23
        @param load_commit: If not None, this commit will be used to load \a fixture_path
            instead of the commit in which it was most recently modified commit.
    """
    fms = get_latest_fixture_migrations(fixture_path)
    # If there is no migration history in the fixture, exit with warning
    if len(fms) == 0:
        logger.info('There is no South migration history in this fixture.  You need to '\
                    'initialize the fixture before attempting to migrate it.')
        ret = False
    else:
        logger.info('--- Finding commit when fixture was last modified.')
        original_branch, load_commit = \
            identify_and_check_out_last_modified_commit(
                fixture_path, debug=debug, commit=load_commit
            )
        logger.info('Using commit: {} to load fixture.'.format(load_commit[:8]))

        logger.info('--- Creating compatible database.')
        # Prevents 'another session is using the database' errors due to lingering connections.
        django.db.close_connection()
        clear_south_migration_caches()
        reset_db(database=database)
        if debug:
            if not query_yes_no('Database reset, continue?'):
                check_out_branch(original_branch, debug=debug)
                return False
        sync_all(database=database)
        if debug:
            if not query_yes_no('Empty database created, continue?'):
                check_out_branch(original_branch, debug=debug)
                return False

        logger.info('--- Loading fixture.')
        logger.info('Fixture name: {}'.format(fixture_path))
        load_fixture(fixture_path, database=database)
    
        logger.info('--- Checking out original commit.')
        check_out_branch(original_branch, debug=debug)

        # This is performed in a subprocess because the django model caching gets too confused
        #    by checking out a different commit to do it in this process.
        logger.info('--- Migrating to latest and dumping back to fixture file.')
        migrate_and_dump_cmd = ' '.join(['python','../django_fixture_tools/shared.py',
                                 'migrate_and_dump', fixture_path])
        migrate_and_dump_out = check_output(migrate_and_dump_cmd, shell=True)
        ret = True
    return ret