diff --git a/dbt_meshify/storage/yaml_editors.py b/dbt_meshify/storage/yaml_editors.py index b2fd0f2..3262518 100644 --- a/dbt_meshify/storage/yaml_editors.py +++ b/dbt_meshify/storage/yaml_editors.py @@ -136,7 +136,12 @@ def get_latest_yml_defined_version(self, model_yml: Dict[str, Any]): if no versions, returns 0 """ model_yml_versions = model_yml.get("versions", []) - return max([v.get("v") for v in model_yml_versions]) if model_yml_versions else 0 + try: + return max([int(v.get("v")) for v in model_yml_versions]) if model_yml_versions else 0 + except ValueError: + raise ValueError( + f"Version not an integer, can't increment version for {model_yml.get('name')}" + ) def add_model_version_to_yml( self, diff --git a/tests/fixtures.py b/tests/fixtures.py index 1fd5948..437c065 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -252,3 +252,12 @@ - v: 2 defined_in: daves_model """ + +model_yml_string_version = """ +models: + - name: shared_model + latest_version: john_olerud + description: "this is a test model" + versions: + - v: john_olerud +""" diff --git a/tests/integration/test_version_command.py b/tests/integration/test_version_command.py index 776c84e..d02ad98 100644 --- a/tests/integration/test_version_command.py +++ b/tests/integration/test_version_command.py @@ -16,6 +16,7 @@ expected_versioned_model_yml_no_yml, model_yml_increment_version, model_yml_no_col_no_version, + model_yml_string_version, shared_model_sql, ) @@ -119,3 +120,36 @@ def test_add_version_to_yml(start_yml, end_yml, start_files, expected_files, com yml_file.unlink() reset_model_files(["shared_model.sql"]) assert actual == yaml.safe_load(end_yml) + + +@pytest.mark.parametrize( + "start_yml,start_files,command_options", + [ + ( + model_yml_string_version, + ["shared_model.sql"], + [], + ), + ], + ids=["1"], +) +def test_add_version_to_invalid_yml(start_yml, start_files, command_options): + yml_file = proj_path / "models" / "_models.yml" + reset_model_files(start_files) + yml_file.parent.mkdir(parents=True, exist_ok=True) + runner = CliRunner() + # only create file if start_yml is not None + # in situations where models don't have a patch path, there isn't a yml file to read from + if start_yml: + yml_file.touch() + start_yml_content = yaml.safe_load(start_yml) + with open(yml_file, "w+") as f: + yaml.safe_dump(start_yml_content, f, sort_keys=False) + base_command = ["--select", "shared_model", "--project-path", proj_path_string] + base_command.extend(command_options) + result = runner.invoke(add_version, base_command, catch_exceptions=True) + assert result.exit_code == 1 + assert "Version not an integer" in str(result.exception) + # reset the read path to the default in the logic + yml_file.unlink() + reset_model_files(["shared_model.sql"])