def default_step_condition_factory(target_version, step_name):
    """Create a default condition used by upgrade steps.

    The created condition checks if the step was already successfully ran
    and skip it if it is the case.
    """
    def default_step_condition(alembic, failed_migration, *args):
        if failed_migration and failed_migration.version == target_version:
            try:
                step = next(step for step in failed_migration.data['steps']
                            if step['name'] == step_name)
                # skip the step if it already ran successfully
                if step['status'] in {'success', 'skip'}:
                    return False
            except StopIteration:
                # The step was not run at all.
                pass
        return True
    return default_step_condition