Ejemplo n.º 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
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
Ejemplo n.º 3
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