Ejemplo n.º 1
0
    def validate_schema(self, data, **kwargs):
        if "config_version" not in data:
            raise ge_exceptions.InvalidDataContextConfigError(
                "The key `config_version` is missing; please check your config file.",
                validation_error=ValidationError("no config_version key"),
            )

        if not isinstance(data["config_version"], (int, float)):
            raise ge_exceptions.InvalidDataContextConfigError(
                "The key `config_version` must be a number. Please check your config file.",
                validation_error=ValidationError(
                    "config version not a number"),
            )

        # When migrating from 0.7.x to 0.8.0
        if data["config_version"] == 0 and ("validations_store" in list(
                data.keys()) or "validations_stores" in list(data.keys())):
            raise ge_exceptions.UnsupportedConfigVersionError(
                "You appear to be using a config version from the 0.7.x series. This version is no longer supported."
            )
        elif data["config_version"] < MINIMUM_SUPPORTED_CONFIG_VERSION:
            raise ge_exceptions.UnsupportedConfigVersionError(
                "You appear to have an invalid config version ({}).\n    The version number must be between {} and {}."
                .format(
                    data["config_version"],
                    MINIMUM_SUPPORTED_CONFIG_VERSION,
                    CURRENT_CONFIG_VERSION,
                ), )
        elif data["config_version"] > CURRENT_CONFIG_VERSION:
            raise ge_exceptions.InvalidDataContextConfigError(
                "You appear to have an invalid config version ({}).\n    The maximum valid version is {}."
                .format(data["config_version"], CURRENT_CONFIG_VERSION),
                validation_error=ValidationError("config version too high"),
            )
Ejemplo n.º 2
0
    def set_ge_config_version(cls,
                              config_version,
                              context_root_dir=None,
                              validate_config_version=True):
        if not isinstance(config_version, (int, float)):
            raise ge_exceptions.UnsupportedConfigVersionError(
                "The argument `config_version` must be a number.", )

        if validate_config_version:
            if config_version < MINIMUM_SUPPORTED_CONFIG_VERSION:
                raise ge_exceptions.UnsupportedConfigVersionError(
                    "Invalid config version ({}).\n    The version number must be at least {}. "
                    .format(config_version,
                            MINIMUM_SUPPORTED_CONFIG_VERSION), )
            elif config_version > CURRENT_GE_CONFIG_VERSION:
                raise ge_exceptions.UnsupportedConfigVersionError(
                    "Invalid config version ({}).\n    The maximum valid version is {}."
                    .format(config_version, CURRENT_GE_CONFIG_VERSION), )

        yml_path = cls.find_context_yml_file(search_start_dir=context_root_dir)
        if yml_path is None:
            return False

        with open(yml_path) as f:
            config_commented_map_from_yaml = yaml.load(f)
            config_commented_map_from_yaml["config_version"] = float(
                config_version)

        with open(yml_path, "w") as f:
            yaml.dump(config_commented_map_from_yaml, f)

        return True
