diff --git a/libs/core/kiln_ai/datamodel/basemodel.py b/libs/core/kiln_ai/datamodel/basemodel.py index 322fbdc..5e71ad8 100644 --- a/libs/core/kiln_ai/datamodel/basemodel.py +++ b/libs/core/kiln_ai/datamodel/basemodel.py @@ -8,7 +8,7 @@ class KilnBaseModel(BaseModel): - version: int = 1 + v: int = 1 # schema_version path: Optional[Path] = None @classmethod @@ -16,6 +16,12 @@ def load_from_file(cls: Type[T], path: Path) -> T: with open(path, "r") as file: m = cls.model_validate_json(file.read(), strict=True) m.path = path + if m.v > m.max_schema_version(): + raise ValueError( + f"Cannot load from file because the schema version is higher than the current version. Upgrade kiln to the latest version. " + f"Class: {m.__class__.__name__}, id: {getattr(m, 'id', None)}, path: {path}, " + f"version: {m.v}, max version: {m.max_schema_version()}" + ) return m def save_to_file(self) -> None: @@ -28,4 +34,6 @@ def save_to_file(self) -> None: with open(self.path, "w") as file: json.dump(data, file, indent=4) - print(f"Project saved to {self.path}") + # increment for breaking changes + def max_schema_version(self) -> int: + return 1 diff --git a/libs/core/kiln_ai/datamodel/project.py b/libs/core/kiln_ai/datamodel/project.py index b038228..04ef73a 100644 --- a/libs/core/kiln_ai/datamodel/project.py +++ b/libs/core/kiln_ai/datamodel/project.py @@ -3,3 +3,7 @@ class KilnProject(KilnBaseModel): name: str + + +class KilnTask(KilnBaseModel): + name: str diff --git a/libs/core/kiln_ai/datamodel/test_basemodel.py b/libs/core/kiln_ai/datamodel/test_basemodel.py index aa36144..4a56200 100644 --- a/libs/core/kiln_ai/datamodel/test_basemodel.py +++ b/libs/core/kiln_ai/datamodel/test_basemodel.py @@ -1,12 +1,24 @@ import json import pytest from kiln_ai.datamodel.basemodel import KilnBaseModel +from pathlib import Path @pytest.fixture -def test_file(tmp_path): +def test_file(tmp_path) -> Path: test_file_path = tmp_path / "test_model.json" - data = {"version": 1} + data = {"v": 1} + + with open(test_file_path, "w") as file: + json.dump(data, file, indent=4) + + return test_file_path + + +@pytest.fixture +def test_newer_file(tmp_path) -> Path: + test_file_path = tmp_path / "test_model_sv.json" + data = {"v": 99} with open(test_file_path, "w") as file: json.dump(data, file, indent=4) @@ -16,7 +28,7 @@ def test_file(tmp_path): def test_load_from_file(test_file): model = KilnBaseModel.load_from_file(test_file) - assert model.version == 1 + assert model.v == 1 assert model.path == test_file @@ -27,10 +39,15 @@ def test_save_to_file(test_file): with open(test_file, "r") as file: data = json.load(file) - assert data["version"] == 1 + assert data["v"] == 1 def test_save_to_file_without_path(): model = KilnBaseModel() with pytest.raises(ValueError): model.save_to_file() + + +def test_max_schema_version(test_newer_file): + with pytest.raises(ValueError): + KilnBaseModel.load_from_file(test_newer_file) diff --git a/libs/core/kiln_ai/datamodel/test_project.py b/libs/core/kiln_ai/datamodel/test_project.py index 0b9cf68..c3bca77 100644 --- a/libs/core/kiln_ai/datamodel/test_project.py +++ b/libs/core/kiln_ai/datamodel/test_project.py @@ -6,7 +6,7 @@ @pytest.fixture def test_file(tmp_path): test_file_path = tmp_path / "test_project.json" - data = {"version": 1, "name": "Test Project"} + data = {"v": 1, "name": "Test Project"} with open(test_file_path, "w") as file: json.dump(data, file, indent=4) @@ -16,7 +16,7 @@ def test_file(tmp_path): def test_load_from_file(test_file): project = KilnProject.load_from_file(test_file) - assert project.version == 1 + assert project.v == 1 assert project.name == "Test Project" assert project.path == test_file @@ -28,7 +28,7 @@ def test_save_to_file(test_file): with open(test_file, "r") as file: data = json.load(file) - assert data["version"] == 1 + assert data["v"] == 1 assert data["name"] == "Test Project"