Ejemplo n.º 3
0
def do_config_check(target_directory):
    is_config_ok: bool = True
    upgrade_message: str = ""
    context: Optional[DataContext]
    try:
        context = DataContext(context_root_dir=target_directory)
        ge_config_version: int = context.get_config().config_version
        if int(ge_config_version) < CURRENT_GE_CONFIG_VERSION:
            is_config_ok = False
            upgrade_message = f"""The config_version of your great_expectations.yml -- {float(ge_config_version)} -- is outdated.
Please consult the V3 API migration guide https://docs.greatexpectations.io/docs/guides/miscellaneous/migration_guide#migrating-to-the-batch-request-v3-api and
upgrade your Great Expectations configuration to version {float(CURRENT_GE_CONFIG_VERSION)} in order to take advantage of the latest capabilities.
"""
            context = None
        elif int(ge_config_version) > CURRENT_GE_CONFIG_VERSION:
            raise ge_exceptions.UnsupportedConfigVersionError(
                f"""Invalid config version ({ge_config_version}).\n    The maximum valid version is \
{CURRENT_GE_CONFIG_VERSION}.
""")
        else:
            upgrade_helper_class = GE_UPGRADE_HELPER_VERSION_MAP.get(
                int(ge_config_version))
            if upgrade_helper_class:
                upgrade_helper = upgrade_helper_class(data_context=context,
                                                      update_version=False)
                manual_steps_required = upgrade_helper.manual_steps_required()
                if manual_steps_required:
                    (
                        upgrade_overview,
                        confirmation_required,
                    ) = upgrade_helper.get_upgrade_overview()
                    upgrade_overview = cli_colorize_string(upgrade_overview)
                    cli_message(string=upgrade_overview)
                    is_config_ok = False
                    upgrade_message = """The configuration of your great_expectations.yml is outdated.  Please \
consult the V3 API migration guide \
https://docs.greatexpectations.io/docs/guides/miscellaneous/migration_guide#migrating-to-the-batch-request-v3-api and upgrade your \
Great Expectations configuration in order to take advantage of the latest capabilities.
"""
                    context = None
    except (
            ge_exceptions.InvalidConfigurationYamlError,
            ge_exceptions.InvalidTopLevelConfigKeyError,
            ge_exceptions.MissingTopLevelConfigKeyError,
            ge_exceptions.InvalidConfigValueTypeError,
            ge_exceptions.UnsupportedConfigVersionError,
            ge_exceptions.DataContextError,
            ge_exceptions.PluginClassNotFoundError,
            ge_exceptions.PluginModuleNotFoundError,
            ge_exceptions.GreatExpectationsError,
    ) as err:
        is_config_ok = False
        upgrade_message = err.message
        context = None

    return (
        is_config_ok,
        upgrade_message,
        context,
    )
Ejemplo n.º 4
0
def load_data_context_with_error_handling(
        directory: str,
        from_cli_upgrade_command: bool = False) -> Optional[DataContext]:
    """Return a DataContext with good error handling and exit codes."""
    context: Optional[DataContext]
    ge_config_version: float
    try:
        directory = directory or DataContext.find_context_root_dir()
        context = DataContext(context_root_dir=directory)
        ge_config_version = context.get_config().config_version

        if from_cli_upgrade_command:
            if ge_config_version < CURRENT_GE_CONFIG_VERSION:
                context = upgrade_project_one_or_multiple_versions_increment(
                    directory=directory,
                    context=context,
                    ge_config_version=ge_config_version,
                    from_cli_upgrade_command=from_cli_upgrade_command,
                )
            elif ge_config_version > CURRENT_GE_CONFIG_VERSION:
                raise ge_exceptions.UnsupportedConfigVersionError(
                    f"""Invalid config version ({ge_config_version}).\n    The maximum valid version is \
{CURRENT_GE_CONFIG_VERSION}.
""")
            else:
                context = upgrade_project_zero_versions_increment(
                    directory=directory,
                    context=context,
                    ge_config_version=ge_config_version,
                    from_cli_upgrade_command=from_cli_upgrade_command,
                )

        return context
    except ge_exceptions.UnsupportedConfigVersionError as err:
        directory = directory or DataContext.find_context_root_dir()
        ge_config_version = DataContext.get_ge_config_version(
            context_root_dir=directory)
        context = upgrade_project_strictly_multiple_versions_increment(
            directory=directory,
            ge_config_version=ge_config_version,
            from_cli_upgrade_command=from_cli_upgrade_command,
        )
        if context:
            return context
        else:
            cli_message(string=f"<red>{err.message}</red>")
            sys.exit(1)
    except (
            ge_exceptions.ConfigNotFoundError,
            ge_exceptions.InvalidConfigError,
    ) as err:
        cli_message(string=f"<red>{err.message}</red>")
        sys.exit(1)
    except ge_exceptions.PluginModuleNotFoundError as err:
        cli_message(string=err.cli_colored_message)
        sys.exit(1)
    except ge_exceptions.PluginClassNotFoundError as err:
        cli_message(string=err.cli_colored_message)
        sys.exit(1)
    except ge_exceptions.InvalidConfigurationYamlError as err:
        cli_message(string=f"<red>{str(err)}</red>")
        sys.exit(1